Adding Time to a game
codingmasters
04 Jan 2004, 22:56Right now I'm programming a game which must have time. Is there anyway to it so I can have actual time in the game, or do I have to have 24 hour time instead?
Matthew G.
Matthew G.
I think Im Dead
05 Jan 2004, 08:14Time is actually pretty easy to implement as far as quest goes, and I'm assuming by 'actual time' you mean 12 hour. That is really just a matter of formatting the variables. Here's a simple time script, it's used in the sample battle engine posted a while back and you can customize or change the code however you want.
The way this time script is set up it requires an object defined in your game called "game.info" this can easily be changed to whatever you want or eliminated all-together if you really want to make things your own, ie; you could opt to use variables or whatever you want. It also requires a few variables be set in your "startscript"(if this is a single or multiplayer game, it still goes in the startscript section).
The startscript looks like this.
The game.info object should look something like this.
This is the procedure, it is run after every second.
The final part is the timer, it looks like this.
If you put all the pieces of code in the right places of your game file it should work fine, it also should be pretty easy to modify into supporting 12 hour time, all it really takes is changing a couple numbers and adding an IF to change from AM to PM. You can change the properties of the game.info object to modify the time(which the script does), this is useful for setting the starting time or changing the time of day(say a player is knocked out and wakes up later or something).
On a side note, TIME PUZZLES SUCK! Don't make some crappy part of your game where you have to be at a certain place at a certain time, I don't think anybody likes that.
The way this time script is set up it requires an object defined in your game called "game.info" this can easily be changed to whatever you want or eliminated all-together if you really want to make things your own, ie; you could opt to use variables or whatever you want. It also requires a few variables be set in your "startscript"(if this is a single or multiplayer game, it still goes in the startscript section).
The startscript looks like this.
startscript {
set numeric <second-time; 00>
property <game.info; second-time=00>
set numeric <minute-time; 00>
property <game.info; minute-time=00>
set numeric <hour-time; 00>
property <game.info; hour-time=00>
}
The game.info object should look something like this.
define object <game.info>
alias <game.info>
properties <hour-time=00; minute-time=00; second-time=00>
end define
This is the procedure, it is run after every second.
define procedure <time>
set numeric <second-time; $objectproperty(game.info; second-time)$ + 01>
if ( %second-time% >= 60 ) then {
set numeric <minute-time; $objectproperty(game.info; minute-time)$ +01>
set numeric <second-time; 0>
set string <s-format; %second-time%>
if ( $lengthof(#s-format#)$ = 1 ) then set string <second-time; 0%second-time%> else set string <second-time; %second-time%>
if ( %minute-time% >= 60 ) then {
set numeric <hour-time; $objectproperty(game.info; hour-time)$ +01>
set numeric <minute-time; 0>
set string <m-format; %minute-time%>
if ( $lengthof(#m-format#)$ = 1 ) then set string <minute-time; 0%minute-time%> else set string <minute-time; %minute-time%>
if ( %hour-time% >= 24 ) then {
set numeric <hour-time; 0>
set string <h-format; %hour-time%>
if ( $lengthof(#h-format#)$ = 1 ) then set string <hour-time; 0%hour-time%> else set string <hour-time; %hour-time%>
}
else set string <h-format; %hour-time%>
if ( $lengthof(#h-format#)$ = 1 ) then set string <hour-time; 0%hour-time%> else set string <hour-time; %hour-time%>
property <game.info; hour-time=#hour-time#>
set string <time; $objectproperty(game.info; hour-time)$:$objectproperty(game.info; minute-time)$:$objectproperty(game.info; second-time)$>
}
else set string <m-format; %minute-time%>
if ( $lengthof(#m-format#)$ = 1 ) then set string <minute-time; 0%minute-time%> else set string <minute-time; %minute-time%>
property <game.info; minute-time=#minute-time#>
set string <time; $objectproperty(game.info; hour-time)$:$objectproperty(game.info; minute-time)$:$objectproperty(game.info; second-time)$>
}
else set string <s-format; %second-time%>
if ( $lengthof(#s-format#)$ = 1 ) then set string <second-time; 0%second-time%> else set string <second-time; %second-time%>
property <game.info; second-time=#second-time#>
set string <time; $objectproperty(game.info; hour-time)$:$objectproperty(game.info; minute-time)$:$objectproperty(game.info; second-time)$>
timeron <time>
end define
The final part is the timer, it looks like this.
define timer <time>
interval <1>
action do <time>
enabled
end define
If you put all the pieces of code in the right places of your game file it should work fine, it also should be pretty easy to modify into supporting 12 hour time, all it really takes is changing a couple numbers and adding an IF to change from AM to PM. You can change the properties of the game.info object to modify the time(which the script does), this is useful for setting the starting time or changing the time of day(say a player is knocked out and wakes up later or something).
On a side note, TIME PUZZLES SUCK! Don't make some crappy part of your game where you have to be at a certain place at a certain time, I don't think anybody likes that.
codingmasters
05 Jan 2004, 08:33Thanks ITID. But by the time I got back onto the forums I had already found a way to implement 24-hour time, so it doesn't matter. Thanks for the script though, I'll make sure I copy down the code.
Matthew G.
Matthew G.
Farvardin
05 Jan 2004, 17:28For IF-type adventure game, I generally dislike the idea, but some ppl may enjoy it on the other hand...
I like to see IF as enigma to solve, so I prefer puzzle and reflexion rather than acting quickly during a certain moment of time
I like to see IF as enigma to solve, so I prefer puzzle and reflexion rather than acting quickly during a certain moment of time