Page 1 of 1

Is there any best way to create pause game feature??

Posted: Sun Aug 07, 2022 3:38 am
by tommy1991111
Hello everyone,
Is there any simple way to create pause game feature??

my way:

Code: Select all

function love.load()
    pause = false
    number = 0
end

function love.update(dt)
    function love.keypressed(key, unicode)
		if key == 'p' then pause = not pause end
	end
    if pause == false then
    --    do something when the game is paused
       number = number + 1
    end  
end

function love.draw()
    if pause then love.graphics.print("Game is paused",0,0) else 
    love.graphics.print("Game is running",0,0) end
    love.graphics.print(number,400,300)
end
when my game has more object
I need to add more

Code: Select all

 if pause then
Is there any best way to create pause game feature??
thank you very much

Re: Is there any best way to create pause game feature??

Posted: Mon Aug 08, 2022 5:38 pm
by milon
If you don't want a lot of if/else structuring, then it's time to start implementing state machines.

Also, why is love.keypressed() defined inside of love.update()? It really should be its own separate function IMO.

Re: Is there any best way to create pause game feature??

Posted: Mon Aug 08, 2022 10:28 pm
by BrotSagtMist
To intensify the amount of error here:
You are creating the function 100 times per second but it ins not even used once.
Any usage would be outside of this function :D
This is a fixed version:

Code: Select all

function love.load()
 function love.keypressed(key, unicode)
  if key == 'p' then pause = not pause end
 end
end

function love.update(dt)
    if not pause  then
    --    do something when the game is paused
    end  
end
But as said in another thread yesterday, if cases that are looped 100 times a second just for the sake of pause is still pause is a bit messy. Personally i prefer the renaming scheme:

Code: Select all

 function love.keypressed(key, unicode)
  if key == 'p' then love.update,switch = switch,love.update  end
 end
function run(dt)
-- game
end
  function pause(dt)
  --pause screen  
end
love.update=run
switch=pause
love.update starts() out being identical to run() and switch() is pause(), now whenever p is pressed they do a switchteroo.
There are many more ways to make a pause screen tho.
If there are no other states you can even be pretty slim profile:

Code: Select all

 function love.keypressed(key, unicode)
  if key == 'p' then love.update = love.update and nil or run   end
 end 
 
Since update actually moves numbers but not doing the render this solution freezes the screen _looking_ just perfectly.

For advanced consideration if you are into perfection: This methods pause the movement of objects on the screen, but you are still rendering them new every frame so this pause screen will still take cpu/gpu. Ideally one would want to freeze the entire game loop by either go into a dummy loop with less FPS or halt everything with love.event.wait().
That is not something expected from a hobby game tho.