Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
timer question/problem — Gideros Forum

timer question/problem

keszeghkeszegh Member
edited July 2016 in General questions
I've a problem that i'm calling a timer at 1000/_fps time diffs, which should be equivalent to _fps frames per second, as i understand. but for _fps=60 it's actually 40 fps at the end.

do i make an error? if not, it's an error in gideros? thanks.

below is the code to test, which prints something like this:
timer real fps: 38.710426726566
enterframe real fps: 59.479934009709
const=120
 
frameCount=0
currentTimer=0
 
function onTimer()		    
  --fps counter:    
  frameCount = frameCount + 1
	if frameCount == const then
		local timer = os.timer()
		local deltaTime = timer - currentTimer
		currentTimer = timer
		local fps_test=const/deltaTime		
		print ("timer real fps: ".. fps_test)		
                frameCount = 0
	end	
end
 
frameCount2=0
currentTimer2=0
 
function onEnterFrame()		    
  --fps counter:    
  frameCount2 = frameCount2 + 1
	if frameCount2 == const then
		local timer = os.timer()
		local deltaTime = timer - currentTimer2
		currentTimer2 = timer
		local fps_test=const/deltaTime		
		print ("enterframe real fps: ".. fps_test)		
                frameCount2 = 0
	end	
end
 
_fps=60
 
timer=Timer.new(1000/_fps)
timer:addEventListener(Event.TIMER, onTimer,self)
 
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame,self)
 
timer:start()

Comments

  • hgy29hgy29 Maintainer
    I think it is because whatever period you ask for a timer, gideros always call your timer at a frame boundary, probably in the next frame following the timer expiration. So due to frame to frame jitter, it will call your timer routine sometimes every frame and sometimes every two frames, so you end up seeing between 30 and 60 calls by second
  • keszeghkeszegh Member
    edited July 2016
    @hgy29, thanks for the answer. i thought it would be more logical for the timer to accumulate unprocessed calls and being called twice per frame if necessary to keep on time. if it works like you claim then it's quite useless. e.g. in the above case instead of 60 fps it becomes 40 fps which is a huge difference.
    it looks i will just have to use enterframe then and write my own 'timer' running within this enterframe and using os.timer to time the calls.
  • SinisterSoftSinisterSoft Maintainer
    It could also be that your device is using energy saving mode and lowering the frame rate. Try turning off power saving on the phone and see if that makes a difference.
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • hgy29hgy29 Maintainer
    edited July 2016 Accepted Answer
    @keszegh, I was just guessing, but I was wrong. Reading the code it looks like it should behave like you said, calling the event multiple times in the same frame if some of them were missed.
    There is probably a bug somewhere in gideros though since I can't find any wrong in your Lua code.

    EDIT: looking better at the code, it looks like everything was made so that it would behave as you said, but a tiny bug makes it behave as I said in the end! We'll have it fixed

    Likes: keszegh, antix

    +1 -1 (+2 / -0 )Share on Facebook
  • keszeghkeszegh Member
    edited July 2016
    @SinisterSoft, i checked some of these things, but i was using windows gideros player for testing and also when i've set the timer to 1000/80 then it reached 60 fps (but did not get bigger), so it was more or less obvious that gideros does handle timers like @hgy29 said. i'm glad that it will be changed to the other way, which seems to be much more useful, as it calls the function on average in the given time intervals.
  • and thanks for looking into it, @hgy29, it's very good that you could correct this bug.
Sign In or Register to comment.