Author Topic: Help Using Timers in Scripted Game Dev?  (Read 3220 times)

tebakutis

  • Visitor
  • *
  • Posts: 6
    • View Profile
Help Using Timers in Scripted Game Dev?
« on: 2006-11-20, 04:14:37 PM »
Okay, I must be the biggest noob in existence because I've checked this forum and the Troubleshooting forum and NO ONE has ever asked a question about how to use timers in Gamedev. So, I guess I'll start.

What I need is the basic script to create a timer in Gamedev script, basically so I can do things such as have functions fire after a certain period of time. For instance, having the player switch to an 'invicible' sprite when hit, then switch back to the normal sprite after say, 2 seconds.

I've looked at sample code using timers and I'm just confused as to what is going on here. So, if somebody could just post how to create a basic timer here, I'd be very appreciative.

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Help Using Timers in Scripted Game Dev?
« Reply #1 on: 2006-11-20, 05:47:37 PM »
I can't remember for sure, but I think some projects may have accomplished this without timers by using invisible inventory items that get incremented/decremented by one each frame.  Have you considered such a solution?

tebakutis

  • Visitor
  • *
  • Posts: 6
    • View Profile
Re: Help Using Timers in Scripted Game Dev?
« Reply #2 on: 2006-11-20, 05:54:20 PM »
This is actually the solution I was able to ferret out by digging through threads, following a link to forums, and following a link from there to more forums. :0 I managed to get a basic timer working in game using the approach of incrementing a player inventory item each frame and basing the timing of functions off that. My only concern would be how much processing power such a mechanism takes up.

I don't see it being a big issue with one timer. However, what if I want two timers running simultaneously? Four? Eight? It seems to me the load on the game engine could get quite heavy given enough use of timers, and though there are cases where you could reuse the same timer, I'm assuming you will have to have some timers running independently in cases where both timers may need to be running at once (for instance, a cooldown on use of a spell PLUS a cooldown on your invincibility buff).

Basically, the incrementing an inventory item approach works (going by 60 = 1 second) but the impact it has on gameplay worries me. How big a hit do you think a game takes from running multiple timers? (which I'm assuming must always be running in the background, as the function to increment them is played in OnAfterMoveSprites).

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Help Using Timers in Scripted Game Dev?
« Reply #3 on: 2006-11-20, 06:06:24 PM »
You don't have to write script to increment the inventory count.  That can be done with a global special function of type "Alter Inventory" (global special functions affect the whole map, so you'd need one per map for each map that needs to support this).  I think the overhead of implementing a timer like this is insignificant, as long as the inventory item is invisible.  You should be able to run 10, maybe even as many as a couple hundred of these without a measureable difference.