Morrowind Mod:Storing NPC related data

The UESPWiki – Your source for The Elder Scrolls since 1995
Jump to: navigation, search

Sometimes a mod needs to store some NPC related data. The simplest way to do it is to use global or local variables. And it works fine if your mod affects only a couple of NPCs. But what if you want to do something like making NPCs remember the day they met PC so you can use it in dialogue? This can be done by adding an item to NPCs' inventory. The item should be a light with no "can carry" flag so that player can not steal or loot it. But unfortunately NPCs equip at night any light they have and when they equip a light that has no "can carry" flag they look like they are holding an invisible torch. The good news is that there is a simple workaround for this bug. The following script adds "dummy_light" object to NPCs ( the number of copies added is the day of the month they meet PC ) and makes sure they don't equip it. The script uses MWSE functions so it has to be compiled in MWedit.

begin example_script

short state
short count
short workaround

If ( state == 0 )
        If ( GetDistance Player <= 256 )
                return
        endif
endif

if ( reload_detector->getscale < 3.0 ) ; reload_detector is a persistent item reference
        reload_detector->setscale 3.0
        set workaround to 1             
endif 

ifx ( state )
else
        set count to day
        xadditem "dummy_light" count ; additem does not accept variables
        set state to 1
endif

ifx ( workaround )
        if ( getInterior == 1 )
                set workaround to 0     ; NPCs don't equip lights in interior cells
                return
        endif
        if ( gamehour < 20.15 ) ; NPCs equip lights only when gamehour is between 20 and 20.15
                return
        elseif ( gamehour > 7.5 )
                return
        endif
        set count to getitemcount "dummy_light"
        xremoveitem "dummy_light" count
        xadditem "dummy_light" count ; after this NPCs will never equip dummy_light again unless game is reloaded
        set workaround to 0
endif

end