Legacy GM Need Help Storing Players Into List

P

Pyxus

Guest
In all honestly, I'm beginning to feel like I should perhaps drop this "project" until i'm more proficient at the game maker language. Also, I apologize if the title is misleading cause i'm not exactly sure how to explain my problem but i'll try since I feel as if i'm missing something annoyingly simple, which is a tendency for me... especially when trying to learn something new. But I digress... In short I wanted to create a sort of twitch game bot where each viewer can get an avatar that appears on stream. I found a twitch irc script on the game maker market that works perfectly well, in fact everything works well as far as I can tell. When the "!join" command is used in chat a player object is created and the name of the person who used the command appears on top of their head. My issue is I need to store the viewer's name some how so that the viewer can't spam the command and make multiple clones.

So ideally I thought the best way to do this was to have the variable sender (which is the person using the command) added to the list players. sender should return a string each time (i.e the player's name) so each time the command is run the player/viewer is added to the player list. My essential questions are, is there a way to check if the player is already on the list first, is there a better way to be doing this, and is there a way to draw all the players in the list (only because I want to visually see if players are being added to the list"

If I sounded confusing i'll gladly elaborate, and just so you're aware this isn't a serious project. I literally saw the script in the market and thought "I wonder if I can do xyz" so it's all for the sake of learning ;)

*left out some stuff from the networking event since it isn't important(I don't think), but if you do want to see anything, again, I'll gladly do so!

obj_irc_controller
Code:
----Create Event-----
///Set Channel & Bot
scr_saveinfo();
scr_loadinfo();
irc_create_socket("irc.twitch.tv", 6667, username, oauth, channel, true);
background_colour = colour;
alarm[0] = 1
//Organize Players
randomize();
players = ds_list_create();
new_player = ""


-----Networking Event-----

if msg = "!join"
        {
        new_player = sender
        newObj = instance_create(random(400), random(400), obj_player);
        newObj.image_index = round(random(8));
        ds_list_add(players, sender);
        irc_send_message("@" + sender + " is in the game");
        }
        else
        {
        //Do Nothing
        }
obj_irc_controller
Code:
-----Create Event-----
///Initialize Avatar
//Avatar Customization
image_speed = 0;
face_expression = irandom(6);
name = obj_irc_controller.new_player;
hat = 0;

-----Draw Event-----
draw_self();
draw_text(x-25, y-35, name); //Draw Name
draw_sprite(spr_expressions, face_expression, x, y); //Draw Face
[B]
[/B]
 
N

Noyemi K

Guest
I'll address your questions piecewise, since I think you have a fun little idea and there's nothing wrong with trying to get help figuring out execution:

1. "Is there a way to check if a player is already in the list first?"
Yes! With ds_list_find_index, you can set up a conditional where the player is only added if the check doesn't return an index of -1 (no existing index for that value) For example:
Code:
if msg = "!join"
       {
        new_player = sender
        if (ds_list_find_index(players, sender) != -1)
            {
            newObj = instance_create(random(400), random(400), obj_player);
            newObj.image_index = round(random(8));
            ds_list_add(players, sender);
            irc_send_message("@" + sender + " is in the game");
            }
            else 
            {
            //Do Nothing
            }
        }
        else
        {
        //Do Nothing
        }
2. "Is there a better way to be doing this?"
Possibly! With the current scale of operations though, it seems to work just fine in my opinion. Perhaps if you needed more attributes than just the name of a player, you'd need a bit of a refactor here.

3. "Is there a way to draw all the players in the list?"
Yes! Use a for loop to draw all the list elements, since you can get the size of a list with ds_list_size(). Here's a very simplistic illustration:
Code:
for (i = 0; i < ds_list_size(players); i++)
{
    draw_text(x, y+i * text_height, players[| i])
    //obviously text_height here is a placeholder for whatever method you'll use to get the height of text.
}
 
P

Pyxus

Guest
I'll address your questions piecewise, since I think you have a fun little idea and there's nothing wrong with trying to get help figuring out execution:

1. "Is there a way to check if a player is already in the list first?"
Yes! With ds_list_find_index, you can set up a conditional where the player is only added if the check doesn't return an index of -1 (no existing index for that value) For example:
Code:
if msg = "!join"
       {
        new_player = sender
        if (ds_list_find_index(players, sender) != -1)
            {
            newObj = instance_create(random(400), random(400), obj_player);
            newObj.image_index = round(random(8));
            ds_list_add(players, sender);
            irc_send_message("@" + sender + " is in the game");
            }
            else
            {
            //Do Nothing
            }
        }
        else
        {
        //Do Nothing
        }
2. "Is there a better way to be doing this?"
Possibly! With the current scale of operations though, it seems to work just fine in my opinion. Perhaps if you needed more attributes than just the name of a player, you'd need a bit of a refactor here.

3. "Is there a way to draw all the players in the list?"
Yes! Use a for loop to draw all the list elements, since you can get the size of a list with ds_list_size(). Here's a very simplistic illustration:
Code:
for (i = 0; i < ds_list_size(players); i++)
{
    draw_text(x, y+i * text_height, players[| i])
    //obviously text_height here is a placeholder for whatever method you'll use to get the height of text.
}
Yep, something fairly simple... why am I not surprised xD. Regardless thanks very much for the help! Everything is currently working as intended and I can finally continue on to the fun part of this project, which is getting the avatars to do things like wander around and bounce into each other. :D
https://goo.gl/8kpnvR
 
N

Noyemi K

Guest
Anything else you need, make sure to make use of the GM Reference every step of the way; there's a lot of builtins for doing data structure things (such as writing ds_maps to files).
 
P

Pyxus

Guest
Anything else you need, make sure to make use of the GM Reference every step of the way; there's a lot of builtins for doing data structure things (such as writing ds_maps to files).
Will do! and I don't really need anything else, I did want to know if it was possible to store players added to the list in a text or ini file... not sure if this is the best way of doing it but I have it in mind to store player names along with stats for when the program stops and starts. But on my list of priorities this is kind of low, I'm currently just messing around with different things and thinking of what I really want to do with this, and if you can't help me here I'll cross that bridge when I get to it and do a bit of research.
 
N

Noyemi K

Guest
I just implemented some file handling stuff in my own game, so manipulating/saving lists and other data structures is still fresh on my mind if you need any help with that.
 
Top