Jump to content

[Scripts]Setting a max weight of containers


AndrealphusVIII

Recommended Posts

Hello all

 

For a while I have been trying to set a maximum weight limit to containers in the game.

 

After a long search I get to this script:

 

 

 

ScriptName YourScript Extends ObjectReference

Float fCurrentWeight
Float Property fMaxWeight = 420.0 Auto

Event OnItemAdded(Form akBaseItem, Int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer)
    Float fWeight = akBaseItem.GetWeight()
    If aiItemCount > 1
        Float fWeightTotal = fWeight * aiItemCount
        If fCurrentWeight + fWeightTotal > fMaxWeight
            Int iCount = aiItemCount
            While iCount
                iCount -= 1
                If fCurrentWeight + fWeight > fMaxWeight
                    RemoveItem(akBaseItem, iCount + 1, False, akSourceContainer)
                Debug.Notification("The " + GetBaseObject().GetName() + " is full. (Current weight: " + ( fCurrentWeight as Int) + ")")
                    Return ; No need to continue cycling the loop at this point
                Else
                    fCurrentWeight += fWeight
                EndIf
            EndWhile
        Else
            fCurrentWeight += fWeightTotal
        EndIf
    ElseIf fCurrentWeight + fWeight < fMaxWeight
        fCurrentWeight += fWeight
    Else
        RemoveItem(akBaseItem, 1, False, akSourceContainer)
        Debug.Notification("The " + GetBaseObject().GetName() + " is full. (Current weight: " + ( fCurrentWeight as Int) + ")")
    EndIf
EndEvent

 

I want this script to return any item that makes the current weight go over the weight cap.

 

Problem with this script is that it isn't working properly yet. I'm having following issue:

  • Let's take for instance, I have 90 Dragon Bones (10 Weight each) and 10 Dragon Scales (15 Weight each) in my inventory.
  • The cap is set to 1000 carry weight.
  • First, I deposit 90 Dragon Bones (= 900 carry weight). Nothing special happens, so far so good.
  • Then, I deposit the 10 Dragon Scales (= 150 carry weight), which brings the total current carry weight of the container to 1050. (900 + 150)
  • Instead of returning 4 Dragon Scales (= 60 weight, 1050 - 60 = 990 -> below the cap) to my inventory, it keeps all 10 without returning any. It only seems to display the message: "The Chest is full (Current weight: 1000)" This is obviously a bug. It should return 4 of them.
  • If I deposit anything else after this, it automatically returns it to my inventory and displays the message again. This is intended: since the current carry weight is still 1050, so it is supposed to return it.
  • But, if I take all the Dragon Scales and Dragon Bones back to my inventory (which brings the current container weight to 0), it still displays the message (stating it's still over the cap, while the container is empty though) and returns everything regardless of the "real" current carry weight. This seem to be bugged as well.

 

Any ideas how to make the script work?

 

Thanks in advance

 

Kind regards

Andre

Link to comment
Share on other sites

 

 

  • But, if I take all the Dragon Scales and Dragon Bones back to my inventory (which brings the current container weight to 0), it still displays the message (stating it's still over the cap, while the container is empty though) and returns everything regardless of the "real" current carry weight. This seem to be bugged as well.

To address that you need to use the OnItemRemoved() event and re-calculate the weight and reassign the value to the variable.

 

Try putting Self in front of your RemoveItem commands.

For example:

Self.RemoveItem(akBaseItem, iCount + 1, False, akSourceContainer)

 

Even tho the script is applied to the container it still needs to know where to remove what from. Using Self tells the script to use the object it is attached to.

 

Hopefully those two things (adding the self and using the OnItemRemoved() event) will allow it to work.

Link to comment
Share on other sites

To address that you need to use the OnItemRemoved() event and re-calculate the weight and reassign the value to the variable.

 

So you mean I should add this just before the OnItemAdded() event?

Event OnItemRemoved(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akDestContainer)
    Float fCurrentWeight = CurrentWeight.GetWeight() ; Or GetWeight of something else?
EndEvent

Or isn't this correct at all?

Forgive me if I may sound newbie, but I still have a lot to learn about scripting.

Edited by AndrealphusVIII
Link to comment
Share on other sites

Try something more like this.

Event OnItemRemoved(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akDestContainer)
  Float ItemWeight = akBaseItem.GetWeight()
  fCurrentWeight -= (ItemWeight * aiItemCount)
  Debug.Notification(GetBaseObject().GetName() + " (Current weight: " + ( fCurrentWeight as Int) + ")")
EndEvent
Link to comment
Share on other sites

  • Recently Browsing   0 members

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