GameMaker Need help with Top down Doors (GMS2)

KPJ

Member
I am working on a top down shooter game, I want to implement doors, that swing open and closed slowly everytime the player is close to it and presses F.

Also, when i try and open the door, all doors are affected, not just one. I am aware of using other(), but I just can seem to find out how to use other() in this situation

But when I run the game and press F, the door is just offset by a tiny amount and nothing else happens. Any help?

Code:
Code:
//Player Create
global.door = false;

//Player Step Event
if (distance_to_object(oDoor) < 40 && (keyboard_check_pressed(ord("F"))))
{
    global.dooropen = true;
}

//Door object step event
if (global.door == true && image_angle == 0)
{
    image_angle += 5;
  
    if (image_angle == 90)
    {
        global.door = false;
    }
}

if (global.door == true && image_angle == 90)
{
    image_angle -= 5;
    if (image_angle == 0)
    {
        global.door = false;
    }
}
Note: I am using the sprite origin (set to bottom left) as the hinge, using image_angle
Any help would be appreciated. Thanks!
 
T

Taddio

Guest
It looks like the "open" variable of yyour doors is global in scope, and I can't imagine why on earth that is.
EACH door instance must have it's own "open" variable, defined in obj_door.
You will also have to reference to the door INSTANCE and not the obj_door per se, or all your doors will open and close at the same time, which is probably not what you want.
 
  • Like
Reactions: KPJ

KPJ

Member
It looks like the "open" variable of yyour doors is global in scope, and I can't imagine why on earth that is.
EACH door instance must have it's own "open" variable, defined in obj_door.
You will also have to reference to the door INSTANCE and not the obj_door per se, or all your doors will open and close at the same time, which is probably not what you want.
Taddio, sorry, that was an error in my player step event (i accidentially wrote global.dooropen instead of global.door). Here is the correct code:
Code:
//Player Step
if (distance_to_object(oDoor) < 40 && (keyboard_check_pressed(ord("F"))))
{
    global.door = true;
}
Also how would i reference the instance instead of the object door?
 
T

Taddio

Guest
Well, all the doors are still going to open/close at once. Your door step event only checks for image_angle and global.door
Personally, I would ditch that global variable altogether and make a "open" variable (local, not global) in the door create event, not the player's.
Open = false;
And then work from there. You'll then have to get the instance id of the door you're next to, and if you press F, change that INSTANCE (i.e. not the obj_door at large) "Open" variable you created to true.

Edit: lots of ways to get the instance id, but in the case of a door, you can simply do
doorID = instance_nearest(x,y,obj_door);

doorID would return the unique id of the door you're next to.
Check the manual for more info, there's a ton of golden info on that!!
 
Last edited by a moderator:
  • Like
Reactions: KPJ
T

Taddio

Guest
You should definately get comfortable with this principle (again, the manual is the bible), because you'll get across this situation A LOT. Open a chest and they all open, killing one enemy and they all die, etc.
Rule of thumb is reference the object if there's only ONE instance of it (like an obj_controller, for example), and use INSTANCE ID for all the rest (bullets, enemies, doors, chests, etc.)
Hope that makes sense to you!
Cheers
 
  • Like
Reactions: KPJ

KPJ

Member
Thanks for the explaination as well Taddio. Implemented this in my game and worked perfectly!
 
Top