[Ignore This]

L

LunarIceCream

Guest
Basically the player position resets when I re-enter a room. Camera position has been fixed. Read replies to know what the code is.

I'm using Shaun Spalding's gamemaker tutorial, and have just finished episode 7 of it.
 
Last edited by a moderator:

Relic

Member
You can make the player persistent - meaning it moves from room to room, retaining all of its variables (e.g x and y coordinates). Watch out for going back to the room where the player was created the first time though as then you will have 2 player instances.

Can also store the x variable of the player in a global variable when leaving a room. In the player create event you can set x=global.x_player to move the new player instance to that position - but with this option you need to be careful about what the global variable is the first time the game is started.

Many methods - the best depends on the rest of your game/current code.
 
L

LunarIceCream

Guest
Hmm...I'll try the first one. How do you make the player persistent?
 

Relic

Member
Seeing your game (and noting rooms are very different in size) you may have more luck making the room itself persistent.

This stops the room resetting every time you go back to it. Just watch that whatever triggered going from the first room to the second in your video may trigger again straight away when you return to the first room (for example if it was based on you player colliding with something or having the player x pos being greater than the room width)
 
L

LunarIceCream

Guest
How would you make a room persistent? I'm still pretty new to coding so I have no idea how, sorry.(marking it persistent did nothing so it can't be just that right)

The cameras in both rooms look like this, btw.
Camera in room1.PNG

camera in room2.PNG


And it's based on a collision.
 
Last edited by a moderator:
How would you make a room persistent? I'm still pretty new to coding so I have no idea how, sorry.(marking it persistent did nothing so it can't be just that right)
If persistent has been checked in the room editor’s room properties, then the room is persistent.

If the player is still spawning back at its original coordinates, then you must have those coordinates set somewhere in your code. Can you post your code?

As for the camera problem, it could be a few things.

1) the room is not using any cameras, make sure the camera/view you’re using has been enabled in the room properties
3)the camera size is not the same as the first size(it’s wider, thus showing the whole room)
2)the camera is not set to follow the player, or anything for that matter (set what it follows in the room properties)

It’s a little hard to know the solution without any code.

EDIT: if your cameras are based on a collision (as in, it moves when the player reaches the edge), this might not be a good idea. Instead, it would be more efficient, and better looking imo, if the camera followed the player. To do this, just set the player as what the camera follows in the room properties.
 
L

LunarIceCream

Guest
If persistent has been checked in the room editor’s room properties, then the room is persistent.

If the player is still spawning back at its original coordinates, then you must have those coordinates set somewhere in your code. Can you post your code?

As for the camera problem, it could be a few things.

1) the room is not using any cameras, make sure the camera/view you’re using has been enabled in the room properties
3)the camera size is not the same as the first size(it’s wider, thus showing the whole room)
2)the camera is not set to follow the player, or anything for that matter (set what it follows in the room properties)

It’s a little hard to know the solution without any code.

EDIT: if your cameras are based on a collision (as in, it moves when the player reaches the edge), this might not be a good idea. Instead, it would be more efficient, and better looking imo, if the camera followed the player. To do this, just set the player as what the camera follows in the room properties.
I feel like the player starts back at the original point because that's where I've set them in the room. It wouldn't be a big problem with the starting room, but I feel like it would start to get worse when I add more branching off rooms. I want to make it so the player doesn't have a single set spawn in every room.

Also, I got the code for the Camera Object, I don't think it's based on collisions.

Create Event:
Code:
///Set Up Camera
cam = view_camera[0];
follow = oPlayer
view_w_half = camera_get_view_width(cam) * 0.5;
view_h_half = camera_get_view_height(cam) * 0.5;
xTo = xstart;
yTo = ystart;
Step Event:
Code:
///Update Camera
//Update Destination
if (instance_exists(follow))
{
    xTo = follow.x;
    yTo = follow.y;
}
//Update Object Position
x += (xTo -x) / 25;
y += (yTo -y) / 25;

x = clamp(x,view_w_half,room_width-view_w_half);
y = clamp(y,view_h_half,room_height-view_h_half);

//Update Camera View
camera_set_view_pos(cam,x-view_w_half,y-view_h_half);
 
Last edited by a moderator:
I feel like the player starts back at the original point because that's where I've set them in the room. It wouldn't be a big problem with the starting room, but I feel like it would start to get worse when I add more branching off rooms. I want to make it so the player doesn't have a single set spawn in every room.

Also, I got the code for the Camera Object, I don't think it's based on collisions.

