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

SOLVED Doors not functioning as intended

Hey. It's me again. I'm trying to make doors for a top-down game and I keep encountering this problem where only one door that I interact with and open will actually open, meanwhile the other one does not work at all.
Once open, it will not close again despite being programmed to.
1612218391781.png

This has been annoying me for hours.
Here is the code I wrote for the doors:
GML:
//Create Event
open = false;
Code:
//Step event
with (obj_doorHor)
{
    if (distance_to_object(obj_player) <= 20) && (keyboard_check_pressed(ord('E'))) && (!open)
    {
        open = true;
        sprite_index = spr_doorOpen;
    }
}

with (obj_doorHor)
{
    if (distance_to_object(obj_player) <= 20) && (keyboard_check_pressed(ord('E'))) && (open)
    {
        open = false;
        sprite_index = spr_door;
    }
}
What am I doing wrong? Any help is appreciated. Thanks!
 

Attachments

Last edited:

Slyddar

Member
To follow up on the above solution, and for future reference, place a checkpoint (F9) on the first open = true line, and run in debug mode (F6). When you open the door and step through the code (F11) you will see that both the top section, and the bottom section of code get run, since the door opens in the same step, so the bottom section all becomes true, which means the door closes again. Get used to running in debug and stepping through your code, as it will save you much time.
 
Thanks, I'll try this.

EDIT:
Tried messing around with these solutions and the doors seem to open, but sometimes they don't want to function. It seems inconsistent, or something.
 

Slyddar

Member
You can also output messages to ensure code is running as you expect. Use something like :
Code:
show_debug_message("1 : " + string(open));
as an example. Try changing the number and placing them just after the sprite changes. The output will appear in the output window, so you can essentially test it is working as expected.
 
Top