How to link multiple buttons to multiple doors?

Discussion in 'Programming' started by Stefan, Jan 6, 2019.

  1. Stefan

    Stefan Member

    Joined:
    Dec 28, 2018
    Posts:
    2
    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!
     

    Attached Files:

  2. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    783
    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.
     
    Stefan likes this.
  3. Guitarmike

    Guitarmike Member

    Joined:
    Jun 30, 2018
    Posts:
    40
    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
     
    Stefan likes this.
  4. Stefan

    Stefan Member

    Joined:
    Dec 28, 2018
    Posts:
    2
    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.
     
  5. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    548
    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
    }
    }
    }
     
    Stefan likes this.
  6. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    783
    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
     
  7. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,689
    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.
     
    chamaeleon likes this.
  8. stainedofmind

    stainedofmind Member

    Joined:
    Jun 20, 2016
    Posts:
    691
    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.
     
  9. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    783
    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..
     
  10. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,689
    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
     
  11. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    783
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice