| 
		
			| int Get_Team_Player_Count(int Team) [message #339927] | Wed, 09 July 2008 08:08  |  
			| 
				
				|  |  reborn Messages: 3231
 Registered: September 2004
 Location: uk - london
 
	Karma: 0
 | General (3 Stars) |  |  |  
	| int Get_Team_Player_Count(int Team) seemed to always crash for me, it says it's tested, but I wasn't able to use it. here is the stock version of it: 
 
 int Get_Team_Player_Count(int Team)
{
	int Total = 0;
	GenericSLNode *x = BaseGameObjList->HeadNode;
	while (x)
	{
		GameObject *o = As_SoldierGameObj((GameObject *)x->NodeData);
		if (o && Get_Object_Type(o) == Team)
		{
			Total++;
		}
		x = x->NodeNext;
	}
	return Total;
}
 
 I altered it so that the while loop doesn't try to do Get_Object_Type on the GameObject *, because this is where it seemed to crash. I think you can only use that function on buildings and players, so I'm sure that's why it was crashing...
 
 I changed it to this:
 
 
 int Get_Team_Player_Count(int Team)
{
	int Total = 0;
	GenericSLNode *x = BaseGameObjList->HeadNode;
	while (x)
	{
		GameObject *o = (GameObject *)x->NodeData;
		if (o && Commands->Is_A_Star(o))
		{
			if (Get_Team(Get_Player_ID(o)) == Team)
			{
			Total++;
		}
		}
		x = x->NodeNext;
	}
	return Total;
}
 And I havn't had a crash yet, maybe you'll look into it for 4.0?
   
 
 
 |  
	|  |  | 
	| 
		
			| Re: int Get_Team_Player_Count(int Team) [message #339938 is a reply to message #339927] | Wed, 09 July 2008 09:30   |  
			| 
				
				
					| Genesis2001 Messages: 1397
 Registered: August 2006
 
	Karma: 0
 | General (1 Star) |  |  |  
	| I use this from Hex. ^_^ 
 
 int TeamCount(int Team)
{
	int Count = 0;
	for (GenericSLNode* PlayerIter = PlayerList->HeadNode; (PlayerIter != NULL); PlayerIter = PlayerIter->NodeNext)
	{
		cPlayer *p = (cPlayer *)PlayerIter->NodeData;
		if (p->IsActive && p->PlayerType.Get() == Team)
		{
			Count++;
		}
	}
	return Count;
} |  
	|  |  | 
	|  | 
	|  | 
	| 
		
			| Re: int Get_Team_Player_Count(int Team) [message #340007 is a reply to message #339927] | Wed, 09 July 2008 16:49  |  
			| 
				
				
					|  StealthEye Messages: 2518
 Registered: May 2006
 Location: The Netherlands
 
	Karma: 0
 | General (2 Stars) |   
 |  |  
	| PlayerType is only defined for smart game objects. You should use As_SmartGameObj to test whether you can call GetPlayerType on an object. 
 Hex's solution is cleaner though.
  
 [edit]
 Oh, I see it already does As_SoldierGameObj... It's probably the As_SoldierGameObj call crashing. For TT it calls As_ScriptableGameObj prior to calling As_SoldierGameObj:
 
 
 GameObject *As_SoldierGameObj(GameObject *obj)
{
	if (!Commands->Get_ID(obj) || !obj)
		return 0;
	ScriptableGameObj* o2 = ((BaseGameObj *)obj)->As_ScriptableGameObj();
	if (!o2)
		return 0;
	return (GameObject *)o2->As_SoldierGameObj();
}
 For older versions it probably does not call As_ScriptableGameObj and therefore crash when calling As_SoldierGameObj on a nonscriptable game object.
 
 BlackIntel admin/founder/coder
 Please visit http://www.blackintel.org/
 [Updated on: Wed, 09 July 2008 16:55] Report message to a moderator |  
	|  |  |