Event on Mob health reaching a certain threshold

Is it possible to script an event to happen when a mobs health reaches say 75%, 50% then 25%? I’m pretty sure this is possible because the head jailer’s henchman in the prison in the OC gives up when you get him near death. Maybe I should go check that guy out 1st, lol.

psudocode:

When (health < 75%)
do something

when(health < 50%)
do something else

when (health <25%)
do some more

fleshing it in a bit …

// 'od_test'
/*
    OnDamaged event handler. Runs code depending on how hurt the creature is.

    Note that big hits can skip blocks.
*/

void main()
{
    object oSelf = OBJECT_SELF;

    int iHpPct = GetCurrentHitPoints(oSelf) * 100 / GetMaxHitPoints(oSelf);

    if (iHpPct < 25)
    {
        //
    }
    else if (iHpPct < 50)
    {
        //
    }
    else if (iHpPct < 75)
    {
        //
    }
}
3 Likes

Thank you, that was way more simpler than what Bioware did. Bioware had created a UserDefinedEvent, and it’s giving me a headache trying to read all the documentation on that, lol.

Deleted - too early in the day for me to math properly :wink:

1 Like

That’s exactly what Kevl did.

My bad, I was editing as you typed. It didn’t click at first why he was multiplying by 100.

1 Like

@thejuice027

about the UserDefinedEvent(s) … the code-blocks of the ud are fired by “signalling”. The signal usually comes from the regular AI event-scripts; the OnDamaged script, eg, should have a block like

if (GetSpawnInCondition(NW_FLAG_DAMAGED_EVENT))
{
    SignalEvent(oSelf, EventUserDefined(EVENT_DAMAGED));
}

The condition GetSpawnInCondition() in ‘x0_i0_spawncond’ is set by the OnSpawn handler (usually, it can be set anywhere via script). It’s just a bitflag in a local_int on the creature. If set, the corresponding code in the ud-script fires also, when the regular OnDamaged script runs.

enabling the UD means that, in this case, the OnDamaged script wouldn’t have to be changed. So you’d get all the goodies that’re already happening in the regular script + your custom stuff. As an alternative, if you want to keep the regular OnDamaged code, simply copy and rename the stock OnDamaged script, then insert your custom code. (The signal to the UD-ondamaged code-block can also be kept intact.)

user-defined events provide an additional way to skin the proverbial cat.

 
++coffee

[feline assassin says] we are (proverbially) not amused…

TR

3 Likes

sry bout that T.

i like cats, really

3 Likes

How do you get what’s placed in these “hits” to only fire once? Right now anything you put in there fires every time the creature takes damage <25. So at 26%, 27%, 28%… all the way to <100. So when you get to the <50 and <75 you now have three scripts firing with each hit.

void main()
{
    object oSelf = OBJECT_SELF;

    int iHpPct = GetCurrentHitPoints(oSelf) * 100 / GetMaxHitPoints(oSelf);

    if (iHpPct < 25 && !GetLocalInt(oSelf, "b25"))
    {
        SetLocalInt(oSelf, "b25", TRUE);
    }
    else if (iHpPct < 50 && !GetLocalInt(oSelf, "b50"))
    {
        SetLocalInt(oSelf, "b50", TRUE);
    }
    else if (iHpPct < 75 && !GetLocalInt(oSelf, "b75"))
    {
        SetLocalInt(oSelf, "b75", TRUE);
    }
}

if the if/else statements are setup correctly (depends on how you want things) only 1 condition should fire …

1 Like

Thanks, got this to work! Or is this overkill?

void main()
{
object oSelf = OBJECT_SELF;

int iHpPct = GetCurrentHitPoints(oSelf) * 100 / GetMaxHitPoints(oSelf);

if (iHpPct < 25 && !GetLocalInt(oSelf, "b25"))
{
    SetLocalInt(oSelf, "b25", TRUE);
    if ( GetLocalInt(oSelf, "b25") )
    {
    // stuff happens
    }
}

else if (iHpPct < 50 && !GetLocalInt(oSelf, "b50"))
{
    SetLocalInt(oSelf, "b50", TRUE);
    if ( GetLocalInt(oSelf, "b50") )
    {
    // stuff happens
    }
}

else if (iHpPct < 75 && !GetLocalInt(oSelf, "b75"))
{
    SetLocalInt(oSelf, "b75", TRUE);
    if ( GetLocalInt(oSelf, "b75") )
    {
    // stuff happens
    }
}

}

its overkill …

    SetLocalInt(oSelf, "b25", TRUE);
    if ( GetLocalInt(oSelf, "b25") )

since ‘b25’ has just been set true, it IS true on the next line (ie, nothing changed it between one line and the next).

1 Like