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!
#61
10/19/2005 (3:36 pm)
Here's my updated resource - "Enviro-Torque":

Enviro-Torque Resource Page


Includes
------------
- Stand-alone "GameManager" mod (date/time/weather) - updated
- Enviro-Torque patch/mod (integrated GameMangaer, celestial day/night, and fractalskies)

For
------
TGE 1.3.x



ps. @bil: I have an updated-stand-alone weather mod in there. It might save you some work on the known bug fixes.

pps. @ALL: My personal touch on it was adding "dynamic wind" that shifts every time the rain starts to fall, along with varying rain-intensity (drops-per-min.).

I've since refined it to continously update wind as a part of "updateWeather()" but it hasn't made it into the mod yet. Next release though :-P
#62
12/07/2005 (1:42 am)
i try to implemet this but i am getting Integer Overflow error when game load
i check console and i get this error

starter.fps/server/scripts/weather.cs (427): Unknown command setPercentage.

but i don't think problem ie boz of this.
When i try to run the program without GameManager it is working fine
#63
01/16/2006 (2:19 pm)
Top link is really not working. Anyone having this one laying around, or know of a working link?
#64
01/16/2006 (3:39 pm)
I'm moving some files around between hosts and domains and will look into where this is located and provide a much needed update to a new location. Thanks for everyone's patience and contributions!
#65
01/24/2006 (7:16 am)
Hi Bil,
I was just wondering if this was going to be back up soon, I'd like to use it. It looks like a solid system.
#66
01/24/2006 (6:29 pm)
Hi Scott,

I'm just trying to get my files restored to a new location but in the meantime I highly recommend checking out the Enviro-Torque resource page listed above as it has a modified version and some other features that are quite useful.
#67
01/26/2006 (8:22 am)
Bil, excellent resource. Has anyone ever added the GUI components? Just a simple..

22:00, January 25, 2006 sort of display for me would work.
#68
02/25/2006 (11:34 pm)
@Tom: Did you ever get the day/night cycle intergration finished for this resource?

Thanks :)
#69
02/25/2006 (11:37 pm)
actually reading above my post here a little revealed the Enviro torque resource. I will investigate that :)
#70
03/16/2006 (10:33 am)
Does this work over a network?

EDIT:NVM, yes.
#71
05/29/2006 (2:23 pm)
Where can i get this resource? This Link doesn't work anymore.
#72
06/16/2006 (5:40 am)
I still had the file so here is a temp place for it.

www.matt3d.com/files/gamemgr.zip
#73
07/17/2006 (3:24 pm)
OMG!!!!! the effects are amazing!!!! Ns job
#74
07/23/2006 (4:38 pm)
Hi there, I was just wonderring if someone could help me set up this applyDamage function? Im not really sure where to put it or where in the script to call it so that it does damage? Also do I need to set up variables for damage radius and damage amount? A simple point in the right direction would hopefully be all I need. If this requires engine changes though I wont be able to do it since Ive not yet bought the TGE.

This is the code I saw above:
function LightningData::applyDamage(%this, %obj, %hitObject, %pos, %normal)
{
	radiusDamage(%obj, %pos,  %this.damageRadius, %this.damageAmmount,"fire",40);
}


I would be very grateful for any help!


Jon
#75
09/02/2006 (1:44 pm)
is there a link for the file?
#76
09/04/2006 (9:12 pm)
Does anyone have this resource that could be shared or hosted somewhere?
#77
11/03/2006 (7:50 am)
*BUMP* Anyone had luck locating the resource file? I'd like to dig around and compare to what I have running.
#78
12/31/2006 (11:34 am)
I'd be interested in this resource as well!
#79
04/26/2007 (12:57 pm)
www.matt3d.com/files/gamemgr.zip
#80
09/11/2007 (5:51 pm)
Hello. I had some troubles using this resource in my project (see here for details).
I uploaded a patch to apply on a clean 1.4.2 TGE install on my website here, and I left this resource's code commented out.
Can you give me some hints on what I done wrong? Thanks in advance for anything.

Bye, Berserk.
.