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

SOLVED Return player to nearest solid after death.

Creating a Platformer project.

As it stands right now, whenever the player object "dies", they are destroyed and respawn where the player object is within the room -- which is the very beginning of the room.

I'm trying to make it so when the player dies, they instead respawn on a nearby platform. I'm honestly not entirely sure how to best approach this and am looking for advice or some code examples on how to go about accomplishing this task.

The player cannot simply respawn where they died because falling to their death into the void is possible, so respawning there would just result in a death loop.
 

TsukaYuriko

☄️
Forum Staff
Moderator
I'd set this up in a way where the player memorizes safe locations while standing on them. Safe locations would, for example, be defined as a platform that doesn't move. After dying, it can then be moved back to the last known safe location.
 
I'd set this up in a way where the player memorizes safe locations while standing on them. Safe locations would, for example, be defined as a platform that doesn't move. After dying, it can then be moved back to the last known safe location.
I had thought about this at first, but I'm not sure how to go about doing that. Any pointers on what I should be taking a look at?
 

TsukaYuriko

☄️
Forum Staff
Moderator
Primarily: Your own code. If set up properly, you already have everything you need to make it work. If you have a way to know whether the player is currently standing on solid ground and not falling - and you likely do, because that's what makes your movement code work - you have a way to know whether the current position is a safe location.
 

FrostyCat

Redemption Seeker
You should be reviewing your GML basics, in particular variables and basic events, if you don't instantly recognize what TsukaYuriko is talking about.

Create:
GML:
lastSafeX = x;
lastSafeY = y;
Step:
GML:
if (position_meeting(bbox_left, bbox_bottom+1, obj_ground_static) && position_meeting(bbox_right, bbox_bottom+1, obj_ground_static)) {
    lastSafeX = x;
    lastSafeY = y;
}
Then whenever the player dies, move it back where its lastSafeX and lastSafeY indicates.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
I have an alternative proposal... In my current game (also a platformer) I actually have "marker" objects in the room that I place at specific places. These markers have a sprite, but have their "visible" flag set to off, so they will detect collisions, but NOT be drawn. Now, in a controller object I store two values "x_spawn" and "y_spawn", and in the player object I have a collision event with the marker objects that sets these variables, like this:
GML:
// COLLISION EVENT PLAYER / MARKER
var _x = other.x;
var _y = other.y;
with (obj_Control)
{
x_spawn = _x;
y_spawn = _y;
}
NOW when the player dies, they are recreated at the position of x_spawn/y_spawn as stored in the controller object. Why am I telling you this? Well, I found that using a more dynamic method of generating a "safe" position to respawn was actually pretty complicated, especially as I added more things into the rooms! The spawn point might seem safe, but what if there is a spike above the player? Or a laser that shoots out from the other side of the room? Or a platform that moves up and down from that position? Yes, you can code in checks for ALL these things, but it seemed much more logical to simply add a few invisible objects into the rooms - as I created them in the Room Editor - at places where I KNEW there was no possibility of the player being killed. This may set the player back a couple of seconds of game time, but it's worth that to prevent the player getting frustrated as they die over and over again in a loop because of a poor auto-save position. :)
 
Last edited:
I have an alternative proposal... In my current game (also a platformer) I actually have "marker" objects in the room that I place at specific places. These markers have a sprite, but have their "visible" flag set to off, so they will detect collisions, but NOT be drawn. Now, in a controller object I store two values "x_spawn" and "y_spawn", and in the player object I have a collision event with the marker objects that sets these variables, like this:
GML:
// COLLISION EVENT PLAYER / MARKER
var _x = other.x;
var _y = other.y;
with (obj_Control)
{
x_spawn = _x;
y_spawn = _y;
}
NOW when the player dies, they are recreated at the position of x_spawn/y_spawn as stored in the controller object. Why am I telling you this? Well, I found that using a more dynamic method of generating a "safe" position to respawn was actually pretty complicated, especially as I added more things into the rooms! The spawn point might seem safe, but what if there is a spike above the player? Or a laser that shoots out from the other side of the room? Or a platform that moves up and down from that position? Yes, you can code in checks for ALL these things, but it seemed much more logical to simply add a few invisible objects into the rooms - as I created them in the Room Editor - at places where I KNEW there was no possibility of the player being killed. This may set the player back a couple of seconds of game time, but it's worth that to prevent the player getting frustrated as they die over and over again in a loop because of a poor auto-save position. :)
Interesting take! I can easily see doing a sort of checkpoint system instead with this (as this is essentially what it is), where halfway through the level you hit a checkpoint. I may opt for this instead. Thanks for bringing it up.
 
D

Deleted member 13992

Guest
I do this:

Create event
GML:
    //declare variables
    count_prev_safe = 0;
    x_prev_safe = 0;
    y_prev_safe = 0;
Step event, but only within the "on the ground" state. It's important to have this kind of state or else it won't work.
GML:
    //update last safe position
    if (count_prev_safe == 6) {    //save safe position every 6 frames
        x_prev_safe = x;
        y_prev_safe = y;
        count_prev_safe = 0;
    }
    count_prev_safe++;
Then on death, during the transition, I teleport to x_prev_safe / y_prev_safe. It works well enough for my needs (platformer), but occasionally it'll teleport me right on the edge of a collision. I may change it in the future to marker objects as Nocturne proposed. That's the "safer" approach since you can avoid situations where you respawn with half your feet hanging over an edge.

I know, I know. Everyone else uses alarms instead of what I did. I just don't like alarms.
 
Last edited by a moderator:
Top