Game Development Community

dev|Pro Game Development Curriculum

Game Manager with Time and Weather

by Bil Simser · 07/23/2004 (9:42 am) · 89 comments

I wanted to build a nice, pluggable time and weather system into Torque for would-be RPG creators to use. I didn't see anything that I really liked already so I built this one.

NOTE: The resource link is at the end of this article due to size restrictions when uploading to GarageGames so it's hosted offline.

www.bilsimser.org/images/weather2.jpg
Features:
* configurable game time system that handles time and weather advancement (you can use it for any game management you want like bots, spell recharge times, whatever but this mod just gives you time and weather)
* completely scripted system and easy to modify, extend and add to
* you determine how fast (or slow) time advances (real time, 1 game time hour = 75 seconds, whatever)
* weather system tracks atmospheric pressure and changes accordingly (based on values and the time of year)
* time system is streamed out to the file system on the server and can be reloaded to keep a pseudo persistant world going (at least as far as keeping track of time goes)
* weather system initiates clouds, rain, fog, lightning and cleans up when the cycle stops (pressure builds and declines so it's not constant)
* time and weather system can be queried by user to get information (yes, they can look up into the sky but someone people like to know what's going on through messages).
* configurable time system uses whatever calendar you want (real, fantasy, etc.) with day and month names to give your users a rich experience.

www.bilsimser.org/images/weather.jpg
QuickStart
So you want to get up and running with this. Easy! Here's the instructions to try out the no-hassle install:

1. Unzip the resource into your game directory. Any game will do (and this will work with an out-of-the-box starter.fps)
2. Go into the file server/game.cs and in the onServerCreated() function, add the following line:
exec("./gameMgr.cs");
3. In the same file add the following code to the startGame() function:
// Start the GameManager
new ScriptObject(GameManager);
MissionCleanup.add(GameManager);
GameManager.init();
4. Add the following line to the endGame() function:
// Stop the GameManager
GameManager.delete();
5. Launch the game and wait.

Time and weather changes will start. There are two hotkeys setup for you.
F5 will display the current time and date
F6 will display the current weather conditions

The Mod
The mod is made up of the following files and their locations:
server/scripts/gameMgr.cs - The Game Manager class
server/scripts/time.cs - Time Manager class
server/scripts/weather.cs - Weather Manager class
data/environment/* - rain and lightning graphic files
data/sounds/environment/* - rain and lightning sound files

How it Works
The GameManager class runs everything. You call the ::init method from your own game.cs file to kick it off. It will then initialize any game managers you create and start its own ::update schedule.

The ::update method just calls the same method for each manager. In the same, the TimeManager.update method handles advancing the game calendar while the WeatherManager.update will change the conditions in the game, creating rain, lightning and fog when appropriate.

The GameManager.update method runs every second and tracks a variable called pulse. The pulse variable keeps track of our game clock ticks and is used to determine when a game event fires. You can have as many game events as you like (time, weather, hit point regen, etc.)
and have them fire at different periods. Pulse is updated once per pass through the update method so you just set the frequency of how often your own event handler fires. Think of pulse as a counter.

There's also a global variable called $GameManagerPrefs::SecsPerGameHour. This is the number of real seconds that pass in one game hour and determines when a "game" hour has passed. You can use it to fire events on an hourly basis (e.g. a player needs 1 hour to recharge a spell). The time and weather system update every game hour with this:
if((%this.Pulse % %this.SecsPerGameHour) == 0)
{
    TimeManager.update();
    WeatherManager.update();
}

In the update method you can use the Pulse variable to fire an event every 30 seconds (using the Modulus operator) with this call:
if((%this.Pulse % 30) == 0)
{
    // Do something every 30 seconds real-time
}

So even though the TimeManager.update is happening every second, you don't have to do something every second (which might be taxing on your system). Just try to keep your events short and not too CPU intensive.

Tweaking the System
At the top of each file there are some global variables that can be tweaked. In the TimeManager for example, the number of days in a week, months in a year, etc. as well as the day and month names can be changed to suit your game. Just change whatever you want there and the rest will be updated.

Global Key bindings
The sample mod contains two global keybinding, F5 and F6 (which were unused so I chose them) to call the methods to display the time and weather information to to user. You can move these or rebind them to other keys as you see fit.

Utility Functions
I've added a min and max function which will return the minimum or maximum values of two numbers. I think every game needs this. There's also a handy function called dice(). Pass it the number of dice you want to roll and how many sides each die has and it will come back with a total. If you're from the old AD&D world, this is your typical 2d6, 4d8 and 3d10 rolls. Hope you find it handy.

Notes and Enhancements
A lot of extendability here but it's a fully working time keeper and weather system for someone to use in a single player or multiplayer game. For example you could build a calendar gui component to display on the screen and have it update as time advances in your game. Whatever your imagine takes you I guess.

The weather is driven by atmospheric pressure values, the current sky conditions and some random chance. It's all in one script function but parts of it could be externalized or become data driven. It's pretty basic so nothing really magical here, just easy to use.

I haven't plugged in a real day/night system as that involves engine changes and wanted to keep this as a scripting mod only so you're welcome to do that. The system keeps track of the hour of the game time day so you can still use it (like only allowing certain things to happen at night or the automatic opening and closing of stores during business hours).

With the system running, you can do some cool stuff like display the game time on the screen for players to see. As I mentioned, you can also query the TimeManager system about what the hour of the day is and do things like restrict access to areas, transform those infected with the lycanthrope disease when the moon is out, or otherwise just mess with your players lives when the sun rises and there's no coffin around to sleep in. The choices are yours.

This is, overall, pretty basic but it's a start. Read the source as there are many comments about how to expand or extend this. If you do create something better or add onto it, please let me know and I'll update this for everyone to share.

TorqueScript isn't OO but I did try to make it somewhat OO'ish. Personally I think there's too many global variables and maybe too many ScriptObjects being created so that could be improved. As well, perhaps putting the Managers into an array or list would be better. In any case, if you do improve on it or find a bug share it.

You can also find the resource here:
www.bilsimser.org/download/gamemgr.zip

Enjoy!
Page «Previous 1 2 3 4 5 Last »
#1
07/23/2004 (10:44 am)
Awesome Bil, I'm downloading and implementing immediately!
#2
07/23/2004 (11:24 am)
Great job! I never would have thought of making something like this, but now I just might use it!

Fantastic again!
#3
07/23/2004 (11:41 am)
Very nice! Thanks for posting this :)
#4
07/23/2004 (1:07 pm)
Awesome, Bil! Nice job.
#5
07/23/2004 (4:08 pm)
Great work Bil. You've saved me a fair bit of time with this, possibly enough time to actually start the game i needed it for ;-)
#6
07/24/2004 (2:23 pm)
So, I did some tinkering today. Got some stuff for ya ...

in time.cs, around line 110 (wont be exact due to some other mods), change:

if(((day % 100) / 10) != 1)

to

if(((%day % 100) / 10) != 1)

Also, I have day/night cycles partly integrated. Few timing probs to fix yet, but here's a screenie:

www.burntwasp.com/bwrpg/WDN1.jpg
Might have a resource on that eventually, but my time is severely limited and theres still some timing problems.

T.
#7
07/24/2004 (5:13 pm)
never seems to rain..lol
#8
07/24/2004 (5:24 pm)
@Tom: Thanks for the catch. I'll update the resource. Screenshot looks good. I'm assuming you have to do engine changes to get the day/night cycles working as I don't think you can do it with pure scripting changes.

@Westy: Depends on a bunch of factors. The way the pressure works is that it's not guarenteed that it will rain. I ran one simulation (sped up) that took about 2 weeks gametime before rain appeared.

The cool thing is that you can use this to your advantage. In a RPG type game where the land depends on rain for growth, it would make for a more interesting game if you get a drought that wasn't planned for.
#9
07/24/2004 (6:22 pm)
Bil,

Yeh, it needs C++ changes. I used the most recent day/night resource (forgot URL/author, it was from March this year i think, its easy to find). The patch didnt apply cleanly to today's HEAD, so I dont reccomend anyone try it unless they're happy with patching since some hand-hacking based on .rej files is required. I initially tried it the short-cut way of just syncing up the two different concepts of time, which didnt work too great but it worked well enough for proof of concept. To do it properly it's better to make the time manager take its time from the day/night cycle thingy. Oh, there was also a few C++ changes to the day/night code to fix some timing issues.

Watching TGE do day/night and dynamic weather all at the same time is seriously cool.

T.
#10
07/24/2004 (6:52 pm)
Tom,

Would be great if you can create a clean patch file off the current HEAD. It might help newbs in getting the day/night stuff patched in cleanly.

Looking forward to seeing your progress on this. Hope the mod is working for you.
#11
07/25/2004 (6:16 am)
Bil,

Sure. It's probably better to wait until I have the bugs fixed since there's a few show stoppers (e.g. sky box is very noticably clipped). I'll post some comments in the day/night resource.

T.
#12
07/25/2004 (7:39 am)
Another couple of buglets ... in serverCmdDoTime() in time.cs, around line 94, you have:

// Get the hour of the day value from a 24 hour clock
if($time_info.hours % 12 == 0)
    %hour_of_day = 12;
else
    %hour_of_day = $time_info.hours % 12;

should be:

// Get the hour of the day value from a 24 hour clock
%hdiv2 = $hours_in_day / 2;
if($time_info.hours % %hdiv2 == 0)
    %hour_of_day = %hdiv2;
else
    %hour_of_day = $time_info.hours % %hdiv2;

otherwise for a non-24 hour clock, time reporting is fubar'd.

Another slightly more "serious" bug ...

In time.cs, which I should add runs from the server directory, you're mapping the keyboard command for the client. This should be split out into a seperate .cs, lest only the player hosting the server be able to use the keybind. After discovering that, I went to look at the weather manager and that's missing it's binding completely. The fix for this is so easy I'll leave it as an exercise for the reader ;-) (mostly because I havent done it myself yet)

Will post more fixes if I turn up more nasties :)

T.

Edit: For uber n00bs: forgot to mention that the seperate .cs file should be placed under client. Execing that from default.bind.cs seems to be as good a place as any. Of course, you could hack it straight into default.bind.cs which would probably be simpler. Depends on whether you want the code kept seperate or not :)
#13
07/25/2004 (11:24 am)
@Tom: Thanks for the additional bugfix. I didn't have anyone to test this so I figured there would be a couple of gotchas.

As far as the keybindings, yes, they should be put in default.bind.cs but I had made it as simple as possible so you didn't have to do anything however that's the proper way to go. Silly me always running a local server.
#14
07/25/2004 (3:46 pm)
Hehe. Running a local server isnt so bad, provided you test networked every now and then. Actually, the above bugs never showed up in the game, I noticed them by reading the code when looking for something. I'm ashamed to admit I still havent done a multiplayer test either, mostly because i'm too lazy to plug in the linux box.

Dont really have any time for more hacking now, but will look through the weather manager next time I have time to work on the el secreto project. Dont think any more useful stuff is going to come out of my hacking on the time manager for now since the time management code is too far removed and now completely reliant on the sun's position and elevation, so hard to tell if any problems are due to my changes or your original code.

Anyway, It's a nice resource. Pretty useful as it is, but it still has a lot of potential.

Tom.
#15
08/01/2004 (11:33 am)
I'm new at this, and would appreciate just a bit of help... Everything is where it should be, the engine compiles fine, but no weather/time manager seems to be there... f5 and f6 don't do anything... Am I compiling the right files? I put the scripts in the starter.fps folder (in their respective sub-directories), and am compiling torqueSDK.dsw... Is this the right file to build/compile? Any advice would be well received. Thank you much
#16
08/03/2004 (6:13 am)
I have had the same trouble as Daniel. It loads up with no problem, but no changes take place nor does F5 and F6 perform anything. Any help would be appreciated.

@Tom: Man those screenshots look sweet. I had been trying to implement the day/night cycles but kept getting some errors, so I'm still toying with that resource. Nice to see it in action though.

EDIT: typo
#17
08/03/2004 (7:01 pm)
Chip,
I have since found my problem... I'm sure everyone else already got this one but I don't see it posted on this page so...

there's a scriptobject command (I think in gamemgr.cs) that needs the word "new" in front of it. (Much like the other scriptobject commands on the same page if I remember correctly.)

I noticed it cuz in the console I saw that it was indeed trying to load the gamemgr, but couldn't find the object. Hope that helps
#18
08/03/2004 (7:55 pm)
Hmmm. Haven't noticed a problem (besides the couple of bugs Tom found). All the ScriptObject calls have new in front of them so I'm not sure what the problem is.

The F5/F6 bindings are not set (at least the F6 isn't) so you should add that (it really should be in default.bind.cs but I wanted this mod to be self-contained).
#19
08/04/2004 (3:55 am)
Yeah The problem I had must've been my fault... thanks
#20
08/04/2004 (9:42 am)
Thanks Daniel, I'll check into that later tonight.

The bindings...duh how could I miss that. I thought maybe they were embedded in the GameManager script. Thanks though for all the help!
Page «Previous 1 2 3 4 5 Last »