Jump to content

A trigger to activate a container


Recommended Posts

I'm putting together a player home. I've got a custom storage script running on several containers. The script works like Automatic Item Storage, taking items from the player if they're on a particular formlist.

 

Ideally, I'd like one of the containers (alchemy ingredients) to be opened with a click trigger, similar to a bookcase. I've got a bunch of tiny drawers up against the wall, safe and sound behind a Collision Marker (L_NONCOLLIDABLE) so the drawers themselves can't be activated. There's a separate container which will go behind a wall. So far so good, everything works. The collision marker stops the tiny drawers being activated, the script on the real container is doing its thing. Then I got ambitious.

 

I've created a trigger volume to go in front of the collision marker, and gone to the container and set its Activate Parent to the trigger volume.

 

Here's the script attached to the container, for what it's worth:

 

 

 


Scriptname LHContainerScript extends ObjectReference

{Takes food items from the player and stores them}



Actor Property PlayerRef Auto

Formlist Property DefList Auto ;the default list of items that will automatically be stored.

Formlist Property ExpList Auto ;the expansion list, created on the fly based on items the player puts in the container.

Float Property DelayTime Auto ;a short delay to allow the animation to play

LHContainerScript Property OtherCont01 Auto ;refs to all other containers running this script

LHContainerScript Property OtherCont02 Auto

LHContainerScript Property OtherCont03 Auto

LHContainerScript Property OtherCont04 Auto

LHContainerScript Property OtherCont05 Auto

LHContainerScript Property OtherCont06 Auto

LHContainerScript Property OtherCont07 Auto

LHContainerScript Property OtherCont08 Auto

LHContainerScript Property OtherCont09 Auto

LHContainerScript Property OtherCont10 Auto

Int[] ContentsArray

Bool[] InterestArray

GlobalVariable Property LHShowStorageTut Auto

Message Property LHStorageTutorial01 Auto

Message Property LHStorageTutorial02 Auto



Event OnInit()

ContentsArray = New Int[10]

InterestArray = New Bool[10]

EndEvent



Event OnActivate(ObjectReference akActionRef) ;when the container is activated we want to take items from the player

If LHShowStorageTut.GetValueInt() == 1

LHStorageTutorial01.Show()

LHStorageTutorial02.Show()

LHShowStorageTut.SetValueInt(0)

EndIf

If PlayerRef.IsSneaking()

Debug.Notification("You are sneaking. Autostore will not activate.")

Else

Utility.WaitMenuMode(DelayTime)

TakeItems(Self)

EndIf

EndEvent



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

If akSourceContainer == PlayerRef ; only do anything if the item was taken from the player

CheckAddedItem(akBaseItem) ;when an item is added we want to add it to the expansion list if it's not there already

int iContainerItems = Self.GetQuantity(akBaseItem)

GetShares(akBaseItem)

EndIf

EndEvent



Event OnItemRemoved(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akDestContainer)

If PlayerRef.IsSneaking()

If akDestContainer == PlayerRef

CheckRemovedItem(akBaseItem)

EndIf

EndIf

EndEvent



Function CheckAddedItem(form akItem) ;checks item against both lists and adds it to the expanded list if it's unrecognised

If !(DefList.HasForm(akItem))

If !(ExpList.HasForm(akItem))

ExpList.AddForm(akItem)

Debug.MessageBox("This container will now store " + akItem.GetName())

EndIf

EndIf

EndFunction



Function CheckRemovedItem(form akItem) ;checks item against the expansion list and deletes it if the player has removed them all

If (ExpList.HasForm(akItem))

If Self.GetItemCount(akItem) == 0

ExpList.RemoveAddedForm(akItem)

Debug.MessageBox("This container will no longer store " + akItem.GetName())

EndIf

EndIf

EndFunction



Function TakeItems(ObjectReference akContainer) ;takes items from the player if they appear on either formlist

int iCounter = PlayerRef.GetItemCount(DefList) ;Get a count of each item on the DefList

; Debug.MessageBox("Player has " + iCounter + " Deflist items")

