• 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]Access instance's "Creation Code" without room GUI Editor

L

Lori

Guest
Hey,

I seem to be missing something glaringly obvious about instances :-/

I have two instances of the same object in my room but they only appear after text input by the user (so I cannot put the instances in via the room GUI Editor or else they'd appear right away).

The instances are buttons and I want them to perform different things when clicked.

This task was easy enough for another set of buttons that were there right when the room stared - I just used the instances' "Creation Code" accessed via right click in the room editor and entered
Code:
if mouse_check_button_released(mb_left) then btn1_clicked = true
and btn2_clicked = true for the second button.

How can I access this function in code directly, without the GUI?
Or how can I reference each of two instances (of the same object in one room) in a mouse event? What am I missing here? :(

This is what I had:
Code:
//"yes" button
var btnCheckAnswerY = instance_create (220,356,obj_btn_answerCheck);
btnCheckAnswerY.answerCheck = "Yes"; //This fills the button's text variable
btnCheckAnswerY.checkYesNo = 1; //put this in to use for drawing one of two sprites for the button


//"no" button
var btnCheckAnswerN = instance_create (370,356,obj_btn_answerCheck);
btnCheckAnswerN.answerCheck = "No";
btnCheckAnswerN.checkYesNo = 0;
And then I tried to use that checkYesNo variable in the object obj_btn_answerCheck's mouse event...which of course did not work, because why would it...
Code:
if checkYesNo = 1  //if YES has been clicked
{
    score += 1; 
    room_goto(rm_00_Congratulations);
   
}

if checkYesNo = 0 //if NO has been clicked
{
    room_goto(rm_00_TooBad);
}
Game Maker Studio 1.4
 
U

Ulrich6

Guest
You have to put both instances in the room (in the GUI editor).
Then, considering they have a sprite assigned, in the Create Event of their object you set a variable called visible to false (so they don't show up when you start the game).
Then, whenever you want, in the game, you just have to set the instance's variable visible to true.
 

Weird Dragon

Wizard
GMC Elder
You can assign an individual code to an instance immediately after creating it this way:
Code:
with (instance_create(x,y,obj_whatever))
    {
        //your code;
    }
 
A

Aura

Guest
First of all, the creation code of an instance runs only once when it is created, so putting something that needs to be executed every step (the if statement) there is a bad idea. You can't access the creation code of an instance dynamically. And even if you could, you won't be able to use it the way you want to.

Either way, what you are trying to do is doable enough if you learn how to put variables to some use in situations like this one. You could easily set a variable to two different values (strings are recommended) in the creation codes of both of the instances. You could then go about using it as an identifier.

Code:
if (mouse_check_button_released(mb_left) && instance_position(mouse_x, mouse_y, id) != noone) {
   if (my_type == "exit_game") {
      //End the game
   }
   else if (my_type == "run_game") {
      //Run the game
   }
}
 

Mike

nobody important
GMC Elder
Not quite sure what your asking here.... but you can "name" each instance. If you select one you'll see an INST_xxx name at the bottom. This ID can be used in code as it's the instance ID at runtime. If you right click the instance in the room editor, you can set what this name is, and use that - menu_but_ok, or menu_but_cancel for example.
 
L

Lori

Guest
Thank you, everybody!
Ulrich6: Yeeep. That's what I would call glaringly obvious :D Thanks for the easy and beginner-friendly way to do this
 
Top