Jump to content

The good old OnItemRemoved Event


Recommended Posts

So I'm currently working on a script that checks if the player consumes any food item. The script is attached to a quest.

Scriptname AAA_WTFCoreScript extends Quest

Keyword Property KeywordFood Auto Const

Event OnInit()	
        AddInventoryEventFilter(KeywordFood)
	RegisterForRemoteEvent(Game.GetPlayer(), "OnItemRemoved")
EndEvent


Event ObjectReference.OnItemRemoved(ObjectReference akSource, Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akDestContainer)
        debug.MessageBox("OnItemRemoved Event")
EndEvent

The problem I have is that ingame the messagebox doesn't appear as If the event isn't called. According to the creationkit wiki I have to use the InventoryFilter to avoid getting event spamming which can filter by keyword. So I filter out keywords that are attached to the item. My KeywordFood has the keyword "ObjectTypeFood" attached to it.

 

Also in the examples the event is called simply by "Event OnItemRemoved" without the ObjectReference but if I do this the compiler spits out the error that I'm not allowed to define new events because the script is not flagged as native.

 

What am I doing wrong?

Link to comment
Share on other sites

Use OnItemEquipped(). Food and potions are equipped when they're used. If you want a good example of the game detecting food, look in script\base\hardcore\HC_ManagerScript.psc

 

In the HC script, the game detects when you equip the item, and runs a bunch of checks to make sure it's food and then calls the functions to do the magic of modifying the sustenance pool.

 

What I would do is register for OnItemEquipped(), run a similar check for keywords (the script uses a keyword array that's filled in the quest property, so you might want to check the HC_Manager quest to see what the Beth devs used), and then do what magic you want to happen. The HC script populates a form with the different food as kind of a temporary holding array so the processing function can run uninterrupted. So AddFoodItem() adds to that form, and the while loop inside ProcessFoodItems() will continue on until the form is empty and run each food item separately through ProcessSingleFoodItem(). That's also why ProcessFoodItems() has a bail out check; there's no reason to run it multiple times.

Link to comment
Share on other sites

I don't know what I'm doing wrong but I can't get that to work either. I looked at the HCManager Script and tried to recreate it one by one.

Scriptname AAA_WTFCoreScript extends Quest

Event OnInit()
	InitFoodProcessing()
EndEvent

Function InitFoodProcessing()

	Actor PlayerRef = Game.GetPlayer()
	
	;Register for the OnItemEquipped Event
	RegisterForRemoteEvent(PlayerRef, "OnItemEquipped")

EndFunction

Event Actor.OnItemEquipped(Actor akSender, Form akBaseObject, ObjectReference akReference)
;A notification to tell me what info I receive from the parameters
Debug.Notification("OnItemRemoved Event akSender: " + akSender + ", akBaseObject: " + akBaseObject + ", akReference: " + akReference)
	
        If akBaseObject.HasKeyword(KeywordFood)
		Debug.Notification("Success")
	Else
		Debug.Notification("Total disaster")
	EndIf
endEvent

I'm not getting any notifications and if I change it to debug.trace the papyrus log doesn't write anything down.

Link to comment
Share on other sites

Alright I figured out what was wrong.

 

Apparently I can't register for the OnItemEquipped event OnInit... Yeah go figure...

 

Anyway I solved it by moving the RegisterForRemoteEvent to a different part of my script.

Edited by Crosstieger
Link to comment
Share on other sites

  • Recently Browsing   0 members

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