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

Two-Way Teleport Only Working One Way

B

BlooRooster1

Guest
Hello, I have a simple system that I absolutely cannot get to work properly.
I have a two "teleporters" within the same room that I want to transfer the player back and forth between. Entering tele A transfers to tele B, entering tele B transfers to tele A.

I have renamed two instances of "obj_tele" in the room editor as "tele1" and "tele2" and in the room editor instance creation code, I have:
tele1:
Code:
targetx = tele2.x;
targety = tele2.y;
tele2:
Code:
targetx = tele1.x;
targety = tele1.y;

And in the Step Event for the "obj_tele" i have:
Code:
if obj_player.state == "move" {
    if (instance_place(x,y,obj_player)) {
        if (obj_player.key_select) {
            obj_player.x = targetx;
            obj_player.y = targety;
        }
    }
}
Strangely, it works correctly from Tele 1 to Tele 2, but it does not go from Tele 2 back to Tele 1.

Checking the debug, shows that each instance is setting the correct 'targetx' and 'targety'.

Any idea what is happening here? Thanks in advance!
 

TheouAegis

Member
So targetx and targety aren't global variables?
And neither teleporter is a parent of the other?
And is the player state actually "move" when you try to teleport back?
 
B

BlooRooster1

Guest
So targetx and targety aren't global variables? They are not. This is my first use of creation code within instances in the room editor, but I'm assuming this makes them instance variables.
And neither teleporter is a parent of the other? No, they are both instances of the same object.
And is the player state actually "move" when you try to teleport back? Yes
 

TheouAegis

Member
Odd.
Oh, and how is key_select set in the player? What is the line of code that sets it? Make sure it is a keyboard_check_pressed or keyboard_check_released call.
 
B

BlooRooster1

Guest
Yes.
key_select = keyboard_check_pressed(vk_enter);

I am completely at a loss. I even changed the targetx and targety assignments the actual coordinates, but the result was the same.
 
A

ajan-ko

Guest
Maybe you teleport back to 2.
Code:
if obj_player.state == "move" {
    if (instance_place(x,y,obj_player)) {
        if (obj_player.key_select) {
            obj_player.x = targetx;
            obj_player.y = targety;
            obj_player.state == "" //no moving again
        }
    }
}
 
B

BlooRooster1

Guest
No change unfortunately. I had considered that, but it didn't explain why it works one way but not the other.

EDIT:
I just set up a little test. Removing the "if (instance_place...." statement, thereby simple checking if "key_select" is pressed anywhere and the result is the same. Pressing select ANYWHERE on the map, returns me to that Tele A. It's as if the value of targetx and targety are always Tele A's. Perhaps I need to add something to specify which instance I want to pull the targetx and y values from...which seems to make sense, but I have similiar map objects that are interacted with "key_select" and have not had to specify for any of them.
 
Last edited by a moderator:

Bingdom

Googledom
Perhaps you can try teleporting to the closest teleporter using instance_nearest()
If you don't want that then you can try assigning every teleporter with a channel in the room editor instance creation code
Then whenever the player wants to teleport through a teleporter you can get the target by doing
Player on collision with OBJ_Tele
Code:
var tele = other.id //Id of current teleporter
with (OBJ_Tele) {
    if tele.channel == other.channel and id != tele.id {
        other.targetx = x;
        other.targety = y;
    }
}
Adjust this to suit your preferences ;)
 
Last edited:
A

ajan-ko

Guest
No change unfortunately. I had considered that, but it didn't explain why it works one way but not the other.

EDIT:
I just set up a little test. Removing the "if (instance_place...." statement, thereby simple checking if "key_select" is pressed anywhere and the result is the same. Pressing select ANYWHERE on the map, returns me to that Tele A. It's as if the value of targetx and targety are always Tele A's. Perhaps I need to add something to specify which instance I want to pull the targetx and y values from...which seems to make sense, but I have similiar map objects that are interacted with "key_select" and have not had to specify for any of them.
I never use creation code,
if i were you, I'm gonna create obj_tele1,obj_tele2 with obj_tele as parents,

on create give 2 milisec,
on alarm [0] in obj_tele1 and obj_tele2, set your target.

with that way, i don't need to manually write code each time I make new room.
 
B

BlooRooster1

Guest
Well, it appears i "solved" it. with:

Code:
if obj_player.state == "move" && obj_player.canmove == true {
    if (instance_place(x,y,obj_player)) {
        if obj_player.key_select {
            obj_player.state = "freeze"
            obj_player.x = targetx;
            obj_player.y = targety;
            alarm[0] = 2;
        }
    }
}
In the alarm is: obj_player.state = "move"; so it looks like your first recommendation was correct ajan-ko. I just implemented it wrong the first time I tested. And I'm still not sure why this caused it to work one way, but not the other. But nevertheless it is solved. Thanks for your help everyone!
 

TheouAegis

Member
That's odd, though. I mean, I renamed 2 instances in my dummy project to Fred and Wilma, moved Fred 16 pixels right and Wilma 16 pixels left (so variable read-write), and drew their coordinates to the screen (variable read), and got not problems from either one. Not sure why your first code didn't work. But I did notice you had to change your player state.
 
D

Destroy

Guest
In @ajan-ko 's provided code there's obj_player.state == "" instead of obj_player.state = "" , so if you've tried copy-pasting that code to fix your problem, maybe that's why that didn't fix it?

EDIT: Also, it worked one way because instances run their code in creation order. Which means that if he's at the first teleport, first teleport teleports you to second teleport and second teleport teleports you back. If you're at the second teleport, first teleport does nothing and second teleport teleports you at first. ...or something like that.
 
A

ajan-ko

Guest
In @ajan-ko 's provided code there's obj_player.state == "" instead of obj_player.state = "" , so if you've tried copy-pasting that code to fix your problem, maybe that's why that didn't fix it?

EDIT: Also, it worked one way because instances run their code in creation order. Which means that if he's at the first teleport, first teleport teleports you to second teleport and second teleport teleports you back. If you're at the second teleport, first teleport does nothing and second teleport teleports you at first. ...or something like that.
Well, he is already solved it... and yes, I was wrong with ==, my bad... :D
 

TheouAegis

Member
= when it should be == is NOT a code-breaker. GM doesn't care how many equal signs you use.

b = 1
a = b = 1

works perfectly in GM, at least on Windows, which is how most people test their code. It is identical upon compile to

b = 1
a = b == 1

Even in conditionals, GM does not care.

if a = b

is the exact same as

if a == b
 
A

ajan-ko

Guest
= when it should be == is NOT a code-breaker. GM doesn't care how many equal signs you use.

b = 1
a = b = 1

works perfectly in GM, at least on Windows, which is how most people test their code. It is identical upon compile to

b = 1
a = b == 1

Even in conditionals, GM does not care.

if a = b

is the exact same as

if a == b
No, that's what he meant, I try to change his state to blank state

obj_player.state = "" // changing state to blank state after teleport

instead I write
obj_player.state == "" // doing abolutely nothing
 
Top