• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM [Solved] DS_LIST & Buttons

Nixsdaddy

Member
And once again, I have come for assistance.

I have no problem with normal button objects and making them do what I want. But now.... I am at a point where there will be multiple buttons in the room and I wanted to make it simple for me.

Here's what I've done thus far.
- Made a room, ds_list with add button to add names into the list itself.
- Every time a name is added to the list, a button instance is created with the name on it.

What I'm trying to do.
- When I click on an button, save the name on the button into a global.name variable.

Every time I try, whether in the step event of the button or a mouse<left released> event, I can't get it to make the link between the button instance and the ds_list position so the name can be placed into the global variable.

Thank you beforehand for your help!
 

kamiyasi

Member
I'm not sure I 100% understand what you want.
Are you creating a new object instance for each button that's added to the room?
Something like...
Code:
for (i=0; i<ds_list_size(buttons); i++)
{
     with instance_create(x,y+(20*i),obj_button)
     {
          name = ds_list_find_value(other.buttons,other.i);
     }
}
I hope this helps.
 

Nixsdaddy

Member
I already have the buttons, button text all set up and working.

Here's things I have coded and working.
- blank room with an add to ds_list button which get's the user's input for a name and places it in the list.
- when anything is put into the list, a draw event places an instance of the button and I use draw_text() to place the value of the ds_list on the button.

Here's where I lose it. In the button object, to tell the clicked button what to do. Normally on a single button object, I can just use a mouse<mb_left> event and pop in what I want the button to do. But now that I have x amount of instances of the button based on the size of the ds_list, how can I code it to say that if there are 10 buttons created (plus 10 positions in my list, too)... if I click the 5th one (or whichever), the corresponding value from the ds_list would be placed into a global.name variable.

Sorry if I didn't explain it better. Guess I need to take my meds today.
 
C

CedSharp

Guest
When I create buttons, I generally create a "empty" button object, in the create event I set "text" and "action" to default values. ( text is a string and action is a script ).

Then, it's simply a matter of setting those variables. If you want to place many buttons in a room, use the instance creation code in the room to set text and action.
If you generate your buttons using instance_create, then setting text and action should be a piece of cake since you have access to the button's id :p

Code:
btn_play = instance_create( ..., ..., obj_button );
btn_play.text = "Play";
btn_play.action = scr_btn_play_clicked;
In the mouse left button click
Code:
if( script_exists( action ) ) script_execute( action );
this way, you can easily disable a button by setting 'action' to a non-script value ( I use noone ).

Regards,
CedSharp
 

Nixsdaddy

Member
Hmmmmm.... I'm having about as much luck with that as I was before. The buttons are created at the same time as the drawing of the text in a draw event in obj_drawList. Here's my code for the draw event.

Code:
draw_set_halign(fa_middle);
draw_set_valign(fa_center);
draw_set_font(fnt_list);
draw_set_color(c_white);

//draw out the contents of the ds_list
for (i = 0; i < ds_list_size(global.studentList); i++)
{
    if (i >= 0 && i < 9)
    {
        instance_create(200, yy, obj_studentBox);
        draw_text(200, yy, string(ds_list_find_value(global.studentList, i)));
        yy+=65;
    }

    if (i >= 9 && i < 18)
    {
        if (i == 9) { yy = 65; }
        draw_text(500, yy, string(ds_list_find_value(global.studentList, i)));
        instance_create(500, yy, obj_studentBox);
        yy+=65;
    }
    if (i >= 18 && i < 26)
    {
        if (i == 18) { yy = 65; }
        draw_text(800, yy, string(ds_list_find_value(global.studentList, i)));
        instance_create(800, yy, obj_studentBox);
        yy+=65;
    }
}

yy = y;
This gives me 3 columns of potential buttons with 8 rows per column. I'm doing this project for a elementary classroom where up to 26 students will be in the ds_list. I just wanted to make it easier for the kids to log in to the "game" instead of having to type their name every time the exact same way. But the buttons won't even be until a new name is added by the ds_list add object.

Sorry, gotta run and pick my son up from marching band practice.
 

Nixsdaddy

Member
I went ahead and did the Switch/Case route for now. Took me a little longer, but it's fine.

CedSharp, I'll still work on understanding your code so I may be able to implement it later. Thank you so much for your help!
 
C

CedSharp

Guest
@Nixsdaddy PM me anytime if you'd like a more in-detail explanation of what I told you, or if you would simply like help integrating it in your game :p
Glad I could be of any help ~
 
Top