AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved [l4d1] Problem finding an entity by classname and targetname (https://forums.alliedmods.net/showthread.php?t=324495)

axelnieves2012 05-17-2020 19:44

[l4d1] Problem finding an entity by classname and targetname
 
Hello,

Im working with multiple entities, I scan all of them and I work with specific entities I need.
I filter them by classname and targetname when needed but this method is not working always.
I have no problem finding "func_door" and "func_button".
But I am having problems finding "logic_relay" and "prop_dynamic".

About prop_dynamic, I solved it using: GetEntPropString(entity, Prop_Data, "m_iName", targetname, sizeof(targetname));

PHP Code:

char classname[32], targetname[32];
    
int entity;
//scan all entities...
    
int count GetEntityCount();
    for ( 
entity=1entity<=countentity++ )
    {
        if ( 
IsValidEntity(entity) )
        {
            
GetEntityClassname(entityclassnamesizeof(classname));
            
Entity_GetTargetName(entitytargetnamesizeof(targetname));
            if ( 
strlen(targetname)==)
                
GetEntPropString(entityProp_Data"m_iName"targetnamesizeof(targetname));
        }
        else
            continue;
        
        if ( 
strcmp(classname"logic_relay")==)
        {
            
PrintToServer("STEP 0/4: logic_relay OK - - - - - - - - - - - - - - - - -");
            
GetEntPropString(entityProp_Data"m_iName"targetnamesizeof(targetname));
            if ( 
strcmp(targetname"elevator_bottom_relay")==)
            {
                
//lock doors temporary, so they dont open automatically...
                
SetVariantString("OnTrigger door_elev*:Lock::1.0:-1");
                
AcceptEntityInput(entity"AddOutput");
                
PrintToServer("STEP 1/4: elevator_bottom_relay OK - - - - - - - - - - - - - - - - -");
            }
        }
        else if ( 
strcmp(classname"func_door")==)
        {    
            
//open doors whenever they try to close...
            
SetVariantString("OnClose !self:Open::1.8:-1");
            
AcceptEntityInput(entity"AddOutput");
            
            
//prepare a horde/special infected...
            
HookSingleEntityOutput(entity"OnOpen"OnOpenFirstTimetrue);
            
PrintToServer("STEP 2/4: func_door OK - - - - - - - - - - - - - - - - -");
        }
        
//------------------------------------------------------------------------------------------------------
        
else if ( strcmp(classname"func_button")==)
        {
            if ( 
strcmp(classname"elevator_button")==)
                continue;
            
            
//we need to respawn button to make it usable again...
            //but we will Lock it to prevent firing extra panic events...
            
AcceptEntityInput(entity"Lock");
            
DispatchKeyValue(entity"wait""-1");
            
DispatchSpawn(entity);
            
            
//unlock doors...
            
SetVariantString("OnUseLocked door_elev*:Unlock::0.0:-1");
            
AcceptEntityInput(entity"AddOutput");
            
            
//open doors manually...
            
SetVariantString("OnUseLocked door_elev*:Open::0.3:-1");
            
AcceptEntityInput(entity"AddOutput");
            
PrintToServer("STEP 3/4: func_button OK - - - - - - - - - - - - - - - - -");
        }
        
//------------------------------------------------------------------------------------------------------
        
else if ( strcmp(classname"prop_dynamic")==)
        {
            
GetEntPropString(entityProp_Data"m_iName"targetnamesizeof(targetname));
            if ( 
strcmp(targetname"elevator_model")==)
            {
                
RemoveEntity(entity);
                
PrintToServer("STEP 4/4: prop_dynamic::elevator_model OK - - - - - - - - - - - - - - - - -");
            }
            
/*else
            {
                PrintToServer("STEP 4/4 FAIL: TEST: %s - - - - - - - - - - - - - - - - -", test);
            }*/
        
}
        else
            continue;    
    } 

PHP Code:

stock char[] Entity_GetTargetName(int entitychar[] bufferint size)
{
    return 
GetEntPropString(entityProp_Data"m_target"buffersize);


But I am not able to get logic_relay with this method.
I can only get it like this:
PHP Code:

entity INVALID_ENT_REFERENCE;
    while ( (
entity=FindEntityByClassname(entity"logic_relay"))!=INVALID_ENT_REFERENCE )
    {
        
GetEntPropString(entityProp_Data"m_iName"classnamesizeof(classname));
        if ( 
strcmp(classname"elevator_bottom_relay")==)
        {
            
//lock doors temporary, so they dont open automatically...
            
SetVariantString("OnTrigger door_elev*:Lock::1.0:-1");
            
AcceptEntityInput(entity"AddOutput");
            break;
        }
    } 

This is the only way I could find logic_relay. But I don't think it is the best way because I am scanning all entities twice. Does logic_relay have an special prop for classname and targetname?? m_iName didn't work.

NOTE: In this example, m_iName is set for logic_relay. But if I try to find it on above example, m_iName is never set to elevator_bottom_relay

backwards 05-17-2020 19:51

Re: [l4d1] Problem finding an entity by classname and targetname
 
The enumerating all entities yourself is probably failing with "IsValidEntity" on valid entitys. I had this same issue with spawn point entitiys. When you use FindEntityByClassname, you don't have to check if the entity is valid as they are always valid unless INVALID_ENT_REFERENCE is returned. I would suggest to keep using that second method for entitys you can't find via the first method. Unless you are running these functions on game frame or really frequently it shouldn't cause a big issue.

axelnieves2012 05-17-2020 20:15

Re: [l4d1] Problem finding an entity by classname and targetname
 
Quote:

Originally Posted by backwards (Post 2700772)
The enumerating all entities yourself is probably failing with "IsValidEntity" on valid entitys. I had this same issue with spawn point entitiys. When you use FindEntityByClassname, you don't have to check if the entity is valid as they are always valid unless INVALID_ENT_REFERENCE is returned. I would suggest to keep using that second method for entitys you can't find via the first method. Unless you are running these functions on game frame or really frequently it shouldn't cause a big issue.

Nice, thanx

Balimbanana 05-17-2020 20:30

Re: [l4d1] Problem finding an entity by classname and targetname
 
It is because you are using positive entity indexes. Logic entities generally use largeish negative indexes depending on the game it could be for example -2060347252 as a logic_relay. These entities also do return true for IsValidEntity().
Generally if I am trying to find more entities, I would use FindEntityByClassname with wildcards, something like this:
Code:

        entity = INVALID_ENT_REFERENCE;
        while ( (entity=FindEntityByClassname(entity, "logic_*"))!=INVALID_ENT_REFERENCE )
        {
                if (IsValidEntity(entity))
                {
                        //m_iName checks and such
                }
        }

Edit: Same thing can be said about certain game_, env_, filter_, point_, math_, etc. Depending on what you are trying to find, you could potentially enumerate all entities with
FindEntityByClassname(entity, "*"))


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

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