GM:S 1.4 Problem with doors

Discussion in 'Programming' started by pixeltroid, Mar 4, 2019.

  1. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    601
    I just discovered this bug.

    In my game, player uses a key to go past a door.

    Heres the code that determines what happens if player collides with door:

    Code:
    if global.keys>0 {
    with other{
    global.door_opened[door_id] = true
    instance_destroy();
    audio_play_sound(sfx_dooropen,9,false)
    instance_create(x,y,obj_dooropening)
     }
    global.keys = global.keys -1 }
    else {
    speed = 0
    }
    
    And here's what I have in the doors creat

    Code:
    if global.door_opened[door_id] = true {
    instance_destroy();
    }
    
    The code works...but ONLY if the door is NOT marked as a solid object. But, if its not marked as a solid object, the player can simply walk through the door even if he doesnt have a key!

    However, if I mark the door as a solid object, the player cannot open it even if he has a key!

    What am I doing wrong?

    Any help would be appreciated!
     
  2. NeZvers

    NeZvers Member

    Joined:
    Mar 24, 2018
    Posts:
    320
    Well, of course, that's why you should use your own collision "event" in the step event. Collision event is triggered when you collide with other objects.
    You simply need to check place_meeting(x+sign(horizontal_speed), y, obj_door) and if it's true use instance_place(x+sign(horizontal_speed), y, obj_door) to get id for with().
     
  3. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    601
    I'm not sure I follow :/

    Could you post the exact code for my objects?
     
  4. NeZvers

    NeZvers Member

    Joined:
    Mar 24, 2018
    Posts:
    320
    something like this
    Code:
    if(place_meeting(x+sign(horizontal_speed), y, obj_door))
    {
        var door = instance_place(x+sign(horizontal_speed), y, obj_door); //save ID
        global.door_opened[door] = true;
        instance_destroy(door);
        audio_play_sound(sfx_dooropen,9,false)
        instance_create(x,y,obj_dooropening)
    }
    
     
  5. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    601
    Hi.

    I got this error:


    What should I store "horizontal_speed" to?
     
  6. NeZvers

    NeZvers Member

    Joined:
    Mar 24, 2018
    Posts:
    320
    Your movement speed. If it's top-down then same for y.
    horizontal_speed was just placeholder name you have to figure out what to put there.
    You can use a variable like hinput = right_key - left_key;
    Basically, you need to check if there's a collision one pixel in front same as you would check for a floor in a platformer, only you get ID to manipulate door.
     
    Last edited: Mar 4, 2019

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