|
Senior Member
Join Date: Feb 2019
Location: Turkey
|
05-03-2019
, 10:52
Re: [TF2] Find Player Sticky Count
|
#3
|
Quote:
Originally Posted by Kolapsicle
You can loop all of the server's entities, but if you're going to hammer out calls, tracking entity creation would probably be more efficient.
Entity Loop
Spoiler
PHP Code:
int GetClientEntityCount(int client, const char[] search)
{
if (!IsValidClient(client))
{
return 0;
}
int count;
char classname[64];
for (int i = MaxClients; i < GetMaxEntities(); i++)
{
if (!IsValidEntity(i))
{
continue;
}
GetEntityClassname(i, classname, sizeof(classname));
if (!StrEqual(search, classname))
{
continue;
}
int owner = GetEntPropEnt(i, Prop_Data, "m_hOwnerEntity");
if (owner != client)
{
continue;
}
count++;
}
return count;
}
bool IsValidClient(int client)
{
return 0 < client <= MaxClients && IsClientInGame(client);
}
Creation/Destruction Tracking
Spoiler
PHP Code:
char gEntity[] = "smokegrenade_projectile";
int gEntityCount[MAXPLAYERS + 1];
public void OnClientConnected(int client)
{
gEntityCount[client] = 0;
}
public void OnEntityCreated(int entity, const char[] classname)
{
if (!IsValidEntity(entity))
{
return;
}
if (!StrEqual(gEntity, classname))
{
return;
}
// Entity owner is invalid until the next game frame.
RequestFrame(EntityOwnerFrame, EntIndexToEntRef(entity));
}
void EntityOwnerFrame(int ref)
{
int entity = EntRefToEntIndex(ref);
if (!IsValidEntity(entity))
{
return;
}
int owner = GetEntPropEnt(entity, Prop_Data, "m_hOwnerEntity");
if (!IsValidClient(owner))
{
return;
}
gEntityCount[owner]++;
}
public void OnEntityDestroyed(int entity)
{
if (!IsValidEntity(entity))
{
return;
}
char classname[64];
GetEntityClassname(entity, classname, sizeof(classname));
if (!StrEqual(gEntity, classname))
{
return;
}
int owner = GetEntPropEnt(entity, Prop_Data, "m_hOwnerEntity");
if (!IsValidClient(owner))
{
return;
}
if (gEntityCount[owner] < 1)
{
return;
}
gEntityCount[owner]--;
}
bool IsValidClient(int client)
{
return 0 < client <= MaxClients && IsClientInGame(client);
}
|
Thank You!
|
|
|
|