Solved Count killed enemies (?)

Question that is answered or resolved.

Crimsondeath

Active member
Hi again, I'm remaking the second bonus stage of Neon Lightning Force, so I'm having a little trouble with the enemy killed counter.

It doesn't update when an enemy is killed x.x:

Here are the code lines I'm using:
update.c
C++:
void main(){
    setglobalvar("language", 1);

    setglobalvar("killedP1", 0);
    setglobalvar("killedP2", 0);

    drawstring(22, 226, 0, "x "+getglobalvar("killedP1"));
    drawsprite(loadsprite("data/bgs/bonus-2/iconbonus2.gif"), 2, 222, 999, 1);

    drawstring(291, 226, 0, "x "+getglobalvar("killedP2"));
    drawsprite(loadsprite("data/bgs/bonus-2/iconbonus2.gif"), 271, 222, 999, 1);

}

Ondeathscript for the biker enemies:
C++:
void main()
{
    void self = getlocalvar("self");
    void Pattacker = getlocalvar("other");

    void Player1 = getplayerproperty(0, "entity");
    void Player2 = getplayerproperty(1, "entity");

    int P1BonusScore = getglobalvar("killedP1");
    int P2BonusScore = getglobalvar("killedP2");

    if(Pattacker == Player1){
        P1BonusScore = P1BonusScore + 1;
        setglobalvar("killedP1", P1BonusScore); /* <-- This is suppose to increase the counter for player 1. */
    }
    if(Pattacker == Player2){
        P2BonusScore = P2BonusScore + 1;
        setglobalvar("killedP2", P2BonusScore);
    }
}

So I don't know what I'm doing wrong :/ .
 
Solution
I am using this script by @Kratus as takedamagescriopt and I have no issue with that:

