OnHit Cast spell % to armor

How exactly can I make an armor cast a spell at a certain chance of percentage, on hit?
For example, make my robe cast a lvl 5 fireball centered at the wearer at 20% chance on hit.

If anyone would be kind enough to post an example script, it would be tremendously helpful!

“On-hit: cast spell” is triggered with every hit, there’s no way to do what you ask. At least when using pure NWScript (maybe it will be possible to do with NWNX or NWNCX, @Shadooow may know).

It is possible to do without NWNX using a little trick I used here: https://neverwintervault.org/project/nwn1/script/custom-itemproperties-shadooow

basically what I did:

  1. created a new itemproperty for X% chance to cast spell
  2. created a new itemproperty to be a copy of vanilla OnHitCastSpell
  3. made vanilla OnHitCastSpell invisible to players when examining the item
  4. edited the vanilla onhitcastspell script and checked if there is my custom % property, if so, I rolled dice, if failed I returned the script so nothing happened
    thats it, but this requires to mass edit all items with OnHitCastSpell property (not so many in vanilla but maybe you have some custom) and add the copy itemproperty, or not making it invisible for a price of “lower immersion” as player will see both on the item
1 Like

Wouldn’t be possible to Spellhook that? Every spell is a script too, even from onhit itemptobs, or am I wrong?

that might also work and could be better solution, I will look into this myself, my solution is quite complicated indeed

1 Like

You could also just use on hit: unique power and add your code to x2_s3_onhitcast.nss I think.

3 Likes

yeah obviously if you don’t care about “immersion” you don’t need to make new itemproperties just use this and write the real effect into description, it will also work for modules intented to be with no hak packs

Here is a little tutorial on how to do what you want to do using the spell hook system. It includes some examples of what you might want to do with the spell hook system.

//Place this somewhere in your modules on load event script to set the name of your spell

// Place the name of whatever script you want run before any spell is cast.  Use this as a spell
// hook to set any conditions or effects on a spells use, ie components, wild or nul magic, etc.
    SetModuleOverrideSpellscript("TheNameOfYourSpellookScript");

//and set the code below in your spell hook script

#include "x2_inc_switches"
#include "x2_i0_spells"


void main()
{
    object oPC = OBJECT_SELF;
    object oTarget = GetSpellTargetObject();
    object oArea = GetArea(oPC);
    object oSpellCastObject = GetSpellCastItem();
    string sSpellCastObjectTag = GetStringUpperCase(GetTag(oSpellCastObject));
    string sSpellCastObjectResRef = GetResRef(oSpellCastObject);
    int nSpellId = GetSpellId();
    string sSpellId = IntToString(nSpellId);
    int nCharges = GetItemCharges(oSpellCastObject);
    int nCasterLevel = GetCasterLevel(oPC);

    // this is how to override an existing spell script with your own script for an on hit cast event
    // Set the spell id of the spell you want to run..the line number in the spells 2da for the specific spell
    if(nSpellId == 102)
    {
        // set the tag of the object your looking for
        if(sSpellCastObjectTag == "THE_ITEMS_TAG")
        {
            // run your script
            ExecuteScript("yourspellscript", oPC);
            // don't run the default script fot the event
            SetModuleOverrideSpellScriptFinished();
        }
    }
    // this is an example of causing the default spell script for an item not
    // to fire 20% of the time, basically canceling the event
    // Set the spell id of the spell you want to run..the line number in the spells 2da for the specific spell
    else if(nSpellId == 103)
    {
        // set the tag of the object your looking for
        if(sSpellCastObjectTag == "THE_ITEMS_TAG")
        {
            // is a value less than 3 is rolled
            if(d10() <3)
                // cancel the running of the spell default spell script
                SetModuleOverrideSpellScriptFinished();
        }
    }
    // this is an example of running your own spell script instead of the default spell script
    // for the specific spell
    // Set the spell id of the spell you want to run..the line number in the spells 2da for the specific spell
    else if(nSpellId == 104)
    {
        // run your script
        ExecuteScript("yourspellscript", oPC);
        // don't run the default script fot the event
        SetModuleOverrideSpellScriptFinished();
    }
    // this is an example of running your own spell script ia additioon to the default spell script
    // for the specific spell
    // Set the spell id of the spell you want to run..the line number in the spells 2da for the specific spell
    else if(nSpellId == 104)
    {
        // run your script first, the default will runn afterward
        ExecuteScript("yourspellscript", oPC);
    }

}
1 Like