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«First 1 2 3 4 5 Next»
#81
12/25/2007 (11:02 pm)
I don't see the weather effects for some reason, but it displays the messages 'Sun rises from the east' and so on.

Also, how rain/lightning can stop if it isn't raining/lightning?

Good resource, but needs little tweaking.
#82
12/25/2007 (11:38 pm)
Yes, that's the same problem I'm experiencing.

Bye, Berserk.
.
#83
12/29/2007 (3:15 am)
-snip-
#84
01/05/2008 (7:36 am)
Found these useful commands for the day & night.

Sky.setFieldValue(useSkyTextures, "0");
Sky.setFieldValue(SkySolidColor, "1 1 1 1");

You can play around with these colour vaules in the mission editor.
#85
07/06/2008 (5:45 pm)
was wondering how i could add in a call to player health regen via the game manager, ie how to set a variable for how much health regen the player gets in a global variable or variable saved in a file and then have the health go up that amount each tick etc
#86
09/20/2008 (2:11 am)
Does anyone still have this resource? I seem to have lost it in my drive format.
#88
10/02/2008 (9:43 am)
Awesome, thanks.
#89
03/23/2010 (3:38 pm)
You can find it hosted over here too ... merged with some other code.
http://www.torquepowered.com/community/resources/view/8848
Page«First 1 2 3 4 5 Next»