Input lag on iPhone 4

Monkey Targets Forums/iOS/Input lag on iPhone 4

Grey Alien(Posted 2014) [#1]
Hi all, I've recently been testing my game on a variety of iDevices and discovered a weird problem.

I'm using SetUpdateRate 60 and on iPhone 4S and iPad 2 and higher the game runs at 60FPS and the slider control I've made to move the player left/right is super-smooth.

On iPhone 4 the framerate is also 60FPS but with the occasional dropped frame when a lot is going on, which is to be expected.

However, something weird is happening to the input. Whenever the action hots up the input becomes increasingly laggy and then even when the FPS returns to normal the input *remains* laggy! By laggy I mean that as you slide your finger across this control I've made the player (ship) moves in large chunks maybe only about 4-5 times a second instead of 60 times a second. It only reverts to normal when I start a new level (a shop screen is shown in between).

I can understand why the input might be laggy while the FPS is reduced but input should return to normal when the FPS returns to normal. For example, I can play a level with lots of aliens and shoot them all except one then that last alien and all my bullets are running at 60FPS but the input has dropped to like 4FPS.

I tried an experiment where I used SetUpdateRate 30 and this seemed to solve the problem, but obviously it didn't look very smooth. So I reverted back to SetUpdateRate 60 and modified my slider input code to only be called every other frame (so at 30FPS) but that didn't work. Perhaps I'd need to make ALL my input code (for other buttons etc on screen) also run at 30FPS (every other frame) so that the input system isn't overloaded, but that might not work if Monkey is still polling the input system at 60FPS.

Maybe I need to just speed up my game so that it never drops frames and then the input rate won't drop down and stay low.

So has anyone else encountered anything like this before? It's it a Monkey issue or an iDevice issue I wonder?

Any thoughts welcome, thanks.


Nobuyuki(Posted 2014) [#2]
Render thread has real-time priority on ios so maybe the system is just choking?


Grey Alien(Posted 2014) [#3]
I just noticed this on my iPhone 4S too. This only occurred since I upgraded to XCode 5 and Monkey 077f, although the game code has changed too (but not the input code). May try to downgrade Monkey to see if it goes away.


rIKmAN(Posted 2014) [#4]
If you can get it onto TestFlight or similar I will give it a spin on my iPod 4G and report back?

I am using Monkey76d, XCode5 etc and have never had any input lag issues, but have only tested my game on iPod 4Gs and iPad 2/3/4, sp maybe it is an iPhone specific issue?

Does your input loop just use standard Monkey Touch() commands?


Grey Alien(Posted 2014) [#5]
Yep it uses standard Monkey Touch() commands. I actually have an iPod 4G too but haven't got round to testing on it yet. As it only has 256Mb RAM and is quite slow I was considering trying to filter it out by making the game iOS 7 only.


rIKmAN(Posted 2014) [#6]
What are you iOS versions are you setting as the target in XCode when building, just iOS7?

Have you run it through Instruments to see if there are any memory leaks?
I know you said it fixes itself after you reload a new level - are you resetting anything in this time that may be resetting the leak (so to speak)?

Just thinking out loud, I know you are an experienced coder so sorry if I'm teaching you to suck eggs.

PS. You are Jake, right?
I watched a talk on indie dev the other day with you on the panel with a couple of other people, was really interesting, so thanks. :)


Grey Alien(Posted 2014) [#7]
Actually I haven't set a target in XCode yet so it's whatever the default is for Monkey builds.
Yep done a lot of testing on memory leaks including running it through instruments in multiple ways.

This used to work fine and I haven't changed the input code so I'm thinking that it's like Nobuyuki said and the input thread is getting starved and somehow throttled down. It returns to normal if I pause the game and resume, or just stop pressing my slider control, or beat the level. So it feels like once there's been no input for a short time then the input becomes unthrottled again.

I should add that this lag doesn't exist on the PC version.

No worries, I appreciate the help. I am experienced but I'm not an expert, especially on iOS. So there are still many things I could easily overlook.

Yep I'm Jake! I guess that was the panel I did in 2011 in Seattle at Casual Connect? Been on some other ones but I'm not sure they are online.


Grey Alien(Posted 2014) [#8]
Still doing research and asking around. People seem to think the input thread is being starved (some people said dropping FPS to 30 worked, but that's not an option for this game) and are asking if there's a way to reduce the priority of the update thread, OR do some kind of sleep on the main thread (I guess so the input thread can catch up and process stuff).

Is there a Sleep command in Monkey? Couldn't find anything obvious.


skid(Posted 2014) [#9]
Looking at the target source, SetUpdateRate 0 will use DisplayLink which is best practise on iOS for syncing to display. Not sure if this will affect your update lag problem.


Grey Alien(Posted 2014) [#10]
OK I'll try that thanks skid. The game is relying on a 60Hz update loop for logic/animations though. Will that be affected? I'm not using delta timing/tweening or anything at the moment.