GML Checkpoints (safepoint) in a 2D platformer game

M

MrGreenHill7

Guest
Hello,
I am farely new to this program and i am working on a 2D platformer in gamemaker 8.0, but i ran in to a problem with checkpoints.
I currently have it all working that i an object called "checkpoint_deactivated" and an object "checkpoint_activated". As soon as the character hits one of the checkpoints, it will turn all other checkpoints into "checkpoint_deactivated" and the checkpoint it hits will get replaced with "checkpoint_activated". This way there will only be one activated checkpoint at a time. Now for my question, lets say the character dies to an enemy, how can i get the character to spawn at "checkpoint active"?
thanks in advance,

MGH7
 

Alice

Darts addict
Forum Staff
Moderator
If you always have at most one instance of checkpoint_activate at a time, you can do the following:
GML:
with (checkpoint_activated) {
    instance_create(x, y, obj_Player);
    // or, in GameMaker: Studio, you would use instance_create_depth(...)
    // or instance_create_layer(...), or whatever spawns player object
}
You want to tweak this a bit, for example:
- add or substract some x/y offset to position the player properly
- create some kind of player spawning animation along the way (e.g. fade-in of player sprite along with particle effects)
- handle a situation when the character dies before activating any checkpoint, or prevent it by having a checkpoint at the start of level

At any rate, the general gist of accessing the current active checkpoint is with (checkpoint_activated) {...}, as long as exactly one active checkpoint is present in the room.
(returning to checkpoints in different rooms, like e.g. in a Metroidvania, might be much trickier and involve some global variables manipulation, but you didn't mention having a case like that)
 
Last edited:

FrostyCat

Redemption Seeker
What made you use a decade-old version of GM if you're new? GM 8.0 has a known crash on Windows 10 and 8 in its audio system, and community support for it has almost entirely withered away. For example, the code that Alice gave is for the current version of GM (GMS 2.3). On GM 8.0, you would use this instead:
GML:
with (checkpoint_activated) {
    instance_create(x, y, obj_Player);
}
Unless this is an assignment from a cash-strapped school that's still using decade-old equipment, you should use something more recent instead.
 

curato

Member
The above code is assuming you have a variable with the id of the active checkpoint stored in checkpoint active rather than an on/off in each object. It would be cleaner to hit the checkpoint and assign that id to a global variable (or other accessible) then code like is suggested would create a player object at the x and y of the referenced checkpoint.
 

Alice

Darts addict
Forum Staff
Moderator
The above code is assuming you have a variable with the id of the active checkpoint stored in checkpoint active rather than an on/off in each object. It would be cleaner to hit the checkpoint and assign that id to a global variable (or other accessible) then code like is suggested would create a player object at the x and y of the referenced checkpoint.
Actually, according to OP "checkpoint_activated" and "checkpoint_deactivated" are two different objects rather than an on/off variable. This is also why I stressed the importance of exactly one instance of "checkpoint_activated" being in a room when returning the character to checkpoint; otherwise, with checkpoint_activated being an object, the "with" statement would go through each instance producing multiple character instances.

@FrostyCat Good point about GM8 version, I completely forgot back in the GM8 days instance_create_layer/depth wasn't a thing. I fixed the code in my post, while still addressing how it's done in GM:S.
 

curato

Member
Yes, I see what you mean there. I wasn't reading it correctly. I still think it would be solid to just have a pointer variable point to the correct active check point and ditch the with statement all together because if you screw up somewhere and end up with more than one checkpoint_activated object in you level you are going to hard to troubleshoot error.
 
Top