PlayerRef.RemoveItem(DefList, iCounter, True, Self) ;Take deflist items from the player

iCounter = PlayerRef.GetItemCount(ExpList) ; Get a count of each item on the ExpList

; Debug.MessageBox("Player has " + iCounter + " Explist items")

PlayerRef.RemoveItem(ExpList, iCounter, True, Self) ;Take explist items from the player

Debug.Notification("Storing items")

EndFunction



; Sharing functions



Int Function GetQuantity(Form akItem) ; returns the quantity of akItem

return Self.GetItemCount(akItem)

EndFunction



Bool Function GetInterest(Form akItem)

Bool bInterested = 0

If Deflist.HasForm(akItem)

bInterested = 1

ElseIf ExpList.HasForm(akItem)

bInterested = 1

EndIf

return bInterested

EndFunction



Int Function GetShares(Form akItem)

int iMyItems = Self.GetQuantity(akItem) ; get count from this container

CheckContainers(akItem) ; populate the arrays with data pertaining to this item

int iNumConts = 1 ; 'this' container is obviously interested in the item

int iIndex = InterestArray.Length

While iIndex

iIndex -= 1

If InterestArray[iIndex]

iNumConts += 1

EndIf

EndWhile

; Debug.MessageBox("There are " + iNumConts + " interested containers!")

int iGrandTotal = GetQuantity(akItem); Count the number of items in this container first

iIndex = ContentsArray.Length

While iIndex

iIndex -= 1

iGrandTotal += ContentsArray[iIndex]

EndWhile

; Debug.MessageBox("There are " + iGrandTotal + " " + akItem.GetName() + " altogether!")

int iShare = iGrandTotal / iNumConts

; Debug.MessageBox("Each container should have " + iShare + " " + akItem.GetName())

iIndex = ContentsArray.Length ;overwrite the array one final time and work out how many items to send

While iIndex

iIndex -= 1

If InterestArray[iIndex]

If iShare > ContentsArray[iIndex]

ContentsArray[iIndex] = iShare - ContentsArray[iIndex]

EndIf

Else

ContentsArray[iIndex] = 0

EndIf

EndWhile

SendContents(akItem)

EndFunction



Int[] Function SendContents(form akItem)

If OtherCont01

If OtherCont01.GetQuantity(akItem) < ContentsArray[1]

Self.RemoveItem(akItem, ContentsArray[1], True, OtherCont01)

EndIf

EndIf

If OtherCont02

If OtherCont02.GetQuantity(akItem) < ContentsArray[2]

Self.RemoveItem(akItem, ContentsArray[2], True, OtherCont02)

EndIf

EndIf

If OtherCont03

If OtherCont03.GetQuantity(akItem) < ContentsArray[3]

Self.RemoveItem(akItem, ContentsArray[3], True, OtherCont03)

EndIf

EndIf

If OtherCont04

If OtherCont04.GetQuantity(akItem) < ContentsArray[4]

Self.RemoveItem(akItem, ContentsArray[4], True, OtherCont04)

EndIf

EndIf

If OtherCont05

If OtherCont05.GetQuantity(akItem) < ContentsArray[5]

Self.RemoveItem(akItem, ContentsArray[5], True, OtherCont05)

EndIf

EndIf

If OtherCont06

If OtherCont06.GetQuantity(akItem) < ContentsArray[6]

Self.RemoveItem(akItem, ContentsArray[6], True, OtherCont06)

EndIf

EndIf

If OtherCont07

If OtherCont07.GetQuantity(akItem) < ContentsArray[7]

Self.RemoveItem(akItem, ContentsArray[7], True, OtherCont07)

EndIf

EndIf

If OtherCont08

If OtherCont08.GetQuantity(akItem) < ContentsArray[8]

Self.RemoveItem(akItem, ContentsArray[8], True, OtherCont08)

EndIf

EndIf

If OtherCont09

If OtherCont09.GetQuantity(akItem) < ContentsArray[9]

Self.RemoveItem(akItem, ContentsArray[9], True, OtherCont09)

