View Single Post
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 09-10-2019 , 23:44   Re: Help, how to use EntIndexToEntRef?
Reply With Quote #2

You use them when passing entities to functions. Here's an example from my Mutant Tanks plugin:

PHP Code:
public void OnEntityCreated(int entity, const char[] classname)
{
    if (
g_bPluginEnabled && StrEqual(classname"tank_rock"))
    {
        
// pass the reference of the entity to the timer...
        
CreateTimer(0.1tTimerRockThrowEntIndexToEntRef(entity), TIMER_FLAG_NO_MAPCHANGE);
    }
}

public 
Action tTimerRockThrow(Handle timerint ref)
{
    
// convert the reference back to an index and check if that entity is still there...
    
int iRock EntRefToEntIndex(ref);
    if (!
g_bPluginEnabled || iRock == INVALID_ENT_REFERENCE || !bIsValidEntity(iRock))
    {
        return 
Plugin_Stop;
    }

    
// rest of the code here...

    
return Plugin_Continue;

The reason we pass entities like this is because entities can be changed or deleted anytime between the time of passing them through a function and the time of when we use them in that function.

We do the same thing with clients using GetClientUserId() paired with GetClientOfUserId() or GetClientSerial() paired with GetClientFromSerial():

PHP Code:
public void OnClientPostAdminCheck(int client)
{
    if (!
IsFakeClient(client))
    {
        
CreateTimer(3.0tTimerWelcomeGetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
    }
}

public 
Action tTimerWelcome(Handle timerint userid)
{
    
int client GetClientOfUserId(userid);
    if (!(
client <= MaxClients && IsClientInGame(client) && !IsFakeClient(client)))
    {
        return 
Plugin_Stop;
    }

    
PrintToChat(client"[SM] Welcome %N!"client);

    return 
Plugin_Continue;

__________________
Psyk0tik is offline