Skip to content

Seeking Help with OnDisturbed / detecting when an item is stolen.

Hi I'm hoping someone can help me out with a problem I'm having.
I'm trying to implement a creature like the Brierhearts from Skyrim, so when a particular item gets stolen from their inventory they die.
I set the spawn condition NW_FLAG_DISTURBED_EVENT (I've tested this with a custom OnSpawn script & the NW_GENERIC_MASTER variable) to trigger the OnUserDefined script and added a switch statement:
void main()
{
  effect eBLESS = EffectVisualEffect(SPELL_BLESS);

  switch (GetUserDefinedEventNumber())
    {
      // Event 1008
      case EVENT_DISTURBED:
        SpeakString("Event #1008 occurred! Inventory changed!");
        ApplyEffectToObject(DURATION_TYPE_INSTANT, eBLESS, OBJECT_SELF);
        break;
    }
}
I have other case statements for other events that trigger fine, but the disturbed event never triggers when the target gets pickpocketed, the code seem to run fine for a container just not npcs/creatures.

If anyone can tell me where I'm going wrong it would be greatly appreciated.

Comments

  • 4BOLTMAIN4BOLTMAIN Member Posts: 90
    If I were going to do this I would use the unacquire script.
  • LonelyRoninLonelyRonin Member Posts: 4
    4BOLTMAIN wrote: »
    If I were going to do this I would use the unacquire script.

    My knowledge of the toolset is old and probably out dated but doesn't the OnUnAcquireItem script only run for PC characters not NPCs ? hence my attempts at an OnDisturbed or OnUserDefined script.
  • 4BOLTMAIN4BOLTMAIN Member Posts: 90
    edited August 2022
    It should work for NPC's. I use this to reduce my shops and donation racks inventory when they get full so I can verify that it does work on placeable objects.

    In the toolset it can be found by clicking Edit, Module Properties and then the Events tab shows the script associated with unacquire.

    I saw this section in the unacquire code
        if (GetModuleSwitchValue(MODULE_SWITCH_ENABLE_TAGBASED_SCRIPTS) == TRUE)
            {
            SetUserDefinedItemEventNumber(X2_ITEM_EVENT_UNACQUIRE);
            int nRet =   ExecuteScriptAndReturnInt(GetUserDefinedItemEventScriptName(oItem), OBJECT_SELF);
            if (nRet == X2_EXECUTE_SCRIPT_END)
                {
                return;
                }
            }
    
  • ProlericProleric Member Posts: 1,316
    OnAcquireItem and OnUnAcquireItem definitely work for all creatures.

    Bear in mind that they are module events, not creature events.

    The ones that only work for PCs are OnPlayerEquipItem and OnPlayerUnEquipItem, which you don't need here.
  • LonelyRoninLonelyRonin Member Posts: 4
    THANK YOU BOTH!

    I have the basic idea working now, I really appreciate the help, thanks.
  • JohnnyTsunamiJohnnyTsunami Member Posts: 6
    THANK YOU BOTH!

    I have the basic idea working now, I really appreciate the help, thanks.

    I have had a few fights with OnDisturbed ( specifically, when a non-pc creature was dropping an item into a placeable inventory ) . I gave up and I use a heartbeat script to check for the item.
    How did you solve your problem?
  • ProlericProleric Member Posts: 1,316
    edited August 2022
    That's potentially a different case. So far in this thread, we've been talking about a creature. The Lexicon tells us that "Creatures fires this event (OnDisturbed) only in case they spotted thief!" so it's necessary to use OnAcquireItem etc

    Dropping an item into a placeable inventory is different. OnDisturbed fires on the placeable. See Lexicon for the functions you use to discover the item, event and actor.
  • LonelyRoninLonelyRonin Member Posts: 4
    How did you solve your problem?

    OnDisturbed can detect both adding and removing from a chest, I thought, its supposed to detect when something is pick-pocketed but it doesn't. Npc inventories aren't treated as containers, as I was my thinking at the time.

    I believe you can use OnDisturbed to detect both INVENTORY_DISTURB_TYPE_ADDED & INVENTORY_DISTURB_TYPE_REMOVED events, INVENTORY_DISTURB_TYPE_STOLEN doesn't seem to apply to containers (from my limited testing).

    OnDisturbed test script of a chest:
        object oItem=GetInventoryDisturbItem();
        object oPC=GetLastDisturbed();
        int nType=GetInventoryDisturbType();
    
        // Debug
        SpeakString("Item: " + GetName(oItem));
        SpeakString("Last disturbed by: " + GetName(oPC));
        SpeakString("Disturbed Type(0=Added, 1=Removed, 2=Stolen): " + IntToString(nType));
    
        switch (nType)
        {
            case INVENTORY_DISTURB_TYPE_ADDED:
                SpeakString(GetName(oItem) + " added to " + GetName(OBJECT_SELF));
                break;
            case INVENTORY_DISTURB_TYPE_REMOVED:
                SpeakString(GetName(oItem) + " removed from " + GetName(OBJECT_SELF));
                break;
            case INVENTORY_DISTURB_TYPE_STOLEN:
                SpeakString(GetName(oItem) + " stolen from " + GetName(OBJECT_SELF));
                break;
        }
    

    The code below is a dirty little proof of concept I did to see if it would suit my purposes.
    //::///////////////////////////////////////////////
    //:: OnItemUnAcquireScript
    //:: mod_unacquire_it
    //:://////////////////////////////////////////////
    /*
        Put into: OnItemUnAcquire Event of the Module
    */
    //:://////////////////////////////////////////////
    //:: Created By: Lonely Ronin
    //:: Created On: August 14, 2022
    //:://////////////////////////////////////////////
    
    void main()
    {
        object oItem        = GetModuleItemLost();
        object oCreature    = GetModuleItemLostBy();
        object oPC          = GetModuleItemAcquiredBy();
    
        effect eDeath       = EffectDeath();
        effect eFX          = EffectVisualEffect(VFX_FNF_SMOKE_PUFF);
        effect ePuke        = EffectVisualEffect(VFX_COM_CHUNK_YELLOW_SMALL);
        effect eKnockdown   = EffectKnockdown();
    
        // for the skeleton
        if (GetTag(oItem) == "SoulGem")
        {
            AssignCommand(oCreature, SpeakString("I don't feel too well!"));
            ApplyEffectToObject(DURATION_TYPE_INSTANT, eFX, oCreature);
            DelayCommand(1.0, AssignCommand(oCreature, ActionPlayAnimation(ANIMATION_LOOPING_PAUSE_DRUNK, 1.0, 10.0)));
            DelayCommand(10.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, eDeath, oCreature));
            GiveXPToCreature(oPC, 25);
        }
    
        // for the old woman
        if (GetTag(oItem) == "Pacemaker")
        {
            AssignCommand(oCreature, SpeakString("Arrg!"));
            DelayCommand(1.0, AssignCommand(oCreature, SpeakString("My heart!")));
            DelayCommand(1.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, eKnockdown, oCreature, 10.0));
            DelayCommand(2.0, AssignCommand(oCreature, SpeakString("::Gasp::")));
            DelayCommand(3.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, eDeath,  oCreature));
            GiveXPToCreature(oPC, 5);
            // not the most peaceful of ends, shame on you!
        }
    
        // for the Bloodsailor
        if (GetTag(oItem) == "BottleofOutdatedSpirits")
        {
            AssignCommand(oCreature, SpeakString("I don't feel too well!"));
            DelayCommand(1.0, AssignCommand(oCreature, ActionPlayAnimation(ANIMATION_LOOPING_PAUSE_DRUNK, 1.0, 20.0)));
            DelayCommand(5.0, AssignCommand(oCreature, SpeakString("Think I'm gonna puke!")));
            DelayCommand(10.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, ePuke,   oCreature));
            DelayCommand(20.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, eDeath,  oCreature));
            GiveXPToCreature(oPC, 25);
        }
    }
    


  • JohnnyTsunamiJohnnyTsunami Member Posts: 6
    Proleric wrote: »
    That's potentially a different case. So far in this thread, we've been talking about a creature. The Lexicon tells us that "Creatures fires this event (OnDisturbed) only in case they spotted thief!" so it's necessary to use OnAcquireItem etc

    Dropping an item into a placeable inventory is different. OnDisturbed fires on the placeable. See Lexicon for the functions you use to discover the item, event and actor.

    Proleric - thanks for your comment. And you're right, I am working on a placeable inventory. LonelyRonin thanks for pasting your test. Thanks to both of yous for the inspiration. I will take another look at OnDisturbed.
  • JohnnyTsunamiJohnnyTsunami Member Posts: 6
    To conclude my case, I have both creatures and pc's dropping items into a placeable's inventory.
    I was hoping to use the OnDisturbed event to manage the inventories comings and goings. It seems that event only triggers for player characters ( and not for all creatures ).
  • ProlericProleric Member Posts: 1,316
    You can use the OnDisturbed event of the placeable instead.
Sign In or Register to comment.