EndIf

EndIf

If OtherCont10

If OtherCont10.GetQuantity(akItem) < ContentsArray[10]

Self.RemoveItem(akItem, ContentsArray[10], True, OtherCont10)

EndIf

EndIf

; Debug.MessageBox(akItem.GetName() + " stored!")

CleanArrays()

EndFunction



Int[] Function CheckContainers(form akItem)

If OtherCont01

InterestArray[1] = OtherCont01.GetInterest(akItem)

ContentsArray[1] = OtherCont01.GetQuantity(akItem)

; Debug.MessageBox("Container 1 has " + OtherCont01.GetItemCount(akItem) + " " + akItem.GetName())

EndIf

If OtherCont02

InterestArray[2] = OtherCont02.GetInterest(akItem)

ContentsArray[2] = OtherCont02.GetQuantity(akItem)

; Debug.MessageBox("Container 2 has " + OtherCont02.GetItemCount(akItem) + " " + akItem.GetName())

EndIf

If OtherCont03

InterestArray[3] = OtherCont03.GetInterest(akItem)

ContentsArray[3] = OtherCont03.GetQuantity(akItem)

; Debug.MessageBox("Container 3 has " + OtherCont03.GetItemCount(akItem) + " " + akItem.GetName())

EndIf

If OtherCont04

InterestArray[4] = OtherCont04.GetInterest(akItem)

ContentsArray[4] = OtherCont04.GetQuantity(akItem)

; Debug.MessageBox("Container 4 has " + OtherCont04.GetItemCount(akItem) + " " + akItem.GetName())

EndIf

If OtherCont05

InterestArray[5] = OtherCont05.GetInterest(akItem)

ContentsArray[5] = OtherCont05.GetQuantity(akItem)

; Debug.MessageBox("Container 5 has " + OtherCont05.GetItemCount(akItem) + " " + akItem.GetName())

EndIf

If OtherCont06

InterestArray[6] = OtherCont06.GetInterest(akItem)

ContentsArray[6] = OtherCont06.GetQuantity(akItem)

; Debug.MessageBox("Container 6 has " + OtherCont06.GetItemCount(akItem) + " " + akItem.GetName())

EndIf

If OtherCont07

InterestArray[7] = OtherCont07.GetInterest(akItem)

ContentsArray[7] = OtherCont07.GetQuantity(akItem)

; Debug.MessageBox("Container 7 has " + OtherCont07.GetItemCount(akItem) + " " + akItem.GetName())

EndIf

If OtherCont08

InterestArray[8] = OtherCont08.GetInterest(akItem)

ContentsArray[8] = OtherCont08.GetQuantity(akItem)

; Debug.MessageBox("Container 8 has " + OtherCont08.GetItemCount(akItem) + " " + akItem.GetName())

EndIf

If OtherCont09

InterestArray[9] = OtherCont09.GetInterest(akItem)

ContentsArray[9] = OtherCont09.GetQuantity(akItem)

; Debug.MessageBox("Container 9 has " + OtherCont09.GetItemCount(akItem) + " " + akItem.GetName())

EndIf

If OtherCont10

InterestArray[10] = OtherCont10.GetInterest(akItem)

ContentsArray[10] = OtherCont10.GetQuantity(akItem)

; Debug.MessageBox("Container 10 has " + OtherCont10.GetItemCount(akItem) + " " + akItem.GetName())

EndIf

return ContentsArray

EndFunction



Function CleanArrays()

int iIndex = InterestArray.Length

While iIndex

iIndex -= 1

InterestArray[iIndex] = 0

ContentsArray[iIndex] = 0

EndWhile

EndFunction

 

 

 

Here's a screenshot of the setup, with the collision marker, decorative drawers, hidden container and trigger volume shown:

 

 

 

It's firing my script (I know because the tutorial messages are being displayed) but the container's inventory isn't being shown and the container's animation isn't being played. What could I be doing wrong?

Edited by Offtherails
Link to comment
Share on other sites

  • Recently Browsing   0 members

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