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
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!
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
let me check if I understood you correctly:
does it make some sense to write it like this?
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"?
sorry for my basic (and probably boring) questions, as soon as I learn enough to "walk alone" I'll certainly ask something more interesting...
thank you again