Jump to content

NPC's fail to equip items via Papyrus script


Yagichan

Recommended Posts

G'day all,

 

I'm having a little scripting issue. I have an NPC, it intentionally has no default outfit set. My intention is to dynamically adjust it's inventory and it's starting equipment based on which other mods (if any) are installed. My reason for not making this a default outfit is that players, including myself, like to change up what the NPC's wear, and by having it as a default outfit, I would force players to use mods they might not want to use.

 

Unfortunately, everytime the NPC is loaded, the NPC is in their birthday suit. The good news is that the items are in the NPC's inventory.

 

I have attached a very cut down script that illustrates how I am trying to equip this NPC, and I would really appreciate some help in making a version that works.

 

 

Scriptname My_NPC_is_a_nudist extends Actor  
{
 Paired down script to demonstrate the issue.
}

Armor Skyrim_Necklace_of_Dwindling_Magic

Event OnInit()
	Detect_Clothes()
	Get_Dressed()
EndEvent

Function Detect_Clothes()
{
The full version of this function detects various mods at load time and selects armour accordingly.
It then adds the chosen armours to the NPC's inventory.
}
	Skyrim_Necklace_of_Dwindling_Magic = Game.GetFormFromFile(0x000FC063, "Skyrim.esm") as Armor
	Self.AddItem(Skyrim_Necklace_of_Dwindling_Magic, 1, true)
EndFunction

Function Get_Dressed()
{
Put some clothes on the NPC
}
	Self.EquipItem(Skyrim_Necklace_of_Dwindling_Magic, false, true)
EndFunction

 

 

Thanks in advance for any insights you can share.

Link to comment
Share on other sites

My guess is that the issue is trying to call this from OnInit, which is a special case event.

 

from http://www.creationkit.com/OnInit_(Papyrus):

  • Until OnInit has finished running, your script will not receive any events, and other scripts that try to call functions or access properties on your script will be paused until the event finishes. The only exceptions are when the functions are being called from another script inside its OnInit event, or inside a property set function being set by the master file.
    • As a result, it is generally not advisable to fill your OnInit event with a large amount of processes. It is safer and more reliable to allow your OnInit to finish running while only performing minor tasks, and defer other larger activities to an update (as in the example above) or something similar.

So, the attempt to call Self.EquipItem may require references which are not yet available.

 

Consider using RegisterForSingleUpdate in OnInit and firing off your 2 functions from the OnUpdate event.

Link to comment
Share on other sites

After trying your suggestions, I have come up with what I think is a workable solution that doesn't result in an infinite duplication of items. Using OnUpdate didn't work too well, so I went on an OnLoad based approach. It now needs SKSE, but as I need that for other mods, it's a minor issue.

 

An example proof of concept is below.

 

 

 

Scriptname My_NPC_is_a_nudist extends Actor  
{
 Paired down script to demonstrate the issue.
}

Bool  Has_Equiped_Clothes = false
Armor Skyrim_Necklace_of_Dwindling_Magic

Event OnInit()
	Detect_Clothes()
EndEvent

Event OnLoad()
{
This will ensure that the NPC has the selected equipment on when met in game.
The first load will load the "default" outfit, each subsequent load will
equip the armour that was saved in OnUnload
}
	Get_Dressed()
EndEvent

Event OnUnload()
	Amulet = Self.GetWornForm(0x00000020) as Armor
EndEvent

Function Detect_Clothes()
{
The full version of this function detects various mods at load time and selects armour accordingly.
It then adds the chosen armours to the NPC's inventory.
}
	Skyrim_Necklace_of_Dwindling_Magic = Game.GetFormFromFile(0x000FC063, "Skyrim.esm") as Armor
	Self.AddItem(Skyrim_Necklace_of_Dwindling_Magic, 1, true)
EndFunction

Function Get_Dressed()
{
Put some clothes on the NPC
}
If !Has_Equiped_Clothes
	Initial_Dressing()
Else
	Self.EquipItem(Amulet)
EndIf
EndFunction

Function Initial_Dressing()
	Self.EquipItem(Skyrim_Necklace_of_Dwindling_Magic, false, true)
	Has_Equiped_Clothes = true
EndFunction

 

 

Thanks for your advice. If you spot any potential issues in this code, please let me know.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...