• 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!

How to link multiple buttons to multiple doors?

S

Stefan

Guest
Hi folks,

I am just starting with Game Maker and run into a problem I could not figure out.

I want to place multiple buttons and doors in a room.
Each button should be linked to a specific door.

I tried a lot of things, but nothing worked.
Would be great if someone could help me out here.

Thanks in advance!
 

Attachments

chamaeleon

Member
Hi folks,

I am just starting with Game Maker and run into a problem I could not figure out.

I want to place multiple buttons and doors in a room.
Each button should be linked to a specific door.

I tried a lot of things, but nothing worked.
Would be great if someone could help me out here.

Thanks in advance!
Use the instance code in the room editor for each instance to set some instance variable to a different value representing different rooms, and use that value in whatever event code you use for the button object to determine which button was clicked and what door to use.
 
G

Guitarmike

Guest
Depending on what exactly you are trying to do, another solution might be to have the door create it's button as part of its Create event and store a pointer to it in the object.

Code:
CREATE (DOOR OBJECT)
button = instance_create_depth(x,y,depth,obj_button);
button.owner = id;

CREATE (BUTTON OBJECT)
owner = 0;

LEFT RELEASED (BUTTON OBJECT)
with (owner)
{
   //Do something with the door
}
Every time a new door is created - either programmaticly or by placing it in the room editor, a button will be created, too (at run time), with a pointer back to the door instance that owns it. I hope this makes sense. I use this approach a lot in my games.

Good luck
 
S

Stefan

Guest
Thank you guys for your fast reply.

I am still struggling with this:

I want to manually place a few doors and buttons in one room.
Each time the player steps on a button (e.g. buttonID = 1) the corresponding door ( doorID = 1) should open.

In their creation code I set variables (like doorID = 1 and buttonID = 1) for comparison in a step event (if doorID == buttonID).
I am stuck here.
 

Relic

Member
When the two IDs match is one condition to open the door, but there willl be others.

Does the player have to be on or just near the button? Instance_place() or instance_nearest() may come in use. Does the player need to push a keyboard key to activate the switch? Once you know all conditions surrounding the successful button pushing:

//in button step event
If condition 1 and condition 2 {
with obj_door{ //cycle through all doors, checking IDs
If doorID=other.buttonID{
//door open
}
}
}
 

chamaeleon

Member
Seems to me there should be no need to iterate over doors. If the button contains a variable referencing a specific door, when the player steps on a given button you then implicitly know which door is supposed to open. No need to compare id's. Just use the door instance stored in the button
 

FrostyCat

Redemption Seeker
Seems to me there should be no need to iterate over doors. If the button contains a variable referencing a specific door, when the player steps on a given button you then implicitly know which door is supposed to open. No need to compare id's. Just use the door instance stored in the button
One situation where you won't have reliable access to the actual instance ID is when you're adding instances in the room editor. Here the indirect ID method makes a lot of sense, especially in conjunction with the Object Variables interface. If you try to assign a door's instance ID to a button's variable, you may end up with a race condition between the assignment and when the door gets created in the first place.

The small hit at runtime is worthwhile if it means you can build the level in a fully WYSIWYG environment. In a team setting, it could be even more worthwhile if this means you can delegate level design to a non-technical collaborator.
 
I would likely handle this using a ds map in either a global variable or a control instance. Each switch would then be given an ID, and when activated, it will tell the map to switch its state. Meanwhile, each door will also have an ID, and they will query the map to see if they need to open. This would avoid both the race conditions and extra instance iterations.
 

chamaeleon

Member
One situation where you won't have reliable access to the actual instance ID is when you're adding instances in the room editor. Here the indirect ID method makes a lot of sense, especially in conjunction with the Object Variables interface. If you try to assign a door's instance ID to a button's variable, you may end up with a race condition between the assignment and when the door gets created in the first place.
I'd really like to know what you think about the following..

One object, name obj_person. Two instances of obj_person in room editor, named i_bob and i_alice.

i_bob creation code in room editor
Code:
name = "Bob";
friend = i_alice;
i_alice creation code in room editor
Code:
name = "Alice";
friend = i_bob;
obj_person Create event
Code:
name = "NO NAME";
friend = noone;
obj_person Draw event
Code:
draw_text(x, y, "Name: " + name);
if (friend != noone)
    draw_text(x, y+16, "Friend: " + friend.name);
I would have been not surprised at all if this did not work due to the cyclic dependency on the instances in the room creation code, but I did get what I "wanted" displayed, the name of each instance, and the name of the friend..
 

FrostyCat

Redemption Seeker
I would have been not surprised at all if this did not work due to the cyclic dependency on the instances in the room creation code, but I did get what I "wanted" displayed, the name of each instance, and the name of the friend..
Room Creation Code runs after all room-default instances have been created, so you don't have a race condition to begin with.

See: Event Order
 

chamaeleon

Member
Room Creation Code runs after all room-default instances have been created, so you don't have a race condition to begin with.

See: Event Order
I thought maybe I was missing something when you mentioned race conditions in doing this. But perhaps you meant the object create event? Anyway, this is not my thread, apologies for polluting it with a sidebar.
 
Top