SOLVED Automatic player position after room transition help

G

GunnyBoy

Guest
Most of the tutorials I've done involves creating a "door" object for the player to run into and have which room to change to as well as the x and y position to start the player in each of the door objects creation code. While this works I'm trying to think of a way to do this with JUST the door objects unique ID name that follows a naming convention of "current_room_to_next_room" where "current_room" and "next_room" change depending on the rooms I make. I was also going to try to figure out how to parse the unique ID names to automatically make the variables needed for this, but that's for later if this first idea works.

Anyway, from what I can find you can't make any sort of call to any object that isn't in the room you're current in, so what I'm currently trying to do is tell the player what it needs to look for on room change so it can reposition itself. The order I feel like it should work in is this:
1. Player collides with door
2. Door contains variables in creation code for which room to move to
3. Player builds string based on variables that are identical to unique ID of door to look for in next room
4. Player moves to next room with current position being the same as previous room
5. Player looks for instance of door matching unique ID build earlier
6. Player gets x and y of that door
7. Player moves to that x and y position (with coded adjustments so it's not colliding with door)

I know there's probably more issues that need to be solved with this like how the camera will most likely need to be reposition so it's not moving to the player from far away, but I can solve those later. My main issue right now is converting the string into the name of the object to look for. It appears to be converting it correctly when I try to debug it, but its returning as if it doesn't exist in the room after the transition.

Here's the code I have so far, let me know if I'm not showing enough. Also, sorry if my variable naming is confusing, some of it is still placeholder.

Door in room 1 (object name: o_transition, ID name: rm_1_to_rm_2)
Creation code:
Code:
_current_room = room;
_target_room = rm_2;
_target_spawn = asset_get_index(string(_target_room) + "_to_" + string(_current_room));
Player (Persistent)
Collision with o_transition:
Code:
room_goto(other._target_room);
Step event:
Code:
_door = instance_place(x, y, o_transition);

if _door != noone {
    _next_room_spawn = string(room_get_name(_door._target_room) + "_to_" +  room_get_name(_door._current_room));
    _next_room_spawn_object = asset_get_index(_next_room_spawn);
}
if (instance_exists(_next_room_spawn_object)) {
    _new_x = _next_room_spawn_object.x;
    _new_y = _next_room_spawn_object.y;
}
So my main goal right now is for "_new_x" and "_new_y" to equal 0 and 128 (The door in room 2 position), but it's returning -1 for "_next_room_spawn_object" which I think it means it think it doesn't exist even after the room change and the object clearly does exist, also the "_new_x" and "_new_y" are giving me weird numbers that I can't exactly pin point where it's coming from. The "asset_get_index" is returning the name of the object when I debug so my thinking is I'm building the string correctly, but somewhere converting that string for the game to look for the real object with that name doesn't seem to work. Any thoughts?
 
Last edited by a moderator:

Slyddar

Member
Any thoughts?
Yes. The only reason you are trying to convert to a string is for you to read it easily, but really it just makes things more complex.
If you don't want to deal with adding too much info in creation codes, create a ds_grid that is 3 wide and however many doors you want high, with this structure:
target_room, target_x, target_y

Then you just give each door a unique door_id in their creation code, starting at 0, and adding the grid entry with the target variables. Then when you collide with the door, look up the grid entry for that door id, e.g
Code:
room_goto(global.dsg_door[# door_id, 0]);
o_player.x = global.dsg_door[# door_id, 1];
o_player.y = global.dsg_door[# door_id, 2];
This way you can keep all the door destinations in the global.dsg_door grid, and just create it once in a game_start event of an instance that is created at game start. For each new door just add a grid entry with it's targets, and the relevant door_id in it's creation code.
 
Last edited:
G

GunnyBoy

Guest
Yes. The only reason you are trying to convert to a string is for you to read it easily, but really it just makes things more complex.
If you don't want to deal with adding too much info in creation codes, create a ds_grid that is 4 wide and however many doors you want high, with this structure:
index/door_id, target_room, target_x, target_y

Then you just give each door a unique door_id in their creation code, starting at 0, and adding the grid entry with the target variables. Then when you collide with the door, look up the grid entry for that door id, e.g
Code:
var _room = global.ds_grid_door[# door_id, target_room];
room_goto(_room);
o_player.x = global.ds_grid_door[# door_id, target_x];
o_player.y = global.ds_grid_door[# door_id, target_y];
This way you can keep all the door destinations in the global.ds_grid_door grid, and just create it once in a game_start event somewhere.
That's the part I'm trying to avoid, I dont want to have to state each starting position for each door that is going to exist. If I wanted to do that I could follow one of the tutorials I've found that has you list the starting x and y for the next room in each door that you collide with... but I'm just trying to think of a way that doesn't involve that since each door already has an x and y that could be manipulated to the correct position. While your suggestion is a bit more organized by having it all in one place I would still have to gather each x and y for each door manually. If what I'm trying to do doesn't work out I may considering that.
 
G

GunnyBoy

Guest
I'm still working on this and I feel like I'm close... it's 6AM... I don't sleep, I need answers!
 
G

GunnyBoy

Guest
Why aren't you using _target_spawn?
Honestly, I'm not entirely sure. I think that was left over from my testing. I don't think it's needed in this case since I'm using "_next_room_spawn" and "_next_room_spawn_object" to build the name of the object in the player object instead of doing it in the transition object.
 
G

GunnyBoy

Guest
Oh 💩💩💩💩 I think I got it... I had to basically rework the whole thing but I think I got it. I'm going to try to clean it up and I'll post my results in a bit.
 
G

GunnyBoy

Guest
Alright, this seems to work for now. I still have to try to include the string builder, but I'm just happy I got this part to work so far

Player
Create event:
Code:
_next_room_spawn = noone;
_new_x = 0;
_new_y = 0;
_offset_x = 0;
_offset_y = 0;
Collision with transition object:
Code:
_next_room_spawn = other._target_spawn; //Saves these variables to be used when next room starts
_offset_x = other._offset_x;
_offset_y = other._offset_y;
room_goto(other._target_room); //Changes room
Room start:
Code:
if _next_room_spawn != noone { //Uses previous saved variables to reposition player
    _new_x = _next_room_spawn.x;
    _new_y = _next_room_spawn.y;
    x = _new_x + _offset_x;
    y = _new_y + _offset_y;
}
Transition object (named "rm_1_to_rm_2" in room one and "rm_2_to_rm_1" in room 2)
Creation code:
Code:
_current_room = room;
_target_room = rm_2;
_target_spawn = rm_2_to_rm_1; //Which transition object to look for on room change
_offset_x = 32; //offset so you don't collide with transition object on room change
_offset_y = 64;
 
Top