Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
how to get "not regular" timer based events? — Gideros Forum

how to get "not regular" timer based events?

piepie Member
edited February 2013 in General questions
Hi,
I have a table containing some time values ( t={[1] = time1, [2]=time2, [3]=time3...[i]=timei} )

These are some sort of "time marks" in milliseconds, they increase as their index increase, and they have unique and different values each time the app starts. for example it could be:

t={[1] = 32, [2]=156, [3]=1378, [4]=2560}
or
t={[1] = 122, [2]=623, [3]=1358, [4]=3560, [5]=3563}
values are random, but the next one is always bigger than the previous one.


I'd like to call a function everytime a timer (which could be a timer class, or os.timer()) reach one of these time values, until there are no more available.

I made some tests, but unfortunately I wasn't able to get a working solution:
what's my best option on your opinion?

If I check onEnterframe if os.timer() == each of my table values, I enter an infinite loop, since it's really difficult that os.timer() "falls" on any of the values inside t. (I tried printing those values: that happens either using math.floor).

Maybe i could use Timer class.
Is it possible to set up different timers? Something like Timer.new(time[i], 1)?

Ayway, I'd prefer to use os.timer() since reading on the forum it seems that timer class is messy... while os.timer should be more precise on any device... :) I don't know, I am just wondering..
the first and almost only requirement is being able to process myfunction everytime "timer" gets to "time".

If I can use eventdispatcher, could someone please provide an example applicable to time?

Thank you in advance,
kind regards :)

Comments

  • atilimatilim Maintainer
    Accepted Answer
    I would use a single ENTER_FRAME event and check os.timer() * 1000 > table values (instead of ==). Here is a simple code:
    local t = {122, 623, 1358, 3560, 3563}
    local i = 1
    local start = os.timer()
     
    local function onEnterFrame()
    	local msec = (os.timer() - start) * 1000
    	if i <= #t and msec > t[i] then
    		print(t[i])
    		i = i + 1
    	end
    end
     
    stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
  • piepie Member
    Thank you @atlim
    :)

    It's not that much different from my code, but this works! :)
    this is a lesson of logic flow to me.

    I still have a couple of questions about coding in general, from my begginer point of view:

    1) about your choice to use #t directly in the conditional statement:
    could this be considered a "best practice"?
    I used the same syntax, but in a new variable (declared previously).
    Is it better to keep the number of variables low for better performance?

    2) about the use of "local": when should I use it?
    is there a general rule/ reason to avoid or use this before variables and functions?

    Thank you very much, have a wonderful day!
  • ar2rsawseenar2rsawseen Maintainer
    1) probably in this case it would be better to safe the size of table in a separate variable (of course if table size does not change). But you won't see the difference with such a small table

    2) you define local variable when you want to exist only in this specific scope.
    But in @atilim example, i is defined outside onEnterFrame event for simple reason, creating new variable consumes resources, and as onEnterFrame is calles so many times, it would be efficient to create one local variable outside the scope and reuse it in the event again and again. Usually you would define it as a property of a class where event method exists :)
  • piepie Member
    Thank you very much for your explanation:
    let me check if I understood you correctly: :)

    does it make some sense to write it like this?
    function scene:init()
    self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame)
    self.start = os.timer()
    self.i = 1
    end
     
     
    function scene:onEnterFrame()
    if --blah blah?
      end
    end
    So in this specific example "local" and "self." have the same meaning or there is something else?

    I'm not sure if I got the definition of "scope", please correct me if I am wrong:
    scope is almost "everything", i.e. from "function scene:init()" to "end", but it could also be "global" as "the space" outside each function.

    Each scope is a separate entity where local variables are valid only "locally", BUT, if variables are set as propery of a class (like self.i) could be accessed from any method of the same function (init, EnterFrame in this example)?
    Could a property be "local"?
    function scene:init()
    self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame)
    local self.start = os.timer()
    local self.i = 1
    end

    sorry for my basic (and probably boring) questions, as soon as I learn enough to "walk alone" I'll certainly ask something more interesting... :D

    thank you again
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    you understood the concept of local and self correctly and no self.propety can not be local, because self is instance (object available accross all methods) and by defining local self you will probably redefine internal reference to instance for that specific method ;)
Sign In or Register to comment.