Raised This Month: $32 Target: $400
 8% 

Help, how to use EntIndexToEntRef?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
rekcah
Member
Join Date: Jun 2019
Old 09-10-2019 , 20:09   Help, how to use EntIndexToEntRef?
Reply With Quote #1

Ive written my own version of the l4d2 portals plugin, when moving portals around sometimes it breaks and instead of moving the portals something else moves instead .

A friend suggested that to fix this i need to convert the portal to an entityreferance then back to entityindex.

I found the commands EntIndexToEntRef and EntRefToEntIndex, im new to sourcemod and im not sure how to properly use them, can anyone give me an example of how to use them.
rekcah is offline
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
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 22:02.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode