Help with an on death script?

Can anyone give me help with adding something to an NPC’s on death script? I don’t really know how to actually do scripting, I rely on Lilac Soul’s for everything. I can’t figure out how to make it do this:

I want to add 1 to an integer when certain NPCs die, regardless of whether they are killed by the PC or an NPC (second part is essential). Can anyone give me a line of script that can be plugged straight into their normal on death script?

Well, I guess you could just do it like this perhaps:

SetGlobalInt("TheNpcIsDead",1);

Put that on a custom OnDeath script that you put on the OnDeath of the character.

EDIT: Wait…or do you mean you already have an integer that you want to increase the value of?

EDIT2: If you mean you already have an integer that you want to increase the value of it could look something like this (I think):

int nOldValue = GetGlobalInt("TheNPCIsDead"); //Use whatever string has been used before.
	int nNewValue = nOldValue + 1;
	
	SetGlobalInt("TheNPCIsDead", nNewValue);

@andgalf: NWN1 has no SetGlobalInt() function. Go with SetLocalInt() on the module.

You may want to give the whole context. Which object is keeping that integer?

1 Like

Hmm, this is where my lack of understanding of how variables actually function comes in. It is your second one that I’m trying to do: add 1 to a variable each time an NPC with this script dies. But it’s not compiling, and I don’t understand your comment in that script: “use whatever string has been used before.” When would it have been used before? I mean, usually when I use integers it seems the integer is established purely by scripts that reference it. Ie it doesn’t really exist per se. I’ve got other scripts in the module that add 1 to an integer and it seems that they create the integer and add 1 to it.

Sorry, I’m grappling with arcane knowledge that I have no understanding of - thanks for giving me the time!

@NWShacker - Oh, sorry! I thought that was one thing that NWN1 and NWN2 script had in common.

@HidesHisEyes - Sorry, like @NWShacker said one can’t apparently use SetGlobalInt in NWN1. I didn’t know that.
I’ve been modding for NWN2 for 4 years now, and only now have I managed to grasp certain things about scritping, so I know how you feel.
Well, to answer your question: I wondered if someone else, an NPC or something, had died before, and you had established an integer with a name (a string that is, a string is just text really). If that hasn’t happened we need to just create a new one which we can call what you want. I called this variable
TheNPCIsDead, but you can call it what you want.

Then maybe the script could look something like this:

int nOldValue = GetLocalInt(GetModule(),"TheNPCIsDead"); //Use whatever string you like.
	int nNewValue = nOldValue + 1;
	
	SetLocalInt(GetModule(),"TheNPCIsDead", nNewValue);

Hmm. Thanks for your response. It now compiles but testing it, it doesn’t do what I want it to.

For the full context: I’ve put the script on multiple enemy NPCs (six of them) who spawn from a conversation. I want the next conversation to display a particular line only once all six enemies are dead. Even once they’re all dead the conversation still ignores the line I want (the one that checks that integer is 6) and goes on to the next one (which is an “oh no we’re under attack” line).

@andgalf: I have somewhere a list of function differences between the games if you want.

@HidesHisEyes: if all the spawned enemies have the same (but unique in the module) tag, you can check in the conversation whether there is anyone with that tag.

// "Text appears when..." check
int StartingConditional()
{
    // return TRUE if there is at least one enemy alive
    return GetIsObjectValid(GetObjectByTag("ENEMY_TAG"));
    // TRUE means that this convo line will be spoken
}

Change ENEMY_TAG to tag of the spawned enemies. Notice how it doesn’t matter how many are spawned. You don’t need then any special OnDeath logic for them. You just want none of them to exist anymore. If they have different tags, then things get a bit complicated, but still easily doable (CreateObject() can also set tags).

1 Like

@NWShacker Awesome, thank you, I’ll try doing it this way.

@NWShacker - Sure, that could be interesting.

Yes, doing it your way with a starting conditional is way easier since the situation is like HidesHisEyes says.

@andgalf, I’ll PM you when I find it.

EDIT: PM sent. Credit: nwscript of NWN2 was provided by @kevL_s.

@HidesHisEyes, mind that the above script will misbehave when you check “leaves lootable corpse” on the enemies as their objects won’t be actually destroyed on kill.

This will work in all cases:

int StartingConditional()
{
    object oEnemy;
    int iEnemy;

    while(GetIsObjectValid(oEnemy = GetObjectByTag("ENEMY_TAG", iEnemy++)))
    {
        // beware: GetIsDead() always returns FALSE for non-creatures
        if(!GetIsDead(oEnemy))
        {
            // some enemies alive
            return TRUE;
        }
    }

    // all enemies destroyed or dead
    return FALSE;
}

Convo line is again spoken when at least one creature is alive. If you want to reverse the condition (i.e. speak line when they’re all dead), just switch the visible TRUE with FALSE.

1 Like