Pausing game

So in my game, i have a working pause menu which appears when you press ‘p’ however the actual game itself in the background is still running. Could someone please suggest improvements to my code to fix this so when i press ‘p’ not only does the pause menu appear, but the actual game itself pauses. Here is my code for my main.gui.script and my pausemenu.gui.script:

What is not working right now? I see that you are setting the time step to 0 which will pause the game. That should work.

it just doesnt pause, when i press p. the only thing that happens is the pause menu appears but the game in the background is still moving. I could send over a zip folder of my project ?

Let’s try to figure this out without the project.

One thing that I see now is that you send a set_time_step message with mode set to 1:

{ factor = 0, mode = 1 }

I believe the mode should be 0.

It still doesnt work.


How are you moving stuff in your game? Using go.animate()? Or are you moving stuff programmatically in update() (if that is the case are you also using delta time in all movement calculations?)

By the way, there is a set_time_step example here:

Im using update().

Ive seen the example but im still very confused

Ok, are you using the delta time for all calculations? The update() function will still be called but the dt will be 0 when you’ve set the time step factor to 0. If you are using the delta time properly things should just work. If you ignore dt then everything will keep running.

Maybe you can share your update() here?

Sure, here are my update() functions :


If you need I can send the zip file of my project.

It looks like you are applying the delta time in most places. Remember that the update() function will be called even when you pause!

Ah ok so what can i do to fix it

What I told you already. If you move stuff make sure to apply delta time. Example:

function update(self, dt)
    -- this will move the position 10 pixels regardless of delta time
    -- do not do like this
    local pos = go.get_position()
    pos.x = pos.x + 10

    -- always multiply any movement with 
    pos.x = pos.x + 100 *dt

You can also check if dt is 0 and then do an early exit:

function update(self, dt)
    -- do an early exit if dt is 0
   if dt == 0 then return end

   -- run your code here

Sorry im new to defold i really dont understand what i need to change

i thought i was already applying delta time in my scripts?

Oh actually i think i kind of understand let me try

My scripts are already mutiplying by delta time like you said tho and it still doesnt pause when i press p

Could you please zip your project (exclude build and .internal fodlers) and upload it here?

Hey, i couldnt send it on here as my project was too large. I sent you a message on discord

My username is fxn