|
Raine
|
 |
« on: June 22, 2008, 05:22:12 PM » |
|
This might sound silly, but I was wondering how you would manage timed elements.
An example would be a spell which has a duration; would you store the end time and keep on checking against it every n frames? I've been thinking about this and I don't find another solution at the moment.
How would you go about this? Is there any particular method or...?
Thanks!...
|
|
|
|
|
Logged
|
|
|
|
|
newborn
|
 |
« Reply #1 on: June 22, 2008, 08:37:17 PM » |
|
I've always been using something like this:
time += engine.getelapsedtime if time > timeMax then time = 0 do_something end if
|
|
|
|
|
Logged
|
|
|
|
|
Raine
|
 |
« Reply #2 on: June 22, 2008, 10:53:08 PM » |
|
well.. there should be many timers, so it would be iterating through a collection all the time, and that's my main concern
|
|
|
|
|
Logged
|
|
|
|
|
Zaknafein
|
 |
« Reply #3 on: June 22, 2008, 10:59:01 PM » |
|
Here's how I handle it in XNA... maybe you can use ideas. But it basically comes down to what you and/or newborn propose. Just a more "one-component-per-event" approach.
XNA components have an Update method and Render method, the Render should do strictly rendering related stuff and Update should handle game logic. The Update method is passed the time since the game started (among other stuff). I mention this because you say "every 'n' frames", game logic shouldn't be rendering dependent. If you keep rendering at a locked framerate, you may want to update the logic in the free time, more or less often, to keep that framerate.
So an event has starting conditions. On every update it tests all conditions (starting by the broadest to test as little stuff as possible), and if it works, record the starting time. And flag the event as "running".
Then if the action is currently running, use the difference between the current game time and the recorded one to do time-based stuff. If that difference is bigger than the event duration, flag it as "not running" and exit.
Edit : About the many-timers, well it's one "if" per running event. Is that so bad?
|
|
|
|
|
Logged
|
|
|
|
|
Raine
|
 |
« Reply #4 on: June 23, 2008, 05:45:31 PM » |
|
Hi guys, sorry about the delay but I've been busy all day long. I'm not sure it's a real issue, it's more a "is there a better way?" question. I'll take time to read carefully your reply Zak, and then I'll post mine.
Thanks!
|
|
|
|
|
Logged
|
|
|
|
|
beyonder
|
 |
« Reply #5 on: June 23, 2008, 06:01:10 PM » |
|
This has been running around my head too lately...
I've been thinking about a master timer function. Sometimes like this.
TimerFunc FunctionA, Interval, VarA, VarB, VarC
Its a function that you pass a function to plus its variables... then you can use one timer to handle everything.
Err... can you pass a function to a function tho?
|
|
|
|
|
Logged
|
|
|
|
ZaPPZion
Community Member

Posts: 328
|
 |
« Reply #6 on: June 24, 2008, 01:53:20 AM » |
|
you can in .NET languages. I'm not totally sure how it's done, but I've seen it. Maybe google knows an answer.
|
|
|
|
|
Logged
|
|
|
|
|