Teen Programmers Unite  
 

 

Return to forum top

help with a restocking system...

Posted by kmn [send private reply] at April 09, 2003, 07:22:23 PM

Hello, I'm making a game that has shops with a quantity of each item, and I need to make it so every, say, 10 minutes it restocks back up to full items. How would I go about doing something like that? Also, I want it to revive everyone at midnight every night. So basically what I'm asking is how do make a script run at a certain time?

Posted by unknown_lamer [send private reply] at April 09, 2003, 08:15:58 PM

Timers. Pthreads provides them (so does Qt with a nice QTimer class). You can also implement your own by checking the time in the main until you reach your target time. (My IRC bot has some timers stuff, http://sv.nongnu.org/projects/bobotpp/ if you want to grep around in ScriptCommands to see how they are implemented).

Posted by mop [send private reply] at April 09, 2003, 08:22:21 PM

This is a pretty trivial problem, so I'm assuming that you want code to do this with.. even though you didn't specify what you are using.

Either way, I can think of you needing the game to have a built in timer, or access to the computer's clock (although this would allow users to cheat) that divides the minutes by 10 maybe, and if there is no remainder restocks the stores. Then the respawning part would just act on the same principal only every 24 hours.

Posted by buzgub [send private reply] at April 10, 2003, 01:18:32 AM

if this game is web-based, then I would suggest storing the date of the last midnight noticed and the last restock. Every time you load the page, check if it is another midnight or time for a new restock, do that, and store the new time in the relevant variable.

The thing to avoid here is race conditions if one person visits the page while a refresh is in progress - it might see a half-refreshed state or start another refresh, depending on how you do it. You'd need to work out how to avoid that if you wanted your website to be a quality one.

If you are able to do stuff based on timers, though, that's a superior solution - this suggestion is useful only for a game forced to use a request/response model, where the game must be dormant between requests.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.