C-like:
void main()
{
koCount();
}
void koCount(int ondoattack)
{//Count each enemy killed, chaning player variable according to playerindex
    void self    = getlocalvar("self");
    void sType    = getentityproperty(self,"subtype");
    int health    = getentityproperty(self,"health");
    int dead    = getentityproperty(self,"dead");

    //EXCEPTION USED FOR "MOTO_REPEAT" ONLY, ONCE HE DON'T HAVE TAKEDAMAGE EVENT
    if(sType == openborconstant("SUBTYPE_BIKER")){health = 0;}

    //CHECK IF THE CHARACTER IS DEAD
    if(health <= 0 || dead){
        void target    = getlocalvar("attacker");
        void parent    =...
Hi again, I'm remaking the second bonus stage of Neon Lightning Force, so I'm having a little trouble with the enemy killed counter.

It doesn't update when an enemy is killed x.x:

Here are the code lines I'm using:
update.c
C++:
void main(){
    setglobalvar("language", 1);

    setglobalvar("killedP1", 0);
    setglobalvar("killedP2", 0);

    drawstring(22, 226, 0, "x "+getglobalvar("killedP1"));
    drawsprite(loadsprite("data/bgs/bonus-2/iconbonus2.gif"), 2, 222, 999, 1);

    drawstring(291, 226, 0, "x "+getglobalvar("killedP2"));
    drawsprite(loadsprite("data/bgs/bonus-2/iconbonus2.gif"), 271, 222, 999, 1);

}

Ondeathscript for the biker enemies:
C++:
void main()
{
    void self = getlocalvar("self");
    void Pattacker = getlocalvar("other");

    void Player1 = getplayerproperty(0, "entity");
    void Player2 = getplayerproperty(1, "entity");

    int P1BonusScore = getglobalvar("killedP1");
    int P2BonusScore = getglobalvar("killedP2");

    if(Pattacker == Player1){
        P1BonusScore = P1BonusScore + 1;
        setglobalvar("killedP1", P1BonusScore); /* <-- This is suppose to increase the counter for player 1. */
    }
    if(Pattacker == Player2){
        P2BonusScore = P2BonusScore + 1;
        setglobalvar("killedP2", P2BonusScore);
    }
}

So I don't know what I'm doing wrong :/ .

@Crimsondeath,

Because until they have a value assigned, variables are NULL, and NULL + 1 is still NULL. You have to make sure variables are initialized to an integer before you can do math on them.

C:
...
int P1BonusScore = getglobalvar("killedP1");
int P2BonusScore = getglobalvar("killedP2");

// If the globals were empty, we need to set 0 value here.
if(!P1BonusScore){ P1BonusScore = 0; }
if(!P2BonusScore){ P2BonusScore = 0; }

if(Pattacker == Player1){
        P1BonusScore = P1BonusScore + 1;
        setglobalvar("killedP1", P1BonusScore); /* <-- This is suppose to increase the counter for player 1. */
...
 
I think this is the problem:
void main(){
setglobalvar("language", 1);

setglobalvar("killedP1", 0); // counter reset
setglobalvar("killedP2", 0); // counter reset

drawstring(22, 226, 0, "x "+getglobalvar("killedP1"));
drawsprite(loadsprite("data/bgs/bonus-2/iconbonus2.gif"), 2, 222, 999, 1);

drawstring(291, 226, 0, "x "+getglobalvar("killedP2"));
drawsprite(loadsprite("data/bgs/bonus-2/iconbonus2.gif"), 271, 222, 999, 1);

}

You reset both counters using update.c in main() which means both counters are resetted everytime. It's best to reset this when level starts or at least in main menu. If you want to carry the counter to next levels, then do the reset in main menu.
 
Hi Thanks for the quick reply :D

Edit: Ok perfect! I found where to put the script, but there's some little errors too, I'll be posting or editing this comment in a few minutes.

Edit 2:
Ok looks like the counter is increasing at killing enemies, but now the problem is that only Player 2 counter is increased even if Player 1 is the killer x.x:

kOXPs1Q.jpeg


Here is the code, it's used in every ondeathscript enemy of that bonus stage:
C++:
void main()
{
    void self = getlocalvar("self");
    void Pattacker = getlocalvar("other");
    log("Attacker: "+Pattacker+"\n");

    void Player1 = getplayerproperty(0, "entity");
    void Player2 = getplayerproperty(1, "entity");

    int P1BonusScore = getglobalvar("killedP1");
    int P2BonusScore = getglobalvar("killedP2");

    if(!P1BonusScore){ P1BonusScore = 0; }
    if(!P2BonusScore){ P2BonusScore = 0; }

    if(Pattacker == Player1){
        log("Attacker 1: "+Pattacker+"\n");
        P1BonusScore = P1BonusScore + 1;
        setglobalvar("killedP1", P1BonusScore);
    }
    if(Pattacker == Player2){
        log("Attacker 2: "+Pattacker+"\n");
        P2BonusScore = P2BonusScore + 1;
        setglobalvar("killedP2", P2BonusScore);
    }
}

I also used the "log()" function and throws me empty values (?):

Code:
Attacker: <VT_EMPTY>   Unitialized
Attacker 2: <VT_EMPTY>   Unitialized
Attacker: <VT_EMPTY>   Unitialized
Attacker 2: <VT_EMPTY>   Unitialized
 

Attachments

Last edited:
I think the problem here is because enemies are killed with player's shots instead of direct attacks. Therefore, the former couldn't acquire who owns the shots which kills them.

To solve this, player need to inform their shots who their owner is. Then ondeathscript acquires the owner after acquiring the shot to find which player fired the shot. If the shot has no owner, then neither counter would be incremented.
 
To solve this, player need to inform their shots who their owner is.

The engine already does with a native property, assuming you are using the native projectile spawn functions.

It's the "owner" entity property, and it is populated with the projectile's owner entity pointer on spawn.

DC
 
I am using this script by @Kratus as takedamagescriopt and I have no issue with that:

C-like:
void main()
{
koCount();
}
void koCount(int ondoattack)
{//Count each enemy killed, chaning player variable according to playerindex
    void self    = getlocalvar("self");
    void sType    = getentityproperty(self,"subtype");
    int health    = getentityproperty(self,"health");
    int dead    = getentityproperty(self,"dead");

    //EXCEPTION USED FOR "MOTO_REPEAT" ONLY, ONCE HE DON'T HAVE TAKEDAMAGE EVENT
    if(sType == openborconstant("SUBTYPE_BIKER")){health = 0;}

    //CHECK IF THE CHARACTER IS DEAD
    if(health <= 0 || dead){
        void target    = getlocalvar("attacker");
        void parent    = getentityproperty(target,"parent");
        void vType    = getentityproperty(self,"type");
        void eType    = getentityproperty(target,"type");
        void sType    = getentityproperty(target,"subtype");
        void pType    = getentityproperty(parent,"type");
        int add        = 1;
        int pIndex;
        int score;
        
        if(ondoattack){target = getlocalvar("other");}

        if(vType == openborconstant("TYPE_ENEMY")){
            if(parent == NULL()){
                if(eType == openborconstant("TYPE_PLAYER")){
                    pIndex    = getentityproperty(target,"playerindex");
                    setglobalvar("ko"+pIndex, getglobalvar("ko"+pIndex)+add);
                    score = getglobalvar("ko"+pIndex);
                    settextobj(1, 240,  224, 2, 999999999, score, 300+openborvariant("elapsed_time"));
                }
            }

            if(parent != NULL()){
                if(sType != openborconstant("SUBTYPE_FOLLOW") && pType == openborconstant("TYPE_PLAYER")){
                    pIndex = getentityproperty(parent,"playerindex");
                    setglobalvar("ko"+pIndex, getglobalvar("ko"+pIndex)+add);
                    score = getglobalvar("ko"+pIndex);
                    settextobj(1, 240,  224, 2, 999999999, score, 300+openborvariant("elapsed_time"));
                }
            }
        }
    }
}

Just remember to reset your variables on your endlevel.c

C-like:
void main()
{
    setglobalvar("ko0",NULL());
    setglobalvar("ko1",NULL());
    setglobalvar("ko2",NULL());
    setglobalvar("ko3",NULL());
}
 
Solution
I am using this script by @Kratus as takedamagescriopt and I have no issue with that:

C-like:
void main()
{
koCount();
}
void koCount(int ondoattack)
{//Count each enemy killed, chaning player variable according to playerindex
    void self    = getlocalvar("self");
    void sType    = getentityproperty(self,"subtype");
    int health    = getentityproperty(self,"health");
    int dead    = getentityproperty(self,"dead");

    //EXCEPTION USED FOR "MOTO_REPEAT" ONLY, ONCE HE DON'T HAVE TAKEDAMAGE EVENT
    if(sType == openborconstant("SUBTYPE_BIKER")){health = 0;}

    //CHECK IF THE CHARACTER IS DEAD
    if(health <= 0 || dead){
        void target    = getlocalvar("attacker");
        void parent    = getentityproperty(target,"parent");
        void vType    = getentityproperty(self,"type");
        void eType    = getentityproperty(target,"type");
        void sType    = getentityproperty(target,"subtype");
        void pType    = getentityproperty(parent,"type");
        int add        = 1;
        int pIndex;
        int score;
       
        if(ondoattack){target = getlocalvar("other");}

        if(vType == openborconstant("TYPE_ENEMY")){
            if(parent == NULL()){
                if(eType == openborconstant("TYPE_PLAYER")){
                    pIndex    = getentityproperty(target,"playerindex");
                    setglobalvar("ko"+pIndex, getglobalvar("ko"+pIndex)+add);
                    score = getglobalvar("ko"+pIndex);
                    settextobj(1, 240,  224, 2, 999999999, score, 300+openborvariant("elapsed_time"));
                }
            }

            if(parent != NULL()){
                if(sType != openborconstant("SUBTYPE_FOLLOW") && pType == openborconstant("TYPE_PLAYER")){
                    pIndex = getentityproperty(parent,"playerindex");
                    setglobalvar("ko"+pIndex, getglobalvar("ko"+pIndex)+add);
                    score = getglobalvar("ko"+pIndex);
                    settextobj(1, 240,  224, 2, 999999999, score, 300+openborvariant("elapsed_time"));
                }
            }
        }
    }
}

Just remember to reset your variables on your endlevel.c

C-like:
void main()
{
    setglobalvar("ko0",NULL());
    setglobalvar("ko1",NULL());
    setglobalvar("ko2",NULL());
    setglobalvar("ko3",NULL());
}
Sorry the late reply,
This is what I needed, It's working great now :D .

Just one more issue (that is present in the original game too x.x), is when the player 2 joins to the game, while we playing the bonus stage, that Shutdown Openbor but the log didn't shows me the error.
 

Attachments

Back
Top Bottom