Hi all,
I am currently making a game which has different types of small games, I would just say mini games for now, included.
Now it would be pretty nice to have the abillity to pause these games while playing. But honestly, I don't want to code a function for each game which then pauses each timer the game might use and breaks out of a loop and pauses/stops all the sounds, and then resumes all these timers again and idk. I just can't imagine there isn't a smarter way.
Block the thread somehow, stop the game from executing until, idk. Maybe someone with experience can help me.
thanks a lot!
This is a rough outline, but I'd do it somewhat like the following:
abstract class minigame {
string name;
timer@[] timers;
bool paused;
minigame(const string&in name) {
this.name = name;
this.paused = false;
}
timer@ add_timer() {
timer t;
timers.insert_last(t);
return t;
}
void pause() {
paused = !paused;
if (!paused) {
for (uint i = 0; i < timers.length(); i++) timers[i].resume();
}
else {
for (uint i = 0; i < timers.length(); i++) timers[i].pause();
}
}
void update() {}
}
class blackjack : minigame {
timer@ timeout_timer; // the user will time out after 5 seconds of inactivity just for this example.
blackjack() {
super("Blackjack");
@timeout_timer = add_timer();
}
void update() override {
if (paused) return;
if (timeout_timer.elapsed >= 5000) time_out();
}
void time_out() {
alert("Oops", "You ran out of time");
}
}
I unfortunately have to go do something in just a couple minutes so this was written in a notepad window and not tested, but it should give you a general idea. Happy to answer any questions you may have about this, design patterns like this are one of my favorite things about making games.
Hi,
The trick here is to use arrays of timers and sounds, then to have a special new_timer() function that returns a new timer and adds it to the array of them.
This is a very minimal pseudocode example, but it might look something like:
class test {
timer@ walk_timer = new_timer();
timer@ jump_timer = new_timer();
timer@[] timers;
void pause() {
for (uint i = 0; i < timers.length(); i++) timers[i].pause();
}
void resume() {
for (uint i = 0; i < timers.length(); i++) timers[i].resume();
}
timer@ new_timer() {
timer t;
timers.insert_last(t);
return t;
}
}
OMG I just saw this an hour after posting my reply lol! I'd started replying last night, then forgot about it. This morning I was impressed about how discourse saved drafts and decided to continue where I left off and just clicked post, then checked the topic an hour later and saw this. Oops?
Hey yall,
Thank you both for the examples, I'll try these solutions in the next days when I add more games and will definitly tell if I have questions