Jump to content

Script Help - OnItemAdded


Plarux

Recommended Posts

Hello everyone,

 

I'm trying to create a script that is connected to a container. When the player adds a certain misc item found in the form list to the container, the misc item(s) are removed and another misc item (currency) is added to the player's inventory. For instance, depositing 2 toy trucks and 4 toy cars into the container will remove them all, then give the player 80 caps. Trucks are 20 caps/per toy. Cars are 10 caps/per toy. Multiple if statements depending on which items is added and get item count? This script is just the basics from what I've gathered by combing the forums and google.

Event OnInit()
	AddInventoryEventFilter(MiscItems)
EndEvent

Event OnItemAdded(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer)


EndEvent

Any help would be appreciated,

 

Plarux

Link to comment
Share on other sites

So I don't know how this event actually handles checking items passing through it that are more than one "kind", so my code will be a bit sloppy, but I would approach it like this:

Event OnItemAdded(params...)
Int baseTruckCost = 20
Int baseCarCost = 10
Int payment = 0

if akBaseItem == Truck
    baseTruckCost = BaseTruckCost * aiItemCount
endif

if akBaseItem == Car
    baseCarCost = baseCarCost * aiItemCount
endif

payment = baseTruckCost + baseCarCost
Game.GetPlayer().AddItem(caps, payment)

EndEvent
Edited by Rasikko
Link to comment
Share on other sites

Depending on the items that need to be inserted in this container, I might have a better solution. GetGoldValue() allows you to retrieve the value of any form but it will only return the base value even for weapon/armors that have mods that modify their value. You can then make a x:y ratio between your currency and caps.

MiscObject Property Caps001 Auto Const Mandatory ; Replace with your currency
bool silentAdd = true ; Prevents displaying a message to the player when caps are added to their inventory  

Event OnInit()
	AddInventoryEventFilter(MiscItems)
EndEvent

Event OnItemAdded(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer)
    Game.GetPlayer().AddItem(Caps001, aiItemCount * akBaseItem.GetGoldValue(), silentAdd)
EndEvent
Edited by NoCashNoExp
Link to comment
Share on other sites

I used NoCashNoExp's method, because the amount of items in the form list is large.

All credit will go to you in the script and my bounty mod.

 

Here's what I threw together in the CK and tested in-game:

Scriptname DepositBoxScript extends ObjectReference

FormList Property BountyItems Auto Const
MiscObject Property Token Auto Const Mandatory
bool silentAdd = true ; Prevents displaying a message to the player when caps are added to their inventory  

Event OnInit()
	AddInventoryEventFilter(BountyItems)
EndEvent

Event OnItemAdded(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer)
	Game.GetPlayer().AddItem(Token, aiItemCount * akBaseItem.GetGoldValue(), silentAdd)
; Maybe place a utility wait here
	Self.RemoveItem(BountyItems as form, -1, true)
EndEvent

Thank you both for the input and knowledge!

Link to comment
Share on other sites

If the player adds multiple different BountyItems your script may only add tokens for the first akBaseItem added beacuse you are removing all BountyItems that are in the container when the first OnItemAdded triggers

 

 

If that is not inteded, then

Self.RemoveItem(akBaseItem, -1, true)

To allow OnItemAdded to trigger for each BountyItem

Link to comment
Share on other sites

If the player adds multiple different BountyItems your script may only add tokens for the first akBaseItem added beacuse you are removing all BountyItems that are in the container when the first OnItemAdded triggers

 

 

If that is not inteded, then

Self.RemoveItem(akBaseItem, -1, true)

To allow OnItemAdded to trigger for each BountyItem

 

SKK50, thank you!

Link to comment
Share on other sites

Aktually OnItemAdded will probably be queued to trigger anyway even if you delete the items before the event is notified, but the count or cleanup may be inconsistent. Best to be neat 'n tidy when it costs nothing.

 

ps my pleasure.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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