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

Problem with instances' creation code

T

Toxidalf

Guest
Hello, this time I have a problem dealing with rooms.
Basically, I want to make the classic revisitable rooms, to do this I'm using an object that checks for collision with the player, and when this happens I use its (the object trigger) creation code in the room editor to manipulate in which room the player will be put into and the coordinates of him as well.

Now, this works fine.
My player gets to the next room and everything, but only if there is one trigger in the room. If I have two, situation gets tough... my player won't just go where my trigger tells him to go.

I tried using functions such as instance_nearest, distance_to_object to force the player to interact only with the nearest trigger, but that won't work... any ideas?! Thanks!!
 
T

Toxidalf

Guest
Post your collision code.

Sounds like you might be using an object index rather than the instance ID.
Thanks for the reply.
(I am using GM 8.1)

Collision is with an object called "CONTROL":

//COLLISION event with CONTROL
if hspeed == 0 {
room_goto(other.rc)
transition_kind = 21
transition_steps = 110
}

//ROOM END event
self.x = CONTROL.xc
self.y = CONTROL.yc

Note: "rc", "xc" and "yc" are variable that I've created withing the create event of the CONTROL object, I'm using them to set the player into new rooms...
 
W

Wraithious

Guest
hi, you need to use the trigger object's id to get the proper code for it's particular instance to run, also when you transfer to another room the very last thing you want to do is call room_goto because all other code after that is basically ignored. you could set it up something like this:
define 3 global variables in a first run object at game start
Code:
global.transfer = 0;//will initiate the player's repositioning
global.roomTransferToX=0;//will hold new x location when going to next room
global.roomTransferToY=0;//will hold new y location when going to next room
obj_player create event:
Code:
hitTrigger=0;//will hold id of the CONTROL object
if global.transfer=1 alarm[0]=1;//if we've been transported to a new room by the CONTROL object
obj_player collision with CONTROL:
Code:
with(instance_nearest(x,y,CONTROL))
{
obj_player.hitTrigger=id;
if id=obj_player.hitTrigger canGo=1;//this notifies the correct CONTROL to start room change
global.transfer=1;//this will set the alarm to reposition player after room change
}
alarm[0] event:
Code:
x=global.roomTransferToX;
y=global.roomTransferToY;
global.transfer=0;
CONTROL create event:
Code:
canGo=0;
CONTROL step event:
Code:
if(canGo=1)
{
if(instance_exists(obj_player)// set obj_player's position and make the room transfer
    {
    canGo=0;
    global.roomTransferToX=xc;//set the coordinates for player to appear in room
    global.roomTransferToY=yc;
    room=rc;//lastly go to the room
    }
}
 
Last edited by a moderator:
T

Toxidalf

Guest
hi, you need to use the trigger object's id to get the proper code for it's particular instance to run, also when you transfer to another room the very last thing you want to do is call room_goto because all other code after that is basically ignored. you could set it up something like this:
define 3 global variables in a first run object at game start
Code:
global.transfer = 0;//will initiate the player's repositioning
global.roomTransferToX=0;//will hold new x location when going to next room
global.roomTransferToY=0;//will hold new y location when going to next room
obj_player create event:
Code:
hitTrigger=0;//will hold id of the CONTROL object
if global.transfer=1 alarm[0]=1;//if we've been transported to a new room by the CONTROL object
obj_player collision with CONTROL:
Code:
with(instance_nearest(x,y,CONTROL))
{
obj_player.hitTrigger=id;
if id=obj_player.hitTrigger canGo=1;//this notifies the correct CONTROL to start room change
global.transfer=1;//this will set the alarm to reposition player after room change
}
alarm[0] event:
Code:
x=global.roomTransferToX;
y=global.roomTransferToY;
global.transfer=0;
CONTROL create event:
Code:
canGo=0;
CONTROL step event:
Code:
if(canGo=1)
{
if(instance_exists(obj_player)// set obj_player's position and make the room transfer
    {
    cango=0;
    global.roomTransferToX=xc;//set the coordinates for player to appear in room
    global.roomTransferToY=yc;
    room=room2;//lastly go to the room
    }
}
Thanks very muchj! Now I gotta go, but I'm testing this when I come back.

Now, just a quick question before I go: "also when you transfer to another room the very last thing you want to do is call room_goto because all other code after that is basically ignored", what are the real consequences?!
 
W

Wraithious

Guest
Thanks very muchj! Now I gotta go, but I'm testing this when I come back.

Now, just a quick question before I go: "also when you transfer to another room the very last thing you want to do is call room_goto because all other code after that is basically ignored", what are the real consequences?!
When you switch rooms all objects are destroyed, and then objects are created in the new room which means their create events are run at that time, so any local variables set in the previous rooms are reset to their original values.
 
T

Toxidalf

Guest
When you switch rooms all objects are destroyed, and then objects are created in the new room which means their create events are run at that time, so any local variables set in the previous rooms are reset to their original values.
Thanks, so... I asked that because I had another issue: points and ammo. These items respawn once I get back to previous or new room. Is the explanation you gave me, the answer?!
I tried using global variables... nothing was worth it D:
 
W

Wraithious

Guest
Hmm global variables should remain unchanged from room to room, but make sure you only define global variables in the first object in the first room of the game, and also you can never allow the player to go back to the first room in the game or all the global variables will be reset, normally the first room is basically used to set up and start the game.
 
T

Toxidalf

Guest
Hmm global variables should remain unchanged from room to room, but make sure you only define global variables in the first object in the first room of the game, and also you can never allow the player to go back to the first room in the game or all the global variables will be reset, normally the first room is basically used to set up and start the game.
Ok thanks!

Btw, I haven't tested your code because I found another workaround by myself, but thanks anyway!
 
Top