Skip to content

Casting from object

I am looking for the functions which allows casting a spell from an object. The purpose is when the object is used it checks the PC level and uses that for the caster level when casting the spell.

The problem I am running into is the only commands I see are for placeable objects and not items that the PC is able to carry. Thank you for your time and assistance.

Comments

  • DrSaturnDrSaturn Member Posts: 5
    The best way to do this is via Tag Based Scripting. Create a script with a name the same as the tag of the item.
    The script needs to
    #include "x2_inc_switches"
    
    and to check that the event is X2_ITEM_EVENT_ACTIVATE.
    #include "x2_inc_switches"
    
    void main()
    {
        int nEvent = GetUserDefinedItemEventNumber();
        object oPC;
        object oItem;
    
        switch(nEvent){
           case X2_ITEM_EVENT_ACTIVATE:
           oItem = GetItemActivated();
           DoMySpecialStuff(oItem);
        }
    }
    
    See: https://nwnlexicon.com/index.php?title=Grimlar_-_Introduction_To_Tag_Based_Scripting
  • DrSaturnDrSaturn Member Posts: 5
    Oh and I forgot, you have to ensure tag based scripting is enabled, I think it is by default now.
  • CaomhCaomh Member Posts: 40
    Hi DrSaturn,

    Thank you for responding, however the link you referenced does not help.

    I already have a way to use the an object to run a script. The issue is having a function which will allow me to cast the spell of my choice. The only functions I have found for casting spells do not seem to work for objects but will work for placeables.


  • WilliamDracoWilliamDraco Member Posts: 175
    Placeables are an object - "Object" is about the most generic word for a thing in nwn, so can you please be more specific on what you mean by "objects" - Items? Creatures? Placeables? All of these are objects.

    That aside, you cannot set a caster level by script. You either have to have a valid creature cast the spell from its own spelllist, "cheat-cast" it in which case I believe the spell level is determined by the spell's innate level, or from the Cast Spell item property, which has the item level attached to the property.

    DrSaturn also indicated the way that you can "fake" a spell-cast by implementing it directly as a unique property of an item, by replicating the spell scripts with some adjustment for your determination of caster level that you want. You'd have to manually manage things like spell resistance though.
  • CaomhCaomh Member Posts: 40
    Hi WillamDraco

    In object based scripting everything is an object. When in the original constatations presented I specifically mention the only functions will work for placeboes and not objects the PC is able to carry.

    Lets move down your checklist of objects. Can a PC carry a creature? No then it is not what I am asking for. Can a PC carry a placeable? Again, no which again makes it what I am looking for.

    You referenced what DrSaturn’s answer. He answered something I never asked for. I asked for a method to case a spell for an object that a PC can carry. He provided a script which would allow casting on a placeable which a PC could not carry. As I mentioned in my response to him I already have a functioning way of running code from an object the PC is carrying. So, his response was not helpful.

    Now in programing there are many different ways to achieve a goal. It may be possible there is not a default function to pull up code. However, from what was provided the only useful part that actually related to my question is the following.

    DoMySpellStuff(oItem)

    Had the function (or now days method) been defined it would have answered my question. Thank you however for mentioning that there is now base function for pulling up a PCs level. The work around would be using the function GetXP(). The reasoning would be if a character is level 2 then they would have XP =< 2000. Some pseudo code of creating the function for that would getLevel(), if xp is > 1000 than level is 1, if < 2000 level 2 and so on layered in a way checking for high levels first. This combined with setting the spell and some code for setting the spell level. This is sounding more like nwnx side of things which I am not touching at this time.

    And if anyone made it to the end of this, thank you!

  • FreshLemonBunFreshLemonBun Member Posts: 909
    I think there is some confusion here.

    If you want to use an item in the inventory to cast a spell then calling a script by using the inventory item is how this is done. Then in the script cast the spell or a fake spell.

    Otherwise you can try changing some itemproperty 2das to try and make it available directly in the toolset.
  • DrSaturnDrSaturn Member Posts: 5
    I asked for a method to case a spell for an object that a PC can carry. He provided a script which would allow casting on a placeable which a PC could not carry.

    I did not give you a method for casting from a placeable object. Tag-based scripting works for Items. I hope that helps and clears some confusion.

    Getting a spell cast at a particular level has always been a problem in NWN. The ActionCastSpellAtObject and ActionCastSpellAtLocation functions do not allow you to set the caster level, they take it from the Object to which the action is assigned (unless the bCheat flag is set in which case the level is some innate default level).

    To achieve what you want to do why not AssignCommand the ActionCastSpellAt* to the PC on activation of the item? Then the spell will be cast with that PC's level. Without the bCheat flag though, the spell won't work if the PC has not got the required caster levels. Of course, you could set that flag conditionally on whether the PC has the required levels.
  • AschentAschent Member Posts: 15
    edited November 2020
    He answered something I never asked for. I asked for a method to case a spell for an object that a PC can carry. He provided a script which would allow casting on a placeable which a PC could not carry.

    This is incorrect. Tag-based scripting works for items inside a player's inventory. In fact, even a quick review of the link provided clearly states:

    Lexicon:
    The premise is simple: instead of all the code for all the items having to go in the various events, such as OnActivateItem, these events now just retrieve the Tag from the item and execute a script with the same name as that Tag. As such, carrying an item from one module to another is no more difficult than importing an erf containing the item blueprint, the single script file, and then figuring out how and where to place the item in the game. Oh, and then compiling the module. No existing scripts need to be altered.

    Perhaps you should do some more research before dismissing help. Tag-based scripting uses the item's tag to determine the script that is used (assuming you have the Special Ability Item Property set if that is how you intend to use the system). If this is not what you're looking for, I recommend rewording your request as that is what everyone is currently understanding.

    Also, it appears you want to replicate the radial menu and have it list the available spells that a player has available. This is not possible at this time. I drew this conclusion based on your comment above:
    The issue is having a function which will allow me to cast the spell of my choice.

    If that is not the case, disregard.
  • CaomhCaomh Member Posts: 40
    Aschent wrote: »

    Perhaps you should do some more research before dismissing help. Tag-based scripting uses the item's tag to determine the script that is used (assuming you have the Special Ability Item Property set if that is how you intend to use the system). If this is not what you're looking for, I recommend rewording your request as that is what everyone is currently understanding.

    If that is not the case, disregard.

    Happy New Year,

    I already used tag based scripting. My statement was his solution was not an answer to my question. Perhaps during this new year if you do not like my phrasing you should take your advice from last year and disregard.

    Generally rule, if someone says something is not helpful it is not. Trying to insult them does not make it helpful. It makes you appear like a jerk. I found my own solution.
Sign In or Register to comment.