This keeps changing
by Jon Jorajuria · in Torque Game Engine · 02/04/2006 (2:27 am) · 1 replies
I have a join team function that refuses to work. This function is part of a game.cs hierarchy where the functions from one cs file overrides the base cs file. With that said, when the function is called %this is no longer the client ID number it gets changed to "game." However if I put the possible input to (%game, %this) the variables function properly, however the function stops working. the following is the relevant code:
From default.cs (basegame)
From default.cs (basegame)
function DefaultGame::activatePackages(%game)
{
activatePackage(DefaultGame);
if(isPackage(%game.class) && %game.class !$= DefaultGame)
activatePackage(%game.class);
}
function DefaultGame::deactivatePackages(%game)
{
if(isPackage(%game.class) && %game.class !$= DefaultGame)
deactivatePackage(%game.class);
deactivatePackage(DefaultGame);
}TDMGame.csfunction TDMGame::joinTeam(%this, %teamid)
{
//We only have 2 teams so if team is greater than 2 or less than 0 its invalid.
if (%teamid > 2 || %teamid < 0)
return false;
//If we already are on that team return.
if (%teamid == %this.team.teamId)
return false;
%this.leaveTeam();
if (%teamid == 1)
%this.team = $Team1;
if (%teamid == 2)
%this.team = $Team2;
Game.spawnPlayer(%this);
MessageAll('MsgClientJoinTeam', '\c2%1 joined the %2 side',
%this.name,
%this.team.name,
%this.team.teamId,
%this,
%this.sendGuid,
%this.score,
// %this.isAiControlled(),
%this.isAdmin,
%this.isSuperAdmin);
echo("JoinTeamThis=" @ %this);
}Can anybody tell me why I am forced to write my function like function TDMGame::joinTeam(%game, %this, %teamid) in order for the variables to work. Also why the function would stop working when the function is written like that. Is there a work around?About the author
Torque 3D Owner Stephane Conde
Welcome to the wonderful world of TorqueScript. I will attempt to give answers to your questions, but I would highly recommend reading through (and making sure you fully understand) the documentation on TorqueScript before proceeding.
When writing something like 'function TDMGame::joinTeam(%game, %this, %teamId)', the information before the '::' represents the 'namespace' that your function belongs to. What the heck does that mean? It means that your function belongs to something greater. It also means that the first variable represents the object you are calling the function on. Now, that probably makes absolutely no sense to you, but let me explain through example.
In the above case, there is no namespace provided, so the first variable is whatever you pass into the function. Here is an example of how you would call this function:
The %client represents an object ID in this case and gets placed in the '%joiningClient' variable, while '0' represents the team ID and gets placed in (you guessed it!) the %teamId variable.
Now, that should be fairly straight-forward... if it isn't, I urge you to go back to the documentation and get the basics 100% clear before proceeding.
In the above case, we now have provided a namespace, so the first variable becomes the object on which we are calling the function. Here is an example of how you would call this function:
The %game represents your actual 'game' object and will get passed into your joinTeam function as the %this variable. Again, %client represents an object ID that will get placed in the '%joiningClient' variable, while '0' represents the team ID and gets placed in the '%teamId' variable.
Namespaces can add some cleanliness to your code and help in the structure of your scripts. I'm sure things still aren't 100% clear for you, but hopefully that has shed some light on the issues you were having.
Cheers,
Stephane