Create Event:
Code:
///Set Up Camera
cam = view_camera[0];
follow = oPlayer
view_w_half = camera_get_view_width(cam) * 0.5;
view_h_half = camera_get_view_height(cam) * 0.5;
xTo = xstart;
yTo = ystart;
Step Event:
Code:
///Update Camera
//Update Destination
if (instance_exists(follow))
{
    xTo = follow.x;
    yTo = follow.y;
}
//Update Object Position
x += (xTo -x) / 25;
y += (yTo -y) / 25;

x = clamp(x,view_w_half,room_width-view_w_half);
y = clamp(y,view_h_half,room_height-view_h_half);

//Update Camera View
camera_set_view_pos(cam,x-view_w_half,y-view_h_half);
In that case, you'll need to set the player's starting coordinates for each room based on where they entered. I'm not sure what your code is for going to the next room, but in my game I have invisible "doors" that, when the player touches them, sends them to the desired room and also changes their x/y coordinates to the desired coordinates, all right as the player touches the "door".

You'd need to make an obj_door.
In the create event:

Code:
new_room = noone;
new_x = noone;
new_y = noone;
Then, let's say you have four different rooms you can get to from just one central room (north,south,east,west). That would mean you need four doors, each with their own values for the variables we just set. To do that, you would go into the creation code of each door instance in your room. You get there by double clicking on the instance and selecting "creation code".

THEN, set the values you want for that specific door in that creation code.

Code:
new_room = room_2; //whatever you want
new_x = 10; //whatever you want
new_y = 50; //whatever you want
Now all we have to do is get that code to actually DO something. In the player object, make a collision event with the door object. In that collision event:

Code:
room_goto(other.new_room); //change to room specified in the door's creation code
x = other.new_x; //set player's x coordinate to the coordinate in the door's creation code
y = other.new_y; //set player's y coordinate to the coordinate in the door's creation code
There you go! I assume you know how to make objects collide with each other (collision masks, physics, solid, etc.). Just remember, if you're using physics, then x and y need to be phy_position_x and phy_position_y.

As for your cameras, I'm not sure why the view is so big in the second room. Double check and make sure the camera's width and height are the same as the first room's camera in the room properties. As for the jumping of the camera, go ahead and try to fix the player's coordinates first and then let me know if it persists... it shouldn't. Or you can try what I said above and just have the camera follow the player in the room properties... it's far easier.

P.S. If you want to keep your fade in/out effect when transitioning rooms, you would need to run the room transfer code AFTER the initial fade.
 
L

LunarIceCream

Guest
I actually have the door things, in hindsight I probably should've mentioned that as well. The player is placed just outside the visibility of the next room.

And all the creation code for them in each room is "Target = Room_name;"
Code:
Code for room transition object

Player collision event:

/// @desc Move to next room

with (oPlayer)
{
    if (hascontrol)
    {
        hascontrol = false;
        slidetrans(TRANS_MODE.GOTO,other.target);
    }
}
In the Player step event:
Code:
if(hascontrol)
{
key_left = keyboard_check(vk_left)  || keyboard_check (ord ("A"));
key_right = keyboard_check(vk_right) || keyboard_check (ord ("D"));
key_jump = keyboard_check_pressed(vk_space)
}
else
{
    key_right = 0;
    key_left = 0;
    key_jump = 0;
}
I dunno how relevant the stuff regarding doors is, considering I already have them. Unless the creation code interfere's with what I'm trying to accomplish.

Tell me if there is some sort of missing code. Btw good news, I figured out the camera!

(my replies are kinda all over the place, sorry bout that)
 
Last edited by a moderator:
L

LunarIceCream

Guest
Not sure if you saw it the last reply after I edited it like 50 times so xd
 
Not sure if you saw it the last reply after I edited it like 50 times so xd
Your code looks fine. How exactly are you putting the player itself in the room though? In your room editor, has the player instance been placed in both rooms or just the first? Is the player persistent?

Going into the second room seems fine, but then when you try to return to the first room, the player jumps back to its initial spawn point. I would do what the other user suggested and make sure the rooms are persistent, but the player should also be persistent. However, you'll need to set the x and y coordinates for the player in the door's creation code so that when the player goes to the room specified by the door code, they immediately teleport to the correct position (next to the door leading back).
 
L

LunarIceCream

Guest
How do I set the X and Y coordinates then? All the creation code for the doors say is:

