The understanding of game.time variable


Hi all

I finished the stage: “Adventure Time” but it seems like Im confused by how the spawn and game.time in this stage works. There is created a variable called “SpawnTime = 0” which makes the variable equal 0. It is then used in the if statement in the while true loop:

"if game.time > spawnTime:
game.spawnXY(“munchkin”, 60, 35).

So the game spawns a munchkin if game.time has been running longer than the spawnTime variable which is set to 0 which will happen after 1 second, which make a lot of sense. And then just keep chain spawning the munchkins.

But further down in the if statement there is another variable called the same name: spawnTime = game.time + 2.

I understand the concept that every 2 seconds of game time it should spawn another munchkin.

But I don’t understand how the two variables can operate together and not cause interference ? The variable (outside the while true loop) equals 0 and the other variable equals game.time + 2 (inside the while true loop)

Thank you


The code first defines spawnTime outside of the while loop. They temporarily set it to. Then, inside the if statement, game.spawnXY will spawn a munchkin straight away, so when you start the level, there is automatically an enemy already there. Then, the next ogres spawn every 2 seconds.

tldr; First munchkin spawns immediately, then everyone following it spawns every 2 seconds


Thank you for your answer :slight_smile:

I know the concept of what we want to do in this stage, but im still confused with the while loops.

The variable “spawnTime = 0” outside the while loop is used by the while loop. Then “spawnTime = 0” is overwritten by the new variable “spawnTime = game.time + 2” ? in the loop ?

Is that correct ?


Think of the flow like this:

time 0: spawnTime = 0, game.time = 0
time 1: spawnTime = 0, game.time = 1
time 2: spawnTime = 0, game.time = 2
time 3: spawnTime = 0, game.time = 3
!!! game.time > spawnTime, so the if-statement runs and sets spawnTime = game.time + 2 (which is: 3 + 2)
time 4: spawnTime = (3 + 2), game.time = 4
time 5: spawnTime = 5, game.time = 5
time 6: spawnTime = 5, game.time = 6
!!! game.time > spawnTime, so the if-statement runs and sets spawnTime = game.time + 2

The code inside the if-statement only runs if the conditional (the part with the >) is true. So yes, it overrides the variable when the code is run, with a new time to wait until.


Every loop inside the while loop will reassign spawnTime to game.time + 2, every 2 seconds. Is this correct, @Serg?


The loop… “loops” very quick in the Game Development worlds. Let’s say 30 times every second. So, every 2 seconds, the loop has looped 60 times. It does the stuff inside the if-statement if the conditional is true, so every second the if-statement is checked 30 times. It only becomes true after 3 seconds, then performs the action inside the if-statement once, then the conditional is false once again, (until 2 seconds later)


Thank you for your answer Serg :slight_smile: