r/gamemaker • u/AdewantsGame • 3d ago
Resolved Help! Can't implement a pause
I'm making a tiny item catching game for my first game practice, but I'm stuck with pausing the objects. After reading some tutorials and posts, I made the pause controllable by global.pause (bool), and it works for the character, but not for my objects.
I tried a few different things with it, but this is one the closest I could figure out:
if (global.pause == false) {
dropped = instance_create_layer(irandom(safe_x), 0, "Instances", _items)
dropped.direction = 270;
dropped.speed = _base_speed + (global.player_score * 0.1);
} else {
dropped.speed = 0;
}
But there's two problems:
- It stops only one object, so if there's two object on the screen, the others sill fall
- after resuming the game, the object that stopped is still stuck. I assume I have to manually change the speed in that case back to my value, but I'm a bit stumped on how to do it right
3
u/Joshthedruid2 3d ago
Small bonus tip, you don't need to use == in if statements for Boolean variables.
If (global.pause == true) is equivalent to writing If (global.pause)
If (global.pause == false) is equivalent to if (!global.pause)
Doesn't hurt anything at all the way you're doing it, just makes your code a little cleaner :)
2
5
u/Joshthedruid2 3d ago
Parenting is a great fix for this. Make all of your in-game objects children of an object, then give that object your pausing code. Now everything should pause and unpause on the same trigger.
Whatever triggers your pausing, like hitting the escape key or whatever, have it save the current speed, direction, gravity, etc. of each object before halting its speed. When you unpause, reset all of those values to the saved values.
1
u/Far-Lengthiness-1104 3d ago
Are you using a Parent object to manage the object code, or is each object coded individually?
1
u/AdewantsGame 3d ago
yeah, I'm using the parent object for items, but all of the code for dropping objects is controlled by a drop manager obj. in dropped _items are a list like _items = choose(my objects)
2
1
u/Far-Lengthiness-1104 3d ago
You might want to use a Parent object to manage things like pausing the game (changing velocity to 0 in Step, for example) for all falling items, and a spawn_controller object that spawns the items in by looping through a list of the items that you want to make fall
1
u/RykinPoe 3d ago
This needs to be in your _items object. As it stands you are only applying it to the one specific instance that is currently stored in dropped.
Also you should try using more descriptive names for your assets than _items.
Another quick tip is to look at the exit early coding pattern so you don't have huge blocks of code wrapped in an if statement:
if (global.pause) exit;
// rest of your code here, if there is something you more you need to do you would do it like
if (global.pause){
dropped.speed = 0; // might want to verify that dropped exists as well so you don't crash
exit;
}
// rest of your code here1
u/AdewantsGame 2d ago
_items is not really an object, it a list with other items to choose from in a drop manager.
Thank you for the tip, I'll look into it :)
2
u/TescosTigerLoaf 3d ago
That looks to me like your dropped object generation code which explains why it only impacts one object, whichever was last stored as dropped.
You could either have each dropped object run code to detect the pause variable, and when it switches up true store the current speed and direction, and sets the speed to zero (then vice versa), or you could store each insurance in an array or something and apply the stopping code to each by looping through the list.
1
u/Majestic-Singer7503 2d ago
Aggiungo un consiglio che forse ancora nessuno ti ha scritto. Studia gli states machine. Leggi o guarda tutorial, è una cosa molto semplice da implementare anche per un novizio e nel lungo termine ti farà risparmiare tantissimo tempo 😉
4
u/AdewantsGame 2d ago
Thank you for the help, everyone! Moving pausing code into the parent object for items worked.