Game Development Community

Using different player classes - datablock/scope problem?

by Mike Stoddart · in Torque Game Engine · 05/24/2002 (1:13 pm) · 2 replies

I have two player scripts; newPlayer.cs and player.cs. They are identical, except they have different datablocks for playerData.

datablock PlayerData(futuristicClass)
datablock PlayerData(fantasyClass)

When I create the player in game.cs using the following code, the player is created properly:

Create the player object
%player = new Player() {
   dataBlock = fantasyClass;
   client = %this;
};

I can then change fantasyClass to futuristicClass and restart the game, and a different player (and model) is used.

But I can't do this:

if (%teamId == 1)
   {
       switch (%classId)
       {
          case 1:
          %player = new Player() {
             dataBlock = futuristicClass;
             client = %this;
          };

          case 2:
          %player = new Player() {
             dataBlock = fantasyClass;
             client = %this;
          };
       }
   }

I get the following error:

Object 'futuristicClass' is not a member of the 'GameBaseData' data block class
fps/server/scripts/game.cs (347): Register object failed for object (null).
Set::add: Object "0" doesn't exist
fps/server/scripts/game.cs (387): Unable to find object: '0' attempting to call
function 'setTransform'
fps/server/scripts/game.cs (388): Unable to find object: '0' attempting to call
function 'setEnergyLevel'
fps/server/scripts/game.cs (389): Unable to find object: '0' attempting to call
function 'setShapeName'
fps/server/scripts/game.cs (392): Unable to find object: '0' attempting to call
function 'getEyeTransform'

Any ideas what is going wrong? The %this variable is still valid. Is there some kind of scope problem happening here with the datablock?

Thanks

#1
05/24/2002 (4:39 pm)
You can't do that, because the %player variable is being created there, and once it leaves the scope of the if function it is destroyed. (I think).
I would recomment to use a method like:

switch teamId {
case 1:
%myclass = "futuristicClass";
case 2:
%myclass = "fantasyClass";
}

%player = new Player()
{
datablock = %myclass;
client %this;
};

Dont take that code as is, but that's the idea of what you should be doing!
Good luck!
#2
05/24/2002 (5:42 pm)
Thanks. I found an alternative method:

$Game::playerClasses[1,1] = futuristicClass;
   $Game::playerClasses[1,2] = fantasyClass;
   $Game::playerClasses[2,1] = fantasyClass;
   $Game::playerClasses[2,2] = futuristicClass;

   // Create the player object
   %player = new Player() {
      dataBlock = $Game::playerClasses[%teamId, %classId];
      client = %this;
   };