Code:
Target = room2;
and
Code:
Target = room1;
The coordinates would have to do with the player and door object I'm guessing, but that's all I know about it. And the player object for the second room is actually placed just off the screen. There isn't any code telling the player object where to appear, which, in hindsight, is probably the problem in the first place.
 
How do I set the X and Y coordinates then? All the creation code for the doors say is:

Code:
Target = room2;
and
Code:
Target = room1;
The coordinates would have to do with the player and door object I'm guessing, but that's all I know about it. And the player object for the second room is actually placed just off the screen. There isn't any code telling the player object where to appear, which, in hindsight, is probably the problem in the first place.
If you're wanting smooth transitions between rooms, you should just remove the player from the second room all together. If the player is persistent, they will always spawn in each room (unless they are destroyed).

Now, add this to your collision event with the door.
Code:
with (oPlayer)
{
    if (hascontrol)
    {
        hascontrol = false;
        slidetrans(TRANS_MODE.GOTO,other.target);
        x = other.new_x; //add this
        y = other.new_y; //add this
    }
}
Then, in the creation code for each door, put this:
Code:
target = room2; //whatever room you are wanting
new_x = 20; //whatever coordinate you are wanting
new_y = 50; //whatever coordinate you are wanting
Put whatever coordinates you want the player to be at when they enter the room, with new_x being the x coordinate and new_y being the y coordinate. If no mistakes are made, the player should teleport to where they need to be the moment they enter the room.
 
L

LunarIceCream

Guest
x = other.new_x; //add this
y = other.new_y; //add this
All this line of code does is make the game crash when I try to enter the next room. I tried a combination of things, and it either ends up doing nothing, or I add this code back in and it crashes. Removing the player from the second room and marking it persistent doesn't work either.

I know I need to set coordinates for the player somehow, but that one doesn't seem to work. Maybe there's another way, or I need to write something in the player's creation code? I dunno. I might've just done it wrong somehow.

Code I used in collision event with player for door:
Code:
/// @desc Move to next room

with (oPlayer)
{
    if (hascontrol)
    {
        hascontrol = false;
        slidetrans(TRANS_MODE.GOTO,other.target); 
         x = other.new_x;
         y = other.new_y;
    }
}
Creation code for door in both rooms:
Room1
Code:
Target = room2
new_x = -18;
new_y = 649;
Room2
Code:
target = room1
new_x = 1626;
new_y = 636;
Crash message:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step EventoPlayer
for object oLevelEnd:

Variable <unknown_object>.<unknown variable>(100027, -2147483648) not set before reading it.
at gml_Object_oLevelEnd_Collision_b76b4d69_f3a6_4dab_94a5_9aa6e1466ae0 (line 9) - x = other.new_x;
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_oLevelEnd_Collision_b76b4d69_f3a6_4dab_94a5_9aa6e1466ae0 (line 9)
 
The function "other" refers to the instance that the player is colliding with (i.e. the door).

Make sure to set those three variables in the actual door object's create event:
Code:
target = noone; //default
new_x = noone; //default
new_y = noone; //default
You still need to keep what you have in the creation code, though.

EDIT: Found the tutorial you're using.

In the step event of oTransition, down under "case Trans_Mode.GOTO:" you'll want to change it to this:
Code:
mode = TRANS_MODE_INTRO;
room_goto(target);
oPlayer.x = oDoor.new_x;
oPlayer.y = oDoor.new_y;
break;
The issue I think was that the transition hadn't finished yet and you were still trying to set the player's coordinates before that, which is why the game couldn't find new_x and new_y?

Now see if it works?
 
Last edited:
L

LunarIceCream

Guest
Ah sorry, I’m on vacation and don’t have the original computer I was doing this on, so I’ll get back to this soon.
 
L

LunarIceCream

Guest
Ok, back. So could you clarify on this? I tried adding what you said, tried adding both things, but I don't really get it?

Do I add just this,
Code:
mode = TRANS_MODE_INTRO;
room_goto(target);
oPlayer.x = oDoor.new_x;
oPlayer.y = oDoor.new_y;
break;
this,
Code:
with (oPlayer)
{
    if (hascontrol)
    {
        hascontrol = false;
        slidetrans(TRANS_MODE.GOTO,other.target);
        x = other.new_x; //add this
        y = other.new_y; //add this
    }
}
or both?

The function "other" refers to the instance that the player is colliding with (i.e. the door).

Make sure to set those three variables in the actual door object's create event:
Code:
target = noone; //default
new_x = noone; //default
new_y = noone; //default
You still need to keep what you have in the creation code, though.
Mostly I just need clarification on this, though.
 
Top