0; I think this is checking all of the players b"> Accessing all players Inventory | Torque Game Engine | Forums | Community | GarageGames.com

Game Development Community

Accessing all players Inventory

by Jonny · in Torque Game Engine · 08/01/2006 (5:12 am) · 6 replies

Is there any way to check all the players inventory for something (in my case a flag)? At the minute Im using a bad hack - defining the player as a global variable then using $player.GetInventory("Flag1") > 0; I think this is checking all of the players but Im not sure, and it seems to cause some problems. I'd be very grateful to anyone who could help me out of this!


Jon

#1
08/01/2006 (5:17 am)
$player is just a global, it does not represent all players. A global can be accessed from anywhere unlike a local variable which is local to the scope of that function.
#2
08/01/2006 (7:08 am)
I know but it seems to work to some extent since all players are defined as $player when they are spawned. Perhaps if I used ClientGroup.getInventory();? Ill try that and post if it works....
#3
08/01/2006 (7:36 am)
That won't do it. You have to call getInventory off the player object so it would look like this, ClientGroup.getObject(0).getInventory(); 0 would get you the client that is also acting as the server. Clients are numbered in the order they joined so obviously higher numbers will get you those clients.

Just do a for loop with ClientGroup.getObject(i).getInventory() and you can check everyone's inventory.
#4
08/01/2006 (1:05 pm)
As mentioned above. Would be something like the following..

function findFlagCarrier()
{
   // Loop through every connected client to the server.
   for(%i = 0; %i < ClientGroup.getCount(); %i++)
   {
      // Get the clients player object.
      %player = ClientGroup.getObject(%i).player;

      // Does the player have a flag? Could be more then one player with  flag.
      // So stuff the player into an array for parsing below.
      if(isObject(%player))
      {
         if(%player.getInventory("Flag1") > 0)
         {
            %found[%i] = %player;
            echo(ClientGroup.getObject(%i).nameBase SPC "has a flag");
         }
      }
   }

   // Now loop through the players that have a flag.
   for(%i = 0; %found[%i] !$=""; %i++)
   {
      %found[%i].doWhatever();
      echo("Doing something to" SPC %found[%i].client.nameBase);
   }
}
#5
08/01/2006 (1:09 pm)
Nice example code.

Quote:
I know but it seems to work to some extent since all players are defined as $player when they are spawned.

They are not. They are defined as %client.player and put in a simGroup/array.
#6
08/01/2006 (3:26 pm)
Thanks very much, I will implement it straight away!

@ Stefan: sorry if I was unclear, I didnt mean in general I meant in my script the player object is given the global $player as it is spawned.