Timers

edited January 2010 in General discussions
I was looking for a way to create timers (i.e. do this in x seconds) in orx. I can't find a easy way to do it via the API, but I assume this is done somehow internally (since objects have can have a lifetime)? Is there a main clock that keeps track of it or do each object have their own clock?
Right now I cheat a bit and do this myself with FXs, so if I want an object to be slowed down a while, for example, I add an empty FX named Slow. I was just curious if there was any better ways of doing it :).

Comments

  • edited January 2010
    There's no one liner to add a timer right now. I kept wanting to add this and worked on something else instead. I'll try to add this next week.

    However, there's a main clock (the core one), that can be found like this:
    orxCLOCK *pstClock = orxClock_FindFirst(orx2F(-1.0f), orxCLOCK_TYPE_CORE);
    

    When you have this clock, you can get its info (orxClock_GetInfo()). The info structure is public and contains the DT, the DT modifier and the elapsed time since its creation.

    Now the objects are updated using the main clock by default but we can associate a clock to an object now.

    Either in config (Clock = MyClock + look at CreationTemplate.ini to see how to declare a clock) or at runtime with orxObject_SetClock(). If you look at MushroomStew's code (aka ProjectX), I use this to slow down objects.

    Slowing down objects with clocks is better than with FX as it'll modify all the aspect of the object (speed, sound pitch, etc).

    Hope this helps! :)
  • edited January 2010
    Thanks for the reply, I'll look closer at MushroomStews code. But there's no hurry to add timers for my sake.
    The reason I used FXs is that I wanted an easy way to add several "timed properties" on an object and to keep track of them. For example, in my tower defence game I want to be able to make the enemies move slower but also something that increases damage taken.
  • edited January 2010
    And here they are!

    If you sync orx, you'll find timer handling functions.
    You can add timers on any clock you want, or if you want to add them on the main clock, just use the *Global* version of the functions.

    You have orxClock_AddTimer()/orxClock_RemoveTimer. You have to give a strictly positive delay (used between 2 calls of the timer) and you can specify a repetition number. If you give -1, the timer will be called infinitely.
    When removing a timer, you have to give the associated callback function and an optional delay. If the delay is -1, any timer matching the callback function will be removed.
    If the delay is strictly positive, only the timer matching both the delay and the callback will be removed.

    If you have any comments/suggestions regarding the timers, please let me know! :D
  • edited January 2010
    Wow, that was fast :).
    I'm downloading the source right now. I guess it's been a while since I updated so there's a lot to download.
    I'll get back once I've tried it out.
  • edited January 2010
    I've had some strange errors since I updated my orx libaries. Everything seems to compile and link ok. I've debugged a bit but it really puzzles me.

    The following is logged in the debug log (I hope it's readable):
    [2010-01-17 19:22:30] <OBJECT> (orxStructure_Register() - c:erikprogrammeringorxorxdev-msvs2008	runkorxcodesrcobjectorxstructure.c:305) Structure is already registered.
    [2010-01-17 19:22:30] <OBJECT> (orxStructure_Register() - c:erikprogrammeringorxorxdev-msvs2008	runkorxcodesrcobjectorxstructure.c:305) Structure is already registered.
    [2010-01-17 19:22:30] <DISPLAY> (orxGraphic_SetData() - c:erikprogrammeringorxorxdev-msvs2008	runkorxcodesrcdisplayorxgraphic.c:691) Data given is not a texture.
    [2010-01-17 19:22:30] <DISPLAY> (orxGraphic_CreateFromConfig() - c:erikprogrammeringorxorxdev-msvs2008	runkorxcodesrcdisplayorxgraphic.c:382) Couldn't link text (ScoreText) data to graphic (ScoreGraphic).
    [2010-01-17 19:22:30] <DISPLAY> (orxText_Delete() - f:erikprogrammeringorxorxdev-msvs2008	runkorxcodesrcdisplayorxtext.c:469) Tried to delete text object when it was still referenced.
    [2010-01-17 19:22:30] <DISPLAY> (orxGraphic_SetData() - c:erikprogrammeringorxorxdev-msvs2008	runkorxcodesrcdisplayorxgraphic.c:691) Data given is not a texture.
    [2010-01-17 19:22:30] <DISPLAY> (orxGraphic_CreateFromConfig() - c:erikprogrammeringorxorxdev-msvs2008	runkorxcodesrcdisplayorxgraphic.c:382) Couldn't link text (HealthText) data to graphic (HealthGraphic).
    [2010-01-17 19:22:30] <DISPLAY> (orxText_Delete() - f:erikprogrammeringorxorxdev-msvs2008	runkorxcodesrcdisplayorxtext.c:469) Tried to delete text object when it was still referenced.
    [2010-01-17 19:22:30] <DISPLAY> (orxGraphic_SetData() - c:erikprogrammeringorxorxdev-msvs2008	runkorxcodesrcdisplayorxgraphic.c:691) Data given is not a texture.
    [2010-01-17 19:22:30] <DISPLAY> (orxGraphic_CreateFromConfig() - c:erikprogrammeringorxorxdev-msvs2008	runkorxcodesrcdisplayorxgraphic.c:382) Couldn't link text (HealthText) data to graphic (HealthGraphic).
    [2010-01-17 19:22:30] <DISPLAY> (orxText_Delete() - f:erikprogrammeringorxorxdev-msvs2008	runkorxcodesrcdisplayorxtext.c:469) Tried to delete text object when it was still referenced.
    [2010-01-17 19:22:41] <ASSERT> (orxFXPointer_Create() - f:erikprogrammeringorxorxdev-msvs2008	runkorxcodesrc
    enderorxfxpointer.c:344) [Assertion failed] : <sstFXPointer.u32Flags & orxFXPOINTER_KU32_STATIC_FLAG_READY>
    

    The following is in my ini-files:
    [ScoreObject]
    Position = {-0.49,-0.4,0.1}
    ParentCamera = Camera
    Graphic = ScoreGraphic
    
    [ScoreGraphic]
    Text = ScoreText
    
    [ScoreText]
    String = 0
    
    [Bump]
    SlotList = ColorFlash
    Loop = false
    KeepInCache = true
    
    [ColorFlash]
    Type        = color
    StartTime   = 0.0
    EndTime     = 0.05
    Curve       = sine
    Absolute    = true
    StartValue  = (255, 255, 255)
    EndValue    = (255, 0, 0)
    

    It's adding the "Bump" fx that causes the assertion fail. I've tried both with and with-out quotes for the String value in [ScoreText].

    Any ideas what might cause this?

    Timers seem to work fine though :).
  • edited January 2010
    I'm glad the timers work! :)

    As for the crash, it's a very interesting one! ^^ Which version of orx are you using? The embedded one or the other?

    I'd suggest to rebuild orx to make sure everything is based on the same .h include files as the structure IDs have been changed. Make sure you also use the latest .h files for your own project along the rebuilt .lib/.dll. Lastly, try to rebuild your own project.

    If you still have the errors afterward, would you mind making a zip with your project (source, include + solution/workspace) so that I can try locally to see what's going wrong?
  • edited January 2010
    I've built and I've rebuilt again and again. Finally I got it to work once I removed the orx source and got it from svn again. I used TortoiseSVN to just update my previous copy, so it was probably something that wasn't cleaned/rebuilt properly. Including work today I've probably spent 3 hours just building and rebuilding different packages :).
  • edited January 2010
    I hate when this happens! ^^

    So everything's working now? I had similar issues with TortoiseSVN at some point, but it didn't happen for a couple of weeks now. I have no idea what went wrong either.
  • edited January 2010
    Yep, everything is working great now. I'd love to blame TortoiseSVN, but these things seems to happen to me a lot so I'll just be happy thats things worked themself out this time :).
    As for the timers, they work great. They really simplify things for me, so thanks for that. I haven't checked how accurate they are, but I assume they're about as accurate as the clock they run on.
  • edited January 2010
    I'm glad you find the timers helpful. I've been wanting to add them for ages but never took the time to do so. Which was ridiculous of me as it was a mere 2-3 hours work total.

    Their precision is the exact same one as the clock. So if you register them on a 60Hz clock, their precision is 1/60s (not including the OS-dependent time precision which is too small to be taken in account here).

    Also note that the timers are influenced by the time modifier of the associated clock, ie. if you slow down the clock, timers will also slow down. :)

    Lastly, you can safely remove a timer from within its callback. For example, if you register a timer with a function MyTimer() and a repetition of -1 (infinite), you can safely remove this timer with a call to orxClock_RemoveTimer() from inside MyTimer(). The timer is actually flagged as obsolete and memory will be freed during the next clock update.
Sign In or Register to comment.