Game Development Community

Please help me about how to know Player object name.

by Nutthawut · in Torque Game Engine · 12/12/2006 (11:32 am) · 4 replies

Hello,
I just buy TGE for 4-5 days.
I never know about C++ and TGE before.
I have some problems. I would like to know Player object name.

In Starter.fps, If first computer login by using name "player1"
and secound computer login by using name "player2".

When I go to console mode, and write

echo(nameToID("player1"));

No objectname return.
then I would like to know what is object name of "player1" and "player2"

#1
12/12/2006 (12:55 pm)
@Nutthawut - nameToID() works differently from what you think.

1. Open example\starter.fps\server\scripts\games.cs

2. Find function GameConnection::createPlayer(%this, %spawnPoint)

3. Look at this section of code
if (%this.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!" );
   }

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


%player = new Player() creates the player object, but never gives it a name.

When you type in "player1" or "player2" when joining a server, that is just a text box GUI that stores the name for other uses.

nameToID() works on objects that are given a name when they are created.


Try this:

1. Start a mission
2. Press F11 to open WorldEditor
3. Find an object that has a name that you can see, like "Grass2" or "Sky"
4. Type this into the console: echo(nameToID(Sky));


To get player IDs:
function getClientPlayerId()
{
 for (%c = 0; %c<ServerConnection.getCount(); %c++)
 { 
   %obj = ServerConnection.getObject(%c);
   if (%obj.getClassName() $= "Player")
   { 
     %name = StripMLControlsChars(%obj.getShapeName());
     if (%name $= $Profile::Player::ServerName)
     {
       return %obj.getId();
     }
   }
  }
 return -1;
}
#2
12/12/2006 (3:21 pm)
I just test your functions,It work!,thanks a lots, but I have more some questions.
I guess your function is get Client PlayerId on only one player logon in game(or I wrong?).
If I would like to know Client Player Id that frist player logon, Please tell me how to use your code.
If I would like to know Client Player Id that secound player logon, Please tell me how to use your code.

And When I use console, it show " (0) Unable to find function StripMLControlsChars" on screen.

I am very noob, just start using.
Thanks a lots.
Nutthawut
#3
12/12/2006 (3:52 pm)
Sorry for the typo, it should be StripMLControlChars, not StripMLControlsChars. No "s" after Control.

As for your other questions. . .hmmm. . .there are different ways. I'll show you a simple way which should also server as a good demonstration of object creation and variable/string manipulation:

In example\starter.fps\server\scripts\game.cs, find GameConnection::createPlayer(%this, %spawnpoint) :
//-----------------------------------------------------------------------------
// Make the changes found in [b]BOLD[/b]
//-----------------------------------------------------------------------------
[b]$CurrentPlayerCount = 0;[/b]
function GameConnection::createPlayer(%this, %spawnPoint)
{
   if (%this.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!" );
   }

[b]   %playerName = "player" @ $CurrentPlayerCount;[/b]
   // Create the player object
   %player = new Player([b]%playerName[/b]) {
      dataBlock = PlayerBody;
      client = %this;
   };
   MissionCleanup.add(%player);
  [b] $CurrentPlayerCount++;[/b]

   // Player setup...
  // ... rest of function is the same

Now....if you call nameToID(), pass in "player0" or "player1", and you'll get the ID back.

Hope that helps.
#4
12/12/2006 (10:06 pm)
Perfect!

Thanks a lots.
Nutthawut