The game paused but the time keeps running (Android)

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
HungerBoatz
Prole
Posts: 11
Joined: Tue Dec 19, 2017 3:08 pm

The game paused but the time keeps running (Android)

Post by HungerBoatz »

So I'm trying to make the game paused when player is in not focus.

For example:

Code: Select all

-- in my update callback
function love.update(dt)
    	if paused == true then return
    	else
     		   -- run the code ..
        
    	 	   -- and somewhere in my update function, I also have
    		if start_time == true then
     			time = time + dt
     		else
     			time = time + 0
     		end
 	end
end

-- in my focus callback
function love.focus(f)
	if not f then
		paused = true
		start_time = false
	else
		paused = false
		start_time = true
	end
end
And the result, my game successfully stop running and starts freezing when it gets paused. But when I'm trying to print the variable 'time' in the screen, the time keeps running and not freezing even the game paused.

Also when I press home button or back button, the time stills running in the game.

Please help :)
User avatar
pgimeno
Party member
Posts: 3713
Joined: Sun Oct 18, 2015 2:58 pm

Re: The game paused but the time keeps running (Android)

Post by pgimeno »

There must be something else we don't know. At first sight that should stop counting the time. Have you tried printing start_time and paused?
HungerBoatz
Prole
Posts: 11
Joined: Tue Dec 19, 2017 3:08 pm

Re: The game paused but the time keeps running (Android)

Post by HungerBoatz »

pgimeno wrote: Mon Jul 01, 2019 10:42 am There must be something else we don't know. At first sight that should stop counting the time. Have you tried printing start_time and paused?
Yes.The start_time printed true and the paused printed false in the screen. Then when I want to slide down my notification bar and put it back again, both start_time and paused still prints the same value. But I don't have idea how to see their value when I'm not in focus, also in my IDE I dont have something like console to print() it in. Anyway, the timer changed so far when I'm back to the screen after not focusing. For example, the timer is in 2 seconds first, then after not focusing in some seconds and back again, it turned to 10 seconds or maybe 12 or 13 etc. not continuing the last second (which is 2).
User avatar
pgimeno
Party member
Posts: 3713
Joined: Sun Oct 18, 2015 2:58 pm

Re: The game paused but the time keeps running (Android)

Post by pgimeno »

Can you reproduce it with a very simple program that contains just the above code and little else?

If not, the problem is elsewhere, and if you want us to help, we'll need to see it, because no one can guess what the rest of the code is doing.
User avatar
slime
Solid Snayke
Posts: 3181
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: The game paused but the time keeps running (Android)

Post by slime »

The first dt after resuming might be very large, since it's based on the real time that elapsed since the last frame.
HungerBoatz
Prole
Posts: 11
Joined: Tue Dec 19, 2017 3:08 pm

Re: The game paused but the time keeps running (Android)

Post by HungerBoatz »

Here what I mean:

Code: Select all

function love.load()
	time = 0
	time_starts = true
	
	text = ""
	
	x = 0
	starts_moving = true
end

function love.update(dt)
	if time_starts == true then
		time = time + dt
	else
		time = time + 0
	end
	
	if starts_moving == true then
		x = x + 1
		if x > love.graphics.getWidth() then
			x = 0
		end
	else
		x = x + 0
	end
end

function love.draw()
	love.graphics.print(time)
	love.graphics.print(text, 0, 100)
	love.graphics.rectangle("fill", x, 20, 60, 60)
	love.graphics.print('x: ' .. x, 0, 200)
end

function love.focus(f)
	if not f then
		time_starts = false
		starts_moving = false
		text = "uh-oh why the timer didn't stops when not focusing?"
	else
		starts_moving = true
		time_starts = true
	end
end
User avatar
pgimeno
Party member
Posts: 3713
Joined: Sun Oct 18, 2015 2:58 pm

Re: The game paused but the time keeps running (Android)

Post by pgimeno »

So, in light of what slime has noted, the easiest fix would be to limit dt to, say, 0.1s. That would advance the clock by at most 0.1 seconds during a pause.

If that's still not desirable, well, if the love.focus() event executes, it can set a flag that would tell the love.update() event to skip that frame.
sphyrth
Party member
Posts: 261
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: The game paused but the time keeps running (Android)

Post by sphyrth »

Bummer. I can't participate when the error can't be reproduced on a desktop.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 5 guests