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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
tommy1991111
Prole
Posts: 1
Joined: Thu Jun 07, 2018 1:48 pm

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

Post 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
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

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

Post 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.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
BrotSagtMist
Party member
Posts: 607
Joined: Fri Aug 06, 2021 10:30 pm

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

Post 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.
obey
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 50 guests