Jump to content

MiscItem OnActivate oddity


stormingromans

Recommended Posts

So, I've been working on a mod for a week or so and was just getting ready to release it, when I discovered a strange bug.

 

I have a MiscItem, with a script attached that uses OnActivate to add a level to a perk. I add this to the world, along with others, via leveled lists that are then added to containers. When you take the MiscItem out the container nothing happens - no OnActivate. Drop it on the ground and pick it up - OnActivate fires.

 

Is it supposed to be like this? What event can I use instead?

 

This doesn't seem to be the case with books.

Link to comment
Share on other sites

After a little bit of testing, this seems to be the default behavior for MiscItems. Tested with the bobbleheads and the OnActivate in the BobbleheadPerkScript doesn't fire if the bobblehead is taken from a container. Anyway ... figured out a work around, at least for my use case. You need to use the OnContainerChanged event instead, or as well.

 

In my case, I wan to add a perk when the player first collects the item, from where ever it may be. I also only want this to only happen the first time the player picks up/retrieves the item. So here's the code I came up with:

Scriptname MyActivateTest extends ObjectReference

bool OnlyOnce
Perk Property PerkToAdd Auto Const

Event OnActivate(ObjectReference akActionRef)
  DoStuff("Hello World from OnActivate")
EndEvent

Event OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)
  if akNewContainer == Game.GetPlayer()
    DoStuff("Hello World from OnContainerChanged")
  endIf
endEvent

Function DoStuff(String message)
  if OnlyOnce == false
    game.GetPlayer().AddPerk(PerkToAdd)
    Debug.Notification(message)
    OnlyOnce = true
  endif
EndFunction

Seems to work, although will probably be more efficient if the OnlyOnce check is done before the container check. Just guessing, but an object equality check is usually less peformant than a boolean check - certainly is in C#, so probably is in papyrus too.

 

Hope this helps out anyone else that comes across this gotcha.

Link to comment
Share on other sites

  • 1 month later...
Event OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)
if Game.GetPlayer().HasPerk(PerkToAdd)
  if akNewContainer == Game.GetPlayer()
    DoStuff("Hello World from OnContainerChanged")
  endIf
endif
endEvent

I hope not to misunderstand your purpose. Have you already considered to use HasPerk() boolean function?

Link to comment
Share on other sites

  • Recently Browsing   0 members

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