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

GML Door Opening

O

ohbadman

Guest
I'm looking to create a way to open a door and go through it but activated by a key press. I've been having some issues. My logic, which is probably wrong, is to have it check to see if the player is colliding with the door object and if they are pressing a button then it will take them through the door to the next room. Is there a way to do it similar to this or am I going about it the wrong way? Right now I have it set up in a basic way, working, where if the player touches the door it sends them to the next room.
 
O

ohbadman

Guest
Problem I was having is that it doesn't seem to work. When I run the game and collide with the object nothing seems to be happening. I've played around with several variations of code to see if something is wrong. It's either that you walk into it and it takes you there with no extra buttons being pressed or nothing happens at all. I know there are longer routes to do what I want but I'm looking for a simpler route. I am pretty new to game maker and code but is it as simple as that it wont work due to the fact that it would only check that on initial collision and it's not a constant check?
 

Gamebot

Member
All the following will go in your door code. You can use instance_nearest to find how close the player is .

I don't know what your code specifically is but I just use one sprite for the door opening and closing again. If I am close to the door (and enter) I let the animation start. Then check with this:
Code:
if (image_index < = 10) image_index += .3
You can then check if a character is away from the door, if ! exists...how ever your doing it. // Do something // or
Code:
if (image_index < 20) image_index += .3
If you want to close it.

In animation end event:
image_index = 0; image_speed = 0;
 
O

ohbadman

Guest
Are you hitting a button though or are you just walking up to the door and when you get close to it, it opens or going away from it, it closes? I'm not even trying to animate anything. I just want to go up to the door press a key and teleport the player.
 
W

Wraithious

Guest
You should just be able to put some code in the players step event to do that, something like:
Code:
if place_meeting(x,y,door_object) && keyboard_check_pressed(ord("X")) room=room2;
 

Joe Ellis

Member
sorry everyone who's posted here but its alot better to do it the other way round, ei have the collision event in the door object, it will be alot simpler, just have a collision with player, and check whether keyboard_check(vk_up) -- or whatever key you need to press, and room_goto(room) or room_goto_next
 
O

ohbadman

Guest
Here is what I have right now:

In my door Object I have:
Code:
new_x = 0;
new_y = 0;
new_room = noone;
---------------------------------------------------------------
In my Player Collision Event with the Door Object:
Code:
( room_exists(other.new_room)) {
       room_goto(other.new_room);
       x = other.new_x;
       y = other.new_y;
   }
---------------------------------------------------------------
And then I am setting the Specific New Room in the Creation Code (ie):
Code:
new_x = 90;
new_y = 230;
new_room = rm_2;
 
Last edited by a moderator:
O

ohbadman

Guest
I appreciate everyone's feedback and am trying a few different options out, I just want what is most efficient. How would integrate this code where I could still keep what I have for having it cleaner and just in the creation code of one door object instead of multiple doors and multiple checks. This is really helping me understand more! Thank you!
 

Joe Ellis

Member
asset_get_index is good for stuff like that, you can put the "next room's" name into it, eg. room_goto(asset_get_index(new_room)),

so basically that function will work for any room in any door object, meaning you can set the new_room variable in each door's creation code( as a string, ei. new_room = "Level 5" < the room's actual name, and it will goto that room

if you want any more info just message me, stuff like this can be confusing on messages
 
W

Wraithious

Guest
In my door Object I have:
Code:
new_x = 0;
new_y = 0;
new_room = noone;
This part seems ok ^^^, but it is here you should state what your new x and new y is like you have done in your 3rd code you posted

In my Player Collision Event with the Door Object:
Code:
( room_exists(other.new_room)) {
room_goto(other.new_room);
x = other.new_x;
y = other.new_y;
}
This part here tho is 90% of the problem, once you call room_goto that's it, you're going to the new room, which you defined as noone so now you are still in the same room but the code is now confused, and doesn't run any further than room_goto

And then I am setting the Specific New Room in the Creation Code (ie):
Code:
new_x = 90;
new_y = 230;
new_room = rm_2;
This here I'm not sure about, is this in the room creation code or the door create code or the player create code?
if it's in the room creation code you may have to use
Code:
with(instance_create(door_x_pos,door_y_pos,door)
{
new_x=90;
new_y=230;
}
in the room's creation code (and not have the door in the room as you will create it with the room creation code).

EDIT: oh but also you should have new_x and new_y as global variables, then in your player's create event use them to place your player at those coordinates.
 
Last edited by a moderator:
O

ohbadman

Guest
It's in the door creation code, sorry! So room_goto is using whatever room I put into the creation code of any door in my main room. So I am going to the next room, that's not so much the problem. It all works as far as touching it and going to the next room I set in the door creation code. I just would prefer if the player pressed a button to do so rather than just walking through it.
 

Joe Ellis

Member
sorry wraithious im not trying to be negative at all about what youv said, i know your trying to help, but im just saying from experience that this whole thing is way more complicated than it needs to be, basically your going to a room when you collide with a door while your pressing a key, this is a really simple situation and it can be configured in 3 lines with the right functions, again im not remotley trying to sound superior or anything, but basically the room can be any room in the project, you just need to get the room's id, which is a number, which no one knows except game maker, the same way as you get an object's id, only you have to do asset_get_index("room_name"),, you put in the room's name which you called it and it gives the room index which will work for room_goto(room_index) theres not really any other way around it with rooms
 
O

ohbadman

Guest
This should have been an if statement earlier I miscopied it.

Code:
if ( room_exists(other.new_room)) {
room_goto(other.new_room);
x = other.new_x;
y = other.new_y;
}
 

Joe Ellis

Member
yeah i wouldnt use that whole room_exists thing, if your setting the next room in the creation code the room will obviously exist,
maybe the player object for each room could have the next room
 
O

ohbadman

Guest
Thank you everyone for your help! It seems to be working now due to everyone's suggestions. I appreciate the help to a newcomer!
 
W

Wraithious

Guest
sorry wraithious im not trying to be negative at all about what youv said, i know your trying to help, but im just saying from experience that this whole thing is way more complicated than it needs to be, basically your going to a room when you collide with a door while your pressing a key, this is a really simple situation and it can be configured in 3 lines with the right functions, again im not remotley trying to sound superior or anything, but basically the room can be any room in the project, you just need to get the room's id, which is a number, which no one knows except game maker, the same way as you get an object's id, only you have to do asset_get_index("room_name"),, you put in the room's name which you called it and it gives the room index which will work for room_goto(room_index) theres not really any other way around it with rooms
no problem, you must have missed my first answer which was 1 simple line of code, but when he posted his code after it was more involved and I thought he was using the room creation event, but the answer i next gave him was actually a much cleaner way to do it because you can have 100 doors in every room and still go to the correct room from each door, And since it's all set up in the room creation event it is fast and minimal added code to the door or player object:
door create event:
Code:
next_x=0;
next_y=0;
new_room=room;
Room creation code:
exibitA.png
Code:
with(instance_create(door_x_pos,door_y_pos,door)
{
next_x=90;
next_y=230;
new_room=room2//whatever room this door should go to
}
//add as many more doors as you wish using the above code
Then in the door's collision event with the player:
Code:
if(keyboard_check_pressed(ord("X")))//whatever key you want to use
{
global.new_x=next_x
global.new_y=next_y
room_goto(new_room);
}
Create event of player:
Code:
x=global.new_x;
y=global.new_y;
And that's it, no asset_get_index or room id's, none of that, you already know what room each door goes to so there is no issue there.
 
Top