User Defined OnHit Cast Spell code

I want to add my custom script to this system script: x2_s3_onhitcast (let’s you add a custom script for onhit event). If anyone knows where I fill my custom script name in…or if additional code is needed, please fill in an example below:

x2_s3_onhitcast

//::///////////////////////////////////////////////
//:: User Defined OnHitCastSpell code
//:: x2_s3_onhitcast
//:: Copyright (c) 2003 Bioware Corp.
//:://////////////////////////////////////////////
/*
This file can hold your module specific
OnHitCastSpell definitions

How to use:
- Add the Item Property OnHitCastSpell: UniquePower (OnHit)
- Add code to this spellscript (see below)

WARNING!
This item property can be a major performance hog when used
extensively in a multi player module. Especially in higher
levels, with each player having multiple attacks, having numerous
of OnHitCastSpell items in your module this can be a problem.

It is always a good idea to keep any code in this script as
optimized as possible.

*/
//:://////////////////////////////////////////////
//:: Created By: Georg Zoeller
//:: Created On: 2003-07-22
//:://////////////////////////////////////////////

#include “x2_inc_switches”

void main()
{

object oItem; // The item casting triggering this spellscript
object oSpellTarget; // On a weapon: The one being hit. On an armor: The one hitting the armor
object oSpellOrigin; // On a weapon: The one wielding the weapon. On an armor: The one wearing an armor

// fill the variables
oSpellOrigin = OBJECT_SELF;
oSpellTarget = GetSpellTargetObject();
oItem = GetSpellCastItem();

if (GetIsObjectValid(oItem))
{
// * Generic Item Script Execution Code
// * If MODULE_SWITCH_EXECUTE_TAGBASED_SCRIPTS is set to TRUE on the module,
// * it will execute a script that has the same name as the item’s tag
// * inside this script you can manage scripts for all events by checking against
// * GetUserDefinedItemEventNumber(). See x2_it_example.nss
if (GetModuleSwitchValue(MODULE_SWITCH_ENABLE_TAGBASED_SCRIPTS) == TRUE)
{
SetUserDefinedItemEventNumber(X2_ITEM_EVENT_ONHITCAST);
int nRet = ExecuteScriptAndReturnInt(GetUserDefinedItemEventScriptName(oItem),OBJECT_SELF);
if (nRet == X2_EXECUTE_SCRIPT_END)
{
return;
}
}
}
}

If your script name is the same as the item tag, all you have to do is enable tag-based scripting OnModuleLoad. If that’s not clear, see the tag-based scripting tutorial in the Lexicon.

Otherwise add something like this before the penultimate }

if (GetTag(oItem) == "item_tag")
{
  SetUserDefinedItemEventNumber(X2_ITEM_EVENT_ONHITCAST);
  ExecuteScript("script_name", OBJECT_SELF);
}

P.S. Best to format scripts as a bbcode [ code ] block on this site, so we can read the indentation.

TBS already enabled Prol, but thanks for this, will insert and see if it works!

If TBS is enabled, you don’t need my code.

Actually I did nothing but test that prior to posting here and it never worked. Giving the green check since it finally did work, when I added your code. It is NWN2, so this result is not surprising to me…lol! One remaining issue is, on hit cast spell only seems to work on melee weapons, though I have found no posts saying it doesn’t work on throwing weapons (axes in my case). I couldn’t get it to work on a throwing axe, after extensive testing with and without your code. Does it just not work on throwing weapons or does it require a different code (I see those listed as ammunition types in the 2da files)? Overall I am trying to create a boomerang weapon. I am okay if I have to do that with a melee weapon (a kukri would definitely work), as then a script to make it disappear and then reappear, (like the throwing axe does), would be acceptable.

Sorry, didn’t know this was NWN2, which I can’t comment on.

Moved the thread to NWN2.

NP…lol! Many of my issues have been solved by NWN1 people (I’ve found the scripting to be almost completely interchangeable).

Well, the basic scripting is most probably interchangeable. It should be easy to do something OnHit with some trivial lines of script.

But there is another question. If you return a thrown weapon to the PC with the OnHit function, what will you do if he/she misses the target? There is no OnMiss event …

1 Like

Somewhat similar in this mod, if the PC misses, the script puts a new instance of the thrown weapon on the ground near the target.
https://neverwintervault.org/project/nwn2/module/last-danaan

Yes, that could work. Doing that on onunacquire? But I still have some “what if’s”.

I have that mod…one of the NWN2 OG’s! Will definitely open it up and take a look at the scripting.

1 Like