OnItemAdded - ObjectReference
Revision as of 04:07, 28 February 2015 by imported>DavidJCobb (→Notes: The trick requires some finesse...)
Member of: ObjectReference Script
Event received when an item is inserted into this object's container.
Syntax
Event OnItemAdded(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer)
Parameters
- akBaseItem: The base object for the item that was added to this container.
- aiItemCount: The number of items added to this container.
- akItemReference: The specific reference added to the container, if any. Will be None if a non-persistant object is added.
- akSourceContainer: The container that the object(s) came from. If None, then the object came from the world.
Examples
Event OnItemAdded(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer)
if !akSourceContainer
Debug.Trace("I picked up " + aiItemCount + "x " + akBaseItem + " from the world")
elseif akSourceContainer == Game.GetPlayer()
Debug.Trace("The player gave me " + aiItemCount + "x " + akBaseItem)
else
Debug.Trace("I got " + aiItemCount + "x " + akBaseItem + " from another container")
endIf
endEvent
Notes
- Once an object has been added to a container it is no longer valid to call member functions on it. Member functions on akItemReference (which is often None) will most likely fail to run with an error.
- If you only care about certain kinds of objects, you should also use AddInventoryEventFilter to filter out any events for things you aren't interested in.
- If the player takes a large number of items out of a container (e.g. by using the Take All feature while in their home), an OnItemAdded event handler that doesn't use an inventory filter will fire for each type (base form) of item that the player removes. If a container has a considerably large variety of items inside, too many OnItemAdded stack will accrue, causing Skyrim to dump those function calls as well as stacks from other mods/DLCs.
- If you need to temporarily stop OnItemAdded and OnItemRemoved calls from being queued, you can use AddInventoryEventFilter with an empty FormList as an argument. To resume listening for item additions and removals, you can call RemoveAllInventoryEventFilters.
- This trick stops OnItemAdded calls from being queued. It doesn't prevent the execution of function calls that have already been queued. To wit, using this trick from inside of OnItemAdded won't stop Skyrim from dumping stacks when you Take All from a QASmoke container. Rather, you'd want to stop listening to inventory events from elsewhere (e.g. an OnMenuOpen event checking for inventory menus, if you don't mind relying on SKSE).