Game Development Community

dev|Pro Game Development Curriculum

Mission Type Objects

by Daniel Neilsen · 01/30/2003 (6:45 pm) · 14 comments

Download Code File

I have created this tutorial as I have often seen users of TGE getting frustrated over this. This is a very differnet way to set up TGE but I find it is a LOT easier to handle all forms of scripting in this manner, especially when the game requires multiple game types.

This tutorial contains files that can be placed into a raw HEAD (28/1/03) which will set the scripts up to use object oriented game type code. When you load up the game it will "seem" exactly the same except for the comment in the console telling you it has loaded some game code.

Anyway, heres the tutorial. Hope it helps some people


This tutorial contains the following files, all of which are compatible with 
the TGE CVS HEAD as of this date.

FILES:
	game.cs
	defaultGame.cs
	DMGame.cs
	CTFGame.cs
	

	WARNING!!!!  BEFORE YOU GO FURTHER BACK UP ALL OF YOUR FILES!!

INSTALLATION:
Copy all of the files above into the example/fps/server/scripts directory.  
Please note that this OVERWRITES the original game.cs as a game object system 
has to run at the core of the script engine to work effectively.


ADVANTAGES OF GAME TYPE OBJECT ARCHITECTURE
- standard functions are available to all game type objects whilst being easily
  altered or overwritten using object-oriented code.
- Script is a lot cleaner as all functions, variables, schedules, etc, etc that 
  are associated with the game object is totally destroyed at the end of each 
  mission
- Game types can very easily be created using this method as 99% of the code 
  is already create in the DefaultGame
  
  
GAME OBJECT HEIRACHY
Most TGE users would be familiar with the heirachy of the player object
eg.	GameBase->ShapeBase->Player
and the game object is along the same lines

	ScriptObject->DefaultGame Object-> Game Object
	
From this model it should be easily seen that any DefaultGame function would 
be available to the game object but, using object inheritance, can be altered
or even overwritten by the Game Object.


HOW IT WORKS
The most important piece of code for this can be found in defaultGame.cs at 
approx line 62-72.  Going through the code we are doing the following:
	Line 64-66 - 	Determine the mission code to load from the mission file
	Line 67-71 - 	Create the game object with superclass of defaultGame
	Line 72	-	Activate the game package (if applicable)
At the end of each mission, the game package is deactivated and the game object,
including all its functions, schedules, variables are deleted.


DMGame and CTFGame are sample game code files (they dont actually do anything though)
and show some of the features of using this system such as:

DMGame::startGame 	- 	This function shows how to load the defaultGame::startGame 
				function AND add code to the end of it for the DM gametype.
DMGame::initGameVars 	- 	This function shows how to overwrite the 
				defaultGame::initGameVars function
DMGame::onMissionLoaded - 	This function shows how to load the 
				defaultGame::onMissionLoaded function AFTER DM game changes

onLeaveMissionArea	-	This shows how to use the GameType packaging that is also 
				part of the system
				

HOW DO I SET UP MISSIONS FOR THIS SYSTEM
Very very easily.  Just simply alter the MissionInfo object of the mission to include 
the mission type.  Here is an example on the Scorched Planet mission.
   new ScriptObject(MissionInfo) {
         name = "Scorched Planet";
         gametype = "CTF";
         descLines = "2";
         desc0 = "Wide open spaces and huge pyramids dominate this scorched planet.";
   };

Edit: ResetMission and endMission functions altered to destroy game object code.

#1
01/31/2003 (11:01 am)
Thanks Daniel, this resource will really help me to organize my code much better

Just one question... How do I spawn a player after he/she dies? I always get this error:

game/server/scripts/camera.cs (39): Unknown command spawnPlayer.
  Object (1068) GameConnection -> GameConnection -> GameConnection -> NetConnection -> SimGroup -> SimSet -> SimObject

[edit]
problem solved
[/edit]

