Check if an entity is not available

O Ilusionista

Captain 100K
Hello my friends.
I have a function that I use to check if a specific entity exists and, if it does, it makes the caller change to a new animation and it works perfectly:
C-like:
void checkEntChangeAnim(char target_name, int Ani)
{// Check if there is a specific entity and make the called to change to a specific animation
//Douglas Baldan / O Ilusionista - 2023.11.25

    void self = getlocalvar("self"); // Get Caller
    int i;
    for (i = 0; i < openborvariant("count_entities"); ++i) // Count available entities and execute an entity enumeration loop
    {
        void ent = getentity(i);
        if ( getentityproperty(ent, "exists") && !getentityproperty(ent, "dead") && getentityproperty(ent, "aiflag", "autokill" ) == 0)// Entity exist and its not dead?
           {
                   if (getentityproperty(ent,"model") == target_name) // Check if the entity model (and not the alias) matches the one you want
            {
                changeentityproperty(self, "animation", openborconstant(Ani)); // Make the caller to change its animation
                continue;
            }
        } else continue;
    }
    return;   
}

The problem is that I want to do the opposite - I want to check if an entity DOES NOT EXIST and, if it does not exist, the animation changes. If it does exist, nothing should happen.

I tried to adapt the code like this (terrible name, I know), but it doesn't work - even though the entity is no longer active, sometimes it works, sometimes it doesn't:
C-like:
void checkEntChangeAnimNot(char target_name, int Ani)
{// Check if there isn't a specific entity and make the called to change to a specific animation
//Douglas Baldan / O Ilusionista - 2025.01.25

    void self = getlocalvar("self"); // Get Caller
    int i;
    for (i = 0; i < openborvariant("count_entities"); ++i) // Count available entities and execute an entity enumeration loop
    {
        void ent = getentity(i);
        if ( getentityproperty(ent, "exists") && !getentityproperty(ent, "dead") && getentityproperty(ent, "aiflag", "autokill" ) == 0)// Entity exist and its not dead?
           {
                   if (getentityproperty(ent,"model") == target_name) // Check if the entity model (and not the alias) matches the one you want
            {
                break;
            }
        } else {
        changeentityproperty(self, "animation", openborconstant(Ani)); // Make the caller to change its animation
        }
    }
    return;   
}

What I am doing wrong?
 
Hello my friends.
I have a function that I use to check if a specific entity exists and, if it does, it makes the caller change to a new animation and it works perfectly:
C-like:
void checkEntChangeAnim(char target_name, int Ani)
{// Check if there is a specific entity and make the called to change to a specific animation
//Douglas Baldan / O Ilusionista - 2023.11.25

    void self = getlocalvar("self"); // Get Caller
    int i;
    for (i = 0; i < openborvariant("count_entities"); ++i) // Count available entities and execute an entity enumeration loop
    {
        void ent = getentity(i);
        if ( getentityproperty(ent, "exists") && !getentityproperty(ent, "dead") && getentityproperty(ent, "aiflag", "autokill" ) == 0)// Entity exist and its not dead?
           {
                   if (getentityproperty(ent,"model") == target_name) // Check if the entity model (and not the alias) matches the one you want
            {
                changeentityproperty(self, "animation", openborconstant(Ani)); // Make the caller to change its animation
                continue;
            }
        } else continue;
    }
    return;
}

The problem is that I want to do the opposite - I want to check if an entity DOES NOT EXIST and, if it does not exist, the animation changes. If it does exist, nothing should happen.

I tried to adapt the code like this (terrible name, I know), but it doesn't work - even though the entity is no longer active, sometimes it works, sometimes it doesn't:
C-like:
void checkEntChangeAnimNot(char target_name, int Ani)
{// Check if there isn't a specific entity and make the called to change to a specific animation
//Douglas Baldan / O Ilusionista - 2025.01.25

    void self = getlocalvar("self"); // Get Caller
    int i;
    for (i = 0; i < openborvariant("count_entities"); ++i) // Count available entities and execute an entity enumeration loop
    {
        void ent = getentity(i);
        if ( getentityproperty(ent, "exists") && !getentityproperty(ent, "dead") && getentityproperty(ent, "aiflag", "autokill" ) == 0)// Entity exist and its not dead?
           {
                   if (getentityproperty(ent,"model") == target_name) // Check if the entity model (and not the alias) matches the one you want
            {
                break;
            }
        } else {
        changeentityproperty(self, "animation", openborconstant(Ani)); // Make the caller to change its animation
        }
    }
    return;
}

What I am doing wrong?

In a situation like this, log() is your friend. I add log traces until eventually I can see everything the code is doing. I also noticed a couple of optimization issues. Try to never run a function in loops unless you have to. The way your code is written, it's running the openborvariant("count_entities") on every loop. There's no need for that. Get it once outside the loop as a variable. Same goes for void ent. You should declare variables outside the loop if possible.

Basically, when writing loops, try to keep everything outside of the loop that you can, and only put things in the loop that have to be refreshed, evaluated or run on every iteration.

C-like:
void checkEntChangeAnimNot(char target_name, int Ani)
{// Check if there isn't a specific entity and make the called to change to a specific animation
//Douglas Baldan / O Ilusionista - 2025.01.25

    void self = getlocalvar("self"); // Get Caller
    int i;
    int count_entities = openborvariant("count_entities");
    void ent = NULL();
    char model_name = "";
 
    log("\n\n checkEntChangeAnimNot( " + target_name + ", " + Ani + ")");
    log("\n\t Caller" + self);
    log("\n\t: " + count_entities);
 
    for (i = 0; i < count_entities; ++i) // Count available entities and execute an entity enumeration loop
    {
        log("\n\t i: " + i);
    
        ent = getentity(i);
    
        log("\n\t ent: " + ent);
    
        if ( getentityproperty(ent, "exists") && !getentityproperty(ent, "dead") && getentityproperty(ent, "aiflag", "autokill" ) == 0)// Entity exist and its not dead?
           {
        
            log("\n\t Valid - ");
            model_name = getentityproperty(ent,"model");       
        
            log("\n\t model_name: " + model_name);
            if (model_name == target_name) // Check if the entity model (and not the alias) matches the one you want
            {
                log("\n\t Matched (should exit here).");
                break;
            }
        
            log("\n\t Not Matchd.");
        
        } else {
        
        log("\n\t Not Valid.");
        
        changeentityproperty(self, "animation", openborconstant(Ani)); // Make the caller to change its animation
        }
    }
    return;
}

Now when I was writing the example with log traces, I think I found your problem. See, if you are wanting to know if there are NOT any entities that match, you're going to have to let the loop finish, and that's your answer. Because that's the only way you will have checked every entity on screen to see if there's a match. Think of it logically. When you are looking to see if "SlimJim" is on screen, then you check each entity looking for "SlimJim". If you find it, you can exit right away.

But if you are looking for "Not SlimJim", you can't stop at an invalid entity or one that doesn't match, because you don't know if any of the entities you haven't checked yet are "SlimJim" or not. You have to check every entity, and only once you look at them all, and the loop is done, do you know for sure there's no "SlimJim".

HTH,
DC
 
Back
Top Bottom