Capture the Flag Triggers
by Anthony Rosenbaum · 06/06/2002 (1:05 pm) · 44 comments
A)ADDING the Flag mesh
LabRat was kind enough to post some free Flag mesh and the basic flag.cs for everyone.HERE is a link. Put the flag.cs file in /server/scripts folder and put the flag folder (full of the mesh and their textures) into data/shapes folder, Ok now we are ready to impliment all this stuff.
INTO THE CODE
B)Initializing flag.csGo to /server/scripts/game.cs and add
C)Adding Flags to Inventory
I ALWAYS forget this, I was going nuts because the flag wasn't mounting. So be sure to add whatever flag you want you player to carry to their datablock so in player.cs I added:
D)Making a Capture the Flag Trigger
This code is my own twist, based off of James Lupiani's and Phil Carlisle's code for Realm Wars Team Assault their file is called teamObjectives.cs and I liked that so we'll call out new file the same.
In our new file we'll put in this code:
1)Is this trigger for this player's team and
2)Does this player have a flag object in his inventory
If both are true the flag will be remove and will be respwned at a location we will declare later(with a Marker Object). There is some other code I left in there that increments a Capping variable and it also calls to the display.
E)Initializing teamObjectives.cs
Now that we made our trigger we got to make sure it loads. Go back to game.cs and add:
INTO THE GAME
F)Adding a Flag with the Mission Editor
Load up a mission. Goto where you want to put a flag. Hit f11 to bring up the mission editor. Then hit f4 (or select World Editor Creator from the "window" menu selection)Expand the Shapes selection. You'll notice you new Flag category. Expand that and there are 3 Flag objects Flag1, Flag2, and Flag3. Choose one and put it on the ground. Remember which one you chose! Also while your there switch to the World Editor Inspector(f3), and note that flag's position. (Note for the example I will presume you used Flag1 item)
G)Adding a Marker object
Before you move, go back to the World Editor Creator(f4) and make a Marker Object. It is found under Mission Objects - Missions - Marker put the marker down close to the flag.
H)Changing/Adding properties to Objects
With the marker still selected bring the World Editor Inspector up (f3) first off you want to make the marker exist exactly where the flag is at. You can do this manually, or just take the numbers from the flag's position variable and put them into the marker's position variable, then click Apply. Now we need to name the Marker. So type:
I)Adding a Trigger
Almost Done!!!
Goto where you want the flag to be capped at.
While in the World Editor Creator(f4) make a trigger object.Mission Objects-Mission-Trigger A Dialog box will pop up, in it you can name the new trigger object. To change the trigger from a "defaultTrigger" just click in that box, the new "CapFlagTrigger" should be available. Click Ok and a Box repesenting the trigger area is created. Resize the trigger accordingly (f1 for more info on positioning and scaling).
J)Adding Dynamic Properties
All right remember those 3 variables I told you about, here is where they are needed. To add a variable, first off be in World Editor Inspector (f4) and select the object we are adding vaiables to (the CapFlagTrigger of course). Click the Dynamic Button a window pops up. In the Window you can add a name for the variable and a value it repesents.
I added . . . .presuming the players' team is 1, and the flag object is Flag1, and the newly created marker object is labled Flag1Respawn . . . .:
SAVE and that is it. . . .take a player, grab the flag and go cap it! It will respawn. Rewrite the code so it fits your needs, enjoy!!!!
If you are wondering what these object look like in the .mis file here you go.
EDIT Note this works with the origial set up for inventory I am currently altering the code so it works for the new Inventory setup as posted my Tim Newell
EDIT
OK if you are using the Inventory Manager by Tim Newell you have to be sure to change a couple things
A)Changing player.cs
Remove the maxInv[] items from the player.cs instead place them in the game.cs
after you create a player. I have each team get assined a specific type of flag it looks like this
B)Altering Flag.cs
Change the onPickup Function to this:
C)Fixing the Trigger
I put the whole trigger function in, but really only 1 line was changed. . .can you find it?
LabRat was kind enough to post some free Flag mesh and the basic flag.cs for everyone.HERE is a link. Put the flag.cs file in /server/scripts folder and put the flag folder (full of the mesh and their textures) into data/shapes folder, Ok now we are ready to impliment all this stuff.
INTO THE CODE
B)Initializing flag.csGo to /server/scripts/game.cs and add
exec("./flag.cs"); to the rest of the executeablesC)Adding Flags to Inventory
I ALWAYS forget this, I was going nuts because the flag wasn't mounting. So be sure to add whatever flag you want you player to carry to their datablock so in player.cs I added:
maxInv[Flag1] = 1; maxInv[Flag2] = 1; maxInv[Flag3] = 1;to the bottom of the LightHumanArmor dataBlock. That takes care of all the flag types found in the flag.cs file.
D)Making a Capture the Flag Trigger
This code is my own twist, based off of James Lupiani's and Phil Carlisle's code for Realm Wars Team Assault their file is called teamObjectives.cs and I liked that so we'll call out new file the same.
In our new file we'll put in this code:
// Trigger count
$Game::TeamObjectives[1] = 0;
$Game::TeamObjectives[2] = 0;
$Game::TeamScores[1] = 0;
$Game::TeamScores[2] = 0;
$Game::TeamFlagCaptures[1] = 0;
$Game::TeamFlagCaptures[2] = 0;
$Game::PointsForCapture = 50;
//-----------------------------------------------------------
//NEW FLAG CAPTURE TRIGGER
//--------------------------------------------------------------
// Trigger datablock
//--------------------------------------------------------------
datablock TriggerData(CapFlagTrigger)
{
tickPeriodMS = 100;
team = -1;
teamEmitter = 0;
pointsforcapture = 50;
};
//--------------------------------------------------------------
// Events
//--------------------------------------------------------------
function CapFlagTrigger::onEnterTrigger( %this, %trigger, %obj )
{
// This method is called whenever an object enters the %trigger
// area, the object is passed as %obj. The default onEnterTrigger
// method (in the C++ code) invokes the ::onTrigger(%trigger,1) method on
// every object (whatever it's type) in the same group as the trigger.
Parent::onEnterTrigger( %this, %trigger, %obj );
//SO the trigger requires 3 dynamic variable when placing
//invcheckitem = Flag1 OR Flag2 OR whatever it is looking for in the players
//inv and
//team = 1 or 2 or whatever you labled the team property
// and
//respawnat = the name of a marker object
echo("ENTERING A CapFLAG TRIGGER");
// check to see if the lad who is in this trigger is on the our team
if( %obj.client.team.teamId $= %trigger.team )
{
echo("This is your team");
// ok, its one of our lads.. see if he has the enemy flag?
if (%obj.getInventory(%trigger.invcheckitem) > 0)
{
echo("You Scored");
// cool.. we got a capture!
$Game::TeamFlagCaptures[%obj.client.team.teamID]++;
// make a hoot about capping.
messageAll( 'MsgObjective', '\c2Team %1 captured a flag! %2 points!', %obj.client.team,$Game::PointsForCapture );
$Game::TeamScores[%obj.client.team.teamID] += $Game::PointsForCapture;
// take the flag out the guys inventory.
%obj.setInventory(%trigger.invcheckitem,0);
%obj.unmountImage($FlagSlot);
// now return the flag..
echo("ENTERING A CapFLAG TRIGGER");
newflag(%trigger.invcheckitem,%trigger.respawnat);
}
}
}now be warned this trigger REQUIRES you to set up you teams with a variable called teamID. Something like this in the games.cs (if you used exodus' tutorial, as I did, it will be familiar)//TEAM TYPES
$Team1 = new ScriptObject()
{
teamId = 1;
name = "BLUE";
score = 0;
numPlayers = 0;
};Just keep this in mind more on the Id later.The Trigger will use 3 variables(we will set later) to determine 2 things 1)Is this trigger for this player's team and
2)Does this player have a flag object in his inventory
If both are true the flag will be remove and will be respwned at a location we will declare later(with a Marker Object). There is some other code I left in there that increments a Capping variable and it also calls to the display.
E)Initializing teamObjectives.cs
Now that we made our trigger we got to make sure it loads. Go back to game.cs and add:
exec("./teamObjectives.cs");to the list.INTO THE GAME
F)Adding a Flag with the Mission Editor
Load up a mission. Goto where you want to put a flag. Hit f11 to bring up the mission editor. Then hit f4 (or select World Editor Creator from the "window" menu selection)Expand the Shapes selection. You'll notice you new Flag category. Expand that and there are 3 Flag objects Flag1, Flag2, and Flag3. Choose one and put it on the ground. Remember which one you chose! Also while your there switch to the World Editor Inspector(f3), and note that flag's position. (Note for the example I will presume you used Flag1 item)
G)Adding a Marker object
Before you move, go back to the World Editor Creator(f4) and make a Marker Object. It is found under Mission Objects - Missions - Marker put the marker down close to the flag.
H)Changing/Adding properties to Objects
With the marker still selected bring the World Editor Inspector up (f3) first off you want to make the marker exist exactly where the flag is at. You can do this manually, or just take the numbers from the flag's position variable and put them into the marker's position variable, then click Apply. Now we need to name the Marker. So type:
Flag1Respawnin the box next to the Apply button. Then click Apply. Your marker is set and is in the correct position.
I)Adding a Trigger
Almost Done!!!
Goto where you want the flag to be capped at.
While in the World Editor Creator(f4) make a trigger object.Mission Objects-Mission-Trigger A Dialog box will pop up, in it you can name the new trigger object. To change the trigger from a "defaultTrigger" just click in that box, the new "CapFlagTrigger" should be available. Click Ok and a Box repesenting the trigger area is created. Resize the trigger accordingly (f1 for more info on positioning and scaling).
J)Adding Dynamic Properties
All right remember those 3 variables I told you about, here is where they are needed. To add a variable, first off be in World Editor Inspector (f4) and select the object we are adding vaiables to (the CapFlagTrigger of course). Click the Dynamic Button a window pops up. In the Window you can add a name for the variable and a value it repesents.
I added . . . .presuming the players' team is 1, and the flag object is Flag1, and the newly created marker object is labled Flag1Respawn . . . .:
name = team value = 1 name = invcheckitem value = Flag1 name = respawnat value = Flag1Respawn
SAVE and that is it. . . .take a player, grab the flag and go cap it! It will respawn. Rewrite the code so it fits your needs, enjoy!!!!
If you are wondering what these object look like in the .mis file here you go.
new Item(FlagToCapture) {
position = "231.146 -202.12 182.761";
rotation = "1 0 0 0";
scale = "1 1 1";
dataBlock = "Flag1";
collideable = "0";
static = "1";
rotate = "1";
};
new Trigger(capArea) {
position = "13.3721 -167.616 161.581";
rotation = "1 0 0 0";
scale = "16.4867 5.21523 3.8244";
nameTag = "capArea";
dataBlock = "CapFlagTrigger";
polyhedron = "0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000 -1.0000000 0.0000000 0.0000000 0.0000000 1.0000000";
respawnat = "FlagReSpawn";
invcheckitem = "Flag1";
team = "1";
};
new Marker(FlagReSpawn) {
position = "231.146 -202.12 182.761";
rotation = "1 0 0 0";
scale = "1 1 1";
seqNum = "0";
msToNext = "1000";
}; it you put this in Scorched earth. The Flag will be on top of a pyramid and the capping area is inside the building.EDIT Note this works with the origial set up for inventory I am currently altering the code so it works for the new Inventory setup as posted my Tim Newell
EDIT
OK if you are using the Inventory Manager by Tim Newell you have to be sure to change a couple things
A)Changing player.cs
Remove the maxInv[] items from the player.cs instead place them in the game.cs
after you create a player. I have each team get assined a specific type of flag it looks like this
if( %this.team.name $= "Red")
{
%player = new Player() {
dataBlock = RedHumanArmor;
client = %this;
};
%player.addInvItem("Flag2", "0","item","Now You have the flag.",1);
}
else{
%player = new Player() {
dataBlock = BlueHumanArmor;
client = %this;
};
%player.addInvItem("Flag1", "0","item","Now You have the flag.",1);
}this set so only ppl on the Red team can carry Flag2 and Blue team can only carry Flag1B)Altering Flag.cs
Change the onPickup Function to this:
function Flag::onPickup(%this, %flag, %player, %amount)
{
if (%player.getClassName() $= "Player" && %player.getMountedImage($FlagSlot) == 0) {
serverPlay3D(WeaponPickupSound,%flag.getTransform());
//%player.incInventory(%this,1);
if(%player.incInvItem(%flag.dataBlock,1) > 0)
echo("incrimented" @ %flag.dataBlock);
else
echo("nothing");
%player.mountImage(%this.image, $FlagSlot);
%flag.delete();
}
}C)Fixing the Trigger
I put the whole trigger function in, but really only 1 line was changed. . .can you find it?
function CapFlagTrigger::onEnterTrigger( %this, %trigger, %obj )
{
// This method is called whenever an object enters the %trigger
// area, the object is passed as %obj. The default onEnterTrigger
// method (in the C++ code) invokes the ::onTrigger(%trigger,1) method on
// every object (whatever it's type) in the same group as the trigger.
Parent::onEnterTrigger( %this, %trigger, %obj );
//SO the trigger requires 3 dynamic variable when placing
//invcheckitem = Flag1 OR Flag2 OR whatever it is looking for in the players
//inv and
//team = 1 or 2 or whatever you labled the team property
// and
//respawnat = the name of a marker object
echo("ENTERING A CapFLAG TRIGGER");
// check to see if the lad who is in this trigger is on the our team
if( %obj.client.team.teamId $= %trigger.team )
{
echo("This is your team");
echo("you have " @ %obj.getInvItem(%trigger.invcheckitem,"amount") @ " Flags");
// ok, its one of our lads.. see if he has the enemy flag?
if (%obj.getInvItem(%trigger.invcheckitem,"amount") > 0)
{
echo("You Scored");
// cool.. we got a capture!
$Game::TeamFlagCaptures[%obj.client.team.teamID]++;
// make a hoot about capping.
messageAll( 'MsgObjective', '\c2Team %1 captured a flag! %2 points!', %obj.client.team,$Game::PointsForCapture );
$Game::TeamScores[%obj.client.team.teamID] += $Game::PointsForCapture;
// take the flag out the guys inventory.
%obj.setInvItem(%trigger.invcheckitem,0);
%obj.unmountImage($FlagSlot);
// now return the flag..
echo("ENTERING A CapFLAG TRIGGER");
newflag(%trigger.invcheckitem,%trigger.respawnat);
}
}
}There you have it enjoy !!!About the author
#22
12/23/2005 (8:04 am)
Hey i got it all workin apart from making the teams pick up the correct flag, i havn't done the Inventory Manager by Tim Newell do i have to in order to make it work
#23
now teams can't pick up flags that share their team number ex. team1, flag1
02/02/2006 (5:32 pm)
to make it so you can only pick up enemy flags go to inventory.cs and replace the old ShapeBase::pickup with this onefunction ShapeBase::pickup(%this,%obj,%amount)
{
// This method is called to pickup an object and add it
// to the inventory. The datablock onPickup method is actually
// responsible for doing all the work, including incrementing
// the inventory.
%data = %obj.getDatablock();
%flagnum = strReplace(%data.image.item, "Flag", "");
if(%flagnum $= %this.client.team.teamId){
%amount= -5;
echo("its not an enemy flag");
}else{
if(%data.className !$= "Flag"){
// Try and pickup the max if no value was specified
if (%amount $= "")
%amount = %this.maxInventory(%data) - %this.getInventory(%data);
// The datablock does the work...
if (%amount < 0)
%amount = 0;
if (%amount)
return %data.onPickup(%obj,%this,%amount);
return false;
}else{
echo("its an enemy flag");
%amount = 1;
return Flag::onPickup(%data.image.item,%obj,%this,%amount);
}
}
}now teams can't pick up flags that share their team number ex. team1, flag1
#24
I take a flag but when enter in a trigger the flag not set a point....
Remaining to player....
Help please....I dont have a inventory manager....
Help to create a capture flag please...THANKS =)
02/27/2006 (8:45 am)
Hi...I have the problem....I take a flag but when enter in a trigger the flag not set a point....
Remaining to player....
Help please....I dont have a inventory manager....
Help to create a capture flag please...THANKS =)
#25
One small thing, I also based my team selection off exodus resource. To get the team name to display for scoring purposes I had to alter this in teamObjective.cs
to
03/04/2006 (6:19 am)
Great resource!One small thing, I also based my team selection off exodus resource. To get the team name to display for scoring purposes I had to alter this in teamObjective.cs
messageAll( 'MsgObjective', '\c2Team %1 captured a flag! %2 points!', [b]%obj.client.team,[/b]$Game::PointsForCapture );
to
messageAll( 'MsgObjective', '\c2Team %1 captured a flag! %2 points!', [b]%obj.client.team.name,[/b]$Game::PointsForCapture );
#26
05/27/2006 (10:51 am)
This is great thanks
#27
I test all...Help me please...
06/12/2006 (7:16 am)
Never never never.......When my player (in red team) take the "blu team's flag" and return to RED team flag for take a point.....Don't leave the Blue flag.....UffI test all...Help me please...
#28
Fantastic walkthrough!
I love it!
Thanks again,
RealTime
07/31/2006 (5:58 am)
I completed this last night but couldn't post a thank you, as I exhausted myself by playing this! LOL!Fantastic walkthrough!
I love it!
Thanks again,
RealTime
#29
I am wondering how i allow my player to pick witch team he is on?
10/10/2006 (3:10 pm)
This is a great tut!!!!!I am wondering how i allow my player to pick witch team he is on?
#30
12/23/2006 (3:58 pm)
I've had this working for a while now, but with some issues. If the player carrying the flag is killed, the flag decays with the body and doesnt respawn, and I don't think another character can take it off the body. Anyways, anyone know?
#31
generically I would say look at the player class for the appropriate call back, see if the player has a flag and throw it or drop it.
I would recommend getting Dark Horizon's Lore , and look at their scripts. You can see how I did it . Good Luck all
12/23/2006 (4:37 pm)
Well granted I do think this resource could be more comprehensive, but at what point would I be describing a particular game's solution.generically I would say look at the player class for the appropriate call back, see if the player has a flag and throw it or drop it.
I would recommend getting Dark Horizon's Lore , and look at their scripts. You can see how I did it . Good Luck all
#32
01/02/2008 (11:09 am)
How would I go about applying this for TGB?
#33
01/02/2008 (11:14 am)
er well it was not designed for TGB, BUT there are lessons that can be applied, like naming Objects, then refering to the names later to get the object properties.
#34
05/31/2008 (7:31 pm)
I can't find the server/scripts folder. I have no idea where you want me to put this stuff. Do you want us to create these folders such as "server/scripts" and "data/shapes"? Because I downloaded the flag meshes and stuff but I really don't know where the server/scripts and other folders are located. Thanks for any replys.
#35
Like the starter.fps example.
It's located in \examples\starter.fps\
08/14/2008 (9:53 am)
It's a subfolder of your game-folder.Like the starter.fps example.
It's located in \examples\starter.fps\
#36
12/10/2008 (5:40 pm)
This is very good for using its a little hard to understand, its probably because iI messed up on something I don't know but thanks anyway its great!!
#37
12/15/2008 (6:56 pm)
I just took the getting started tutorial of TGE. I wonder where is the PlayerData(LightMaleHumanArmor)? There is no PlayerData(LightMaleHumanArmor) function in my player.cs by default...
#38
12/16/2008 (10:29 am)
The article was written in 2002 when LightMaleHumanArmor was the datablock for the main player. Look in the server/scripts/player.cs to see what the new PlayerData name is
#39
04/08/2009 (8:46 am)
Where did it go?
#40
04/15/2009 (6:07 am)
Hi. i need help. when i go to grab the flag it doesnt get mounted to the player. I set up teams by following exodus tutorials. i put the maxInv[Flag1] = 1; ect. under datablack in player.cs .. i put them under the other maxInv commands. I do not know why the flag is not getting mounted. i would greatly appreciate any help. thanks!
Torque Owner Jon Jorajuria
That did it...ty