Page 1 of 2

[SOLVED] love.window.hasFocus() on Android

Posted: Wed Aug 16, 2017 6:42 pm
by ElasticSpandex
So I've made a neat little game, and I want to port it to Android.

Everything works perfectly, except for when you press the home button, or switch to another app, while the game is running.
My game "kind of" runs in the background, but not in the way it should. I have a simple timer that counts score every second, and that keeps ticking while I'm not in the game. Expected, but the wierd thing is, nothing else seems to run correctly... The player should die if he doesn't move, but bump.lua doesn't register any collisions (or maybe the code that should be run when a collision is detected doesn't).

But that's besides the point. The obvious fix would be to pause the game when the player isn't actually playing. Something like this:

Code: Select all

	if not love.window.hasFocus() then 
		if game.state == "playing" then
			game.state = "paused"
		end 
	end 
But it doesn't work... I'm assuming a desktop window isn't the same as an Android window, and therefore love.window.hasFocus() doesn't apply for Android.

I've searched a bit, but I can't find a way to test if the user in the game... It seems like a really simple thing...

Any help would be greatly appreciated :awesome:

Re: love.window.hasFocus() on Android

Posted: Wed Aug 16, 2017 7:56 pm
by slime
Does the timer use love.timer.getTime, os.time, or love.update? If one of the former two, they just query the system for the time and that system counter doesn't pause when your game is paused.

Re: love.window.hasFocus() on Android

Posted: Thu Aug 17, 2017 7:12 am
by ElasticSpandex
slime wrote: Wed Aug 16, 2017 7:56 pm Does the timer use love.timer.getTime, os.time, or love.update? If one of the former two, they just query the system for the time and that system counter doesn't pause when your game is paused.
I'm not sure you understood, my wording wasn't very good.

if game.state == "paused" then the game does actaully pause - on pc. The thing is love.window.hasFocus() doesn't return false on Android.

I'm using love.update to update the timer. Is love.update supposed to stop when the user isn't currently in the game?

All I would like to do, is test if the user is actually in the game on Android. That way i can make a "pausescreen" or something else.

Re: love.window.hasFocus() on Android

Posted: Thu Aug 17, 2017 7:51 am
by zorg
ElasticSpandex wrote: Thu Aug 17, 2017 7:12 am All I would like to do, is test if the user is actually in the game on Android. That way i can make a "pausescreen" or something else.
love.system.getOS

Re: love.window.hasFocus() on Android

Posted: Thu Aug 17, 2017 8:01 am
by Nixola
zorg wrote: Thu Aug 17, 2017 7:51 am
ElasticSpandex wrote: Thu Aug 17, 2017 7:12 am All I would like to do, is test if the user is actually in the game on Android. That way i can make a "pausescreen" or something else.
love.system.getOS
I think the focus was "in game", not "on Android"

Re: love.window.hasFocus() on Android

Posted: Thu Aug 17, 2017 8:04 am
by ElasticSpandex
zorg wrote: Thu Aug 17, 2017 7:51 am
ElasticSpandex wrote: Thu Aug 17, 2017 7:12 am All I would like to do, is test if the user is actually in the game on Android. That way i can make a "pausescreen" or something else.
love.system.getOS
Thanks for the reply!
I apologize for the bad wording, but I meant if the user is "In Game", like actaully playing, and interacting in the game, not just getting the os.
My problem is that i have no way of testing this for Android. Something like love.window.hasFocus()

Re: love.window.hasFocus() on Android

Posted: Thu Aug 17, 2017 10:17 am
by Davidobot
ElasticSpandex wrote: Thu Aug 17, 2017 8:04 am My problem is that i have no way of testing this for Android. Something like love.window.hasFocus()
Make a small program that prints a random number in an in-window console every second or so. Exit the app on android, wait a few seconds and go back into the app - if any new numbers appeared, then the game was not paused.

Re: love.window.hasFocus() on Android

Posted: Thu Aug 17, 2017 1:41 pm
by ElasticSpandex
Davidobot wrote: Thu Aug 17, 2017 10:17 am
ElasticSpandex wrote: Thu Aug 17, 2017 8:04 am My problem is that i have no way of testing this for Android. Something like love.window.hasFocus()
Make a small program that prints a random number in an in-window console every second or so. Exit the app on android, wait a few seconds and go back into the app - if any new numbers appeared, then the game was not paused.
I know the game is not paused... I mentioned above, i have a timer that keeps ticking even when you're not in game. I also went ahead and made another timer just for good mesure, same result.

Here is my love.update function :

Code: Select all

function love.update(dt) 
	if love.window.hasFocus() then 
		game:update(dt)
	end 
end
That is it. Everything is in game:update(). What I want to achieve from this is, the game effectivly pausing when the user isn't in the game. (Ie. has pressed the home button, or in another way left the game). It works on windows when you stop having the window focused (as expected). But the game keeps running when I'm on Android.


As I said in the first post, some of the code runs normally, while the rest doesn't run at all. I'm not 100% sure, but I think "if statements" are ignored? It sounds wierd, here's an example:

Code: Select all

-- This is inside game:update()
	if titleY < height/6 then 
		titleY = titleY + 170*scale*dt
	end
All this does, is make the title of the game scroll down the screen until it hits the limt. In this case it's height/6 (window height)
It should look like this:
Screenshot_20170817-151651.png
Screenshot_20170817-151651.png (5.19 KiB) Viewed 7519 times

But if I press the home button while it is still scrolling down, it will keep going until i "re enter" the app
Resulting in this:
Screenshot_20170817-151706.png
Screenshot_20170817-151706.png (4.91 KiB) Viewed 7519 times
Could this a bug with the Android port?

All this would not be a problem if love.window.hasfocus() actually returned false on Android.

Is there an alternative to love.window.hasFocus() for Android? :3

Re: love.window.hasFocus() on Android

Posted: Thu Aug 17, 2017 3:00 pm
by ElasticSpandex
Alright... I figured it out!
love.window.hasFocus() doesn't work on Android. But love.focus does.
This works:

Code: Select all

function love.focus(f)
	if not f then 
		if game.state == "playing" then 
			game.state = "paused"
		end
	end
end
I appreciate all the help from you guys! :awesome:

Although I'm still confused by the problem I addressed above ¯\_(ツ)_/¯

Re: [SOLVED] love.window.hasFocus() on Android

Posted: Thu Aug 17, 2017 8:05 pm
by Nixola
Maybe LÖVE treats it as if it was one huge update, with dt measuring the time the app wasn't focused?