[SOLVED] love.window.hasFocus() on 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.
ElasticSpandex
Prole
Posts: 9
Joined: Sat Nov 05, 2016 4:47 pm

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

Post 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:
Last edited by ElasticSpandex on Thu Aug 17, 2017 3:00 pm, edited 1 time in total.
User avatar
slime
Solid Snayke
Posts: 3162
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: love.window.hasFocus() on Android

Post 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.
ElasticSpandex
Prole
Posts: 9
Joined: Sat Nov 05, 2016 4:47 pm

Re: love.window.hasFocus() on Android

Post 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.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: love.window.hasFocus() on Android

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: love.window.hasFocus() on Android

Post 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"
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ElasticSpandex
Prole
Posts: 9
Joined: Sat Nov 05, 2016 4:47 pm

Re: love.window.hasFocus() on Android

Post 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()
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: love.window.hasFocus() on Android

Post 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.
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
ElasticSpandex
Prole
Posts: 9
Joined: Sat Nov 05, 2016 4:47 pm

Re: love.window.hasFocus() on Android

Post 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 7414 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 7414 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
ElasticSpandex
Prole
Posts: 9
Joined: Sat Nov 05, 2016 4:47 pm

Re: love.window.hasFocus() on Android

Post 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 ¯\_(ツ)_/¯
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

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

Post by Nixola »

Maybe LÖVE treats it as if it was one huge update, with dt measuring the time the app wasn't focused?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

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