Mike
#2
02/02/2003 (11:25 am)
Daniel, I think you should move your delete and deactivatepackages commands from onServerDestroyed to somewhere else. OnServerDestroyed is only called when, well, the server is destroyed... only... not on mission change, since when chaning missions you don't destroy the server. So when chaning mission the packages aren't deactivated and the Game object isn't destroyed.
In my game I placed these calls in resetMission() in missionLoad.cs. I also placed the gametype "loading" code in there instead of overriding it as a package as you did; i don't see the need for it to be overriden like you did.
#3
02/02/2003 (11:25 am)
Disregard this post
#4
02/02/2003 (12:10 pm)
You are correct about the game object not being detroyed, my mistake, I will update the code.
The main reason I used a package to overwrite these functions is it is a lot cleaner then altering code in the common dir.
#5
02/03/2003 (1:25 pm)
Hm, I'm getting 2 errors if I call "cycleGame();":

*** MISSION RESET
projectca/server/scripts/gametypes/defaultGame.cs (137): Unable to find object: 'game' attempting to call function 'onMissionReset'
*** ENDING MISSION
projectca/server/scripts/gametypes/defaultGame.cs (100): Unable to find object: 'game' attempting to call function 'onMissionEnded'
*** LOADING MISSION: projectca/data/missions/pca_arktis1.mis
any idea why the "game" object isnt defined there anymore?
thx!
#6
02/03/2003 (1:44 pm)
@Beffy: It helps to understand what you are doing and why instead of copying daniel's game code, He did it very quickly so probably it doesnt work at all, it's just an example to show how things should be arranged. I looked at how he did the game object (didnt knew about superclass thingy) and then I started moving my game related functions over when I needed to.
I haven't tried copying daniel's code over and running it, so I have no idea if that's an existing error or you did something wrong, I would go over the whole code again and see if there's anything that isn't obviusly making sense.
#7
02/04/2003 (6:51 am)
Hm, I've just put game.onMissionReset() and game.onMissionEnded() into the if(isObject(game)){...} if clauses and everything seems to work without errors...
there are so many functions involved upon game ending/mission cycling that it's hard to keep track what's really going on... :P
#8
10/16/2003 (4:33 pm)
Been a while, but has there been any notable changes to this resource? There's some problems with it, and I'm trying to wrap my head around it. If i can get it to work like it should, it's an awesome resource. Specifically, I don't see DefaultGame::endGame() ever being called.

Thank you,

Robert
#9
10/16/2003 (7:15 pm)
The basic overall structure still works but for newer versions of TGE, I think some of the function names may have altered.

Basically, you need to run Game.endGame() from the function endGame() and likewise for other functions. This allows the contents of the function to be controlled by the game object (and hence changed with differing game types).
#10
12/28/2005 (4:28 pm)
I've just tried this with TGE1.4, and there are just a few small changes required to get it working. Assuming you are just modifying starter.fps, the DefaultGame::createPlayer function in defaultGame.cs should be like this:
function DefaultGame::createPlayer(%game, %client, %spawnPoint)
{
   if (%client.player > 0) 
   {
      // The client should not have a player currently
      // assigned.  Assigning a new one could result in
      // a player ghost.
      error( "Attempting to create an angus ghost!" );
   }

   // Create the player object
   %player = new Player()
   {
      dataBlock = PlayerBody;
      client = %client;
   };
   MissionCleanup.add(%player);

   // Player setup...
   %player.setTransform(%spawnPoint);
   %player.setShapeName(%client.name);

   // Starting equipment
   %player.setInventory(Crossbow,1);
   %player.setInventory(CrossbowAmmo,10);
   %player.mountImage(CrossbowImage,0);

   // Update the camera to start with the player
   %client.camera.setTransform(%player.getEyeTransform());

   // Give the client control of the player
   %client.player = %player;
   %client.setControlObject(%player);
}
#11
03/12/2006 (3:27 am)
WOW! This resource is exactly what I was looking for!

I was just playing around with the CTFGame.cs, and found that I could override just about everything :D (ie. Armor::onDamage()!). This makes my life so much easier, and will now allow me to start cranking out fun game types!

No ammount of exclamation marks can express my joy! Thanks man <3
#12
03/22/2007 (4:57 am)
I was looking for this kinda things. Thanks man. You saved my effort.
#13
08/02/2007 (10:55 am)
how do u accses the spawn player function
#14
01/19/2010 (10:15 pm)
I know this is an old post but has anyone tryed this with latest TGEA version? Thanks.