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

GameMaker Door warp using instance_nearest() [SOLVED]

PlayerOne

Member
Trying to add the ability to "travel" within the room. I have 2 doors - DoorA and DoorB - and that when the player presses E the player will warp to DoorA from DoorB and vise versa.

Problem is DoorA isn't working properly despite the fact it uses the exact code as in DoorB. However DoorB works without issue and that really doesn't add.

I disabled any code in my player object to rule out the problem and still nothing. I know I can use pre-determined x and y positions for this kind of stuff but this system is far easier to work with given the areas I'm building.

Any ideas?

Code:
var _target = oPlayer

if place_meeting(x+0,y+0,_target) && keyboard_check_pressed(vk_e)
{
   
   var Nearest = instance_nearest(x, y, DoorToDoor_B);
   
   with(_target)
   {
   x = Nearest.x;
   y = Nearest.y;   
   }
       
}
 
F

FuRyvok

Guest
Try to create an Object: oWarp
to the create event add
Code:
roomToGo =  "";
toGoX    = "";
toGoY    = "";
Place the warp object at the room, and double click on it, and press the Creation Code
in there you can make every door unique by adding the coordinates.

In the player Event add a Collision with the oWarp
Code:
if(keyboard_check_pressed(ord("E"))){
room_goto(other.roomToGo);
x = other.toGoX;
y = other.toGoY;
}
 

Relic

Member
You use vk_e instead of ord(‘E’). If you don’t have a compile error you have set this as a macro I guess?

Any chance you have an additional doortodoorB under door A in the room editor by mistake?

These door objects have the same sprite? If not, check the collision mask hasn’t been set way off for door A.
 

PlayerOne

Member
You use vk_e instead of ord(‘E’). If you don’t have a compile error you have set this as a macro I guess?

Any chance you have an additional doortodoorB under door A in the room editor by mistake?

These door objects have the same sprite? If not, check the collision mask hasn’t been set way off for door A.
Sorry, vk_e is a macro. I really have to make a note of that when posting and no I don't have duplicate objects in the same room. I created a blank project running the same code and got the same issue anyway.
 
D

Danei

Guest
Is there any chance it is working, but that the 2nd door is also triggering in the same frame and moving the player back to the first door?
 

PlayerOne

Member
Is there any chance it is working, but that the 2nd door is also triggering in the same frame and moving the player back to the first door?
Not sure what you mean. It only triggers if E is pressed and if the player object is colliding with the door object.
 
D

Danei

Guest
Not sure what you mean. It only triggers if E is pressed and if the player object is colliding with the door object.
Yeah, but let's say you're standing on the first door; you press E; that frame it moves the player to the location of the 2nd door. Then that door's code runs (because its index is higher than the first door's). It's the same frame, so key_pressed ( E ) is still true and the player is touching it, so the player moves to the location of the first door, without appearing to move. That would explain why one door works and not the other. Try putting a show_debug_message when the player is moved by the door.
 

PlayerOne

Member
Yeah, but let's say you're standing on the first door; you press E; that frame it moves the player to the location of the 2nd door. Then that door's code runs (because its index is higher than the first door's). It's the same frame, so key_pressed ( E ) is still true and the player is touching it, so the player moves to the location of the first door, without appearing to move. That would explain why one door works and not the other. Try putting a show_debug_message when the player is moved by the door.
After I replied to your post I mulled it over, tested it, and your correct this does happen. Odd. I'll be honest I never encountered this kind of problem before.
 
Top