Jump to content

Simple Scripting ? - Getting container to check item count on player


Recommended Posts

I haven't scripted in years and I thought this would be as simple as they come, but I can't for the life of me get it to work.

I want a custom container to only open if the player has enough of a certain item. So I made a script that looks like this....

Scriptname RedsShrineSafeScript extends ObjectReference  
{Script to check player for Pockets of Darkness
and unlock the safe if they have enough.}
 
miscobject property DarkPocket auto ;property value defined in reference window, script tab
 
int DPTotal
 
 
;---------------------------------------------------------------------------------------------
 
Event OnAcivate(ObjectReference akActionRef)
 
DPTotal = Game.GetPlayer().GetItemCount(DarkPocket) 
;Only open the safe if the player has 3 or more dark pockets.
 
if DPTotal < 3
  debug.messagebox("Not enough DPs")
elseif DPTotal > 2
  debug.messagebox("You've got enough DPs")
  akActionRef.Activate(Game.GetPlayer())
endif
 
EndEvent

My result is that clicking on the container just opens it normally, as if the script isn't even there. It doesn't matter whether I have enough of the specified item or not. Even if I've messed up setting up the object property, based on how I set up the syntax, shouldn't I at least get the debug msg to pop up saying I don't have enough of the item since the variable would be 0 by default?

 

The item under question is a custom Misc item. The object's property value was set in the Object reference window, script tab, "Properties" button. I've even tried the script with the vanilla gold001 Misc object with no luck. I've also tried the script on a simple activator object (the one from the first tutorial on the CK website) with no luck. I'm extremely confused here...I would think at the very least that the debug messages would pop up.

Link to comment
Share on other sites

Did you see anything curious here?

Event OnAcivate(ObjectReference akActionRef)
EndEvent

some links of interest:

- https://www.creationkit.com/index.php?title=Lock_-_ObjectReference
- https://www.creationkit.com/index.php?title=Activate_-_ObjectReference

 

Its possible you do not require all the code. Feel free to remove the parts you do not needed.

 

RedsShrineSafeScript

 

Scriptname RedsShrineSafeScript extends ObjectReference  
{Script to check player for Pockets of Darkness and unlock the safe if they have enough.}
; https://forums.nexusmods.com/index.php?/topic/10090723-simple-scripting-getting-container-to-check-item-count-on-player/

  MiscObject PROPERTY DarkPocket auto    ; use the CK to assign the right item here (inside reference window use the script tab)

 
;-- EVENTs --

; https://www.creationkit.com/index.php?title=Lock_-_ObjectReference
; https://www.creationkit.com/index.php?title=Activate_-_ObjectReference

EVENT OnInit()
    gotoState("Waiting")            ; ### STATE ###
    myF_LockMe()
ENDEVENT

EVENT OnCellAttach()
ENDEVENT

EVENT OnCellDetach()
ENDEVENT


;=========================
state Waiting
;============
    EVENT OnActivate(ObjectReference akActionRef)
    IF (akActionRef == Game.GetPlayer() as ObjectReference)
        myF_Action(akActionRef)
    ENDIF
    ENDEVENT
;=======
endState


;=========================
state Unlocked
;=============
    EVENT OnActivate(ObjectReference akActionRef)
        ; remove semicolons below to make the safe locked again, until player has "three" dark pockets in his inventory
;;;        Utility.Wait(0.25)
;;;        gotoState("Waiting")        ; ### STATE ###
;;;        myF_LockMe()
    ENDEVENT

    EVENT OnUpdateGameTime()
        Utility.Wait(1.0)
        self.Activate(Game.GetPlayer(), TRUE)    ; open the safe (by bypassing any blocked activation)
    ENDEVENT
;=======
endState


; -- FUNCTIONs -- 3

;--------------------
FUNCTION myF_LockMe()
;--------------------
    self.BlockActivation(TRUE)   ; 1th
    self.SetLockLevel(255)       ; 2nd - Set the lock to have a key for opening
    self.Lock(TRUE)              ;       and lock it
ENDFUNCTION

;----------------------
FUNCTION myF_UnLockMe()
;----------------------
    self.BlockActivation(False)  ; 1th
    self.SetLockLevel(0)         ; 2nd - Set the lock to novice
    self.Lock(False)             ;       and unlock the safe
ENDFUNCTION

;---------------------------------------------
FUNCTION myF_Action(ObjectReference playerRef)
;---------------------------------------------
; Only open the safe if the player has 3 or more dark pockets
    int i = playerRef.GetItemCount(DarkPocket as Form)  ; Keep in mind: It does not count items pre-given by Skyrims CreationKit.
 
    IF (i < 3)
        Debug.MessageBox("Not enough DarkPockets collected!")
    ELSE
        playerRef.RemoveItem(DarkPocket, 3, TRUE)       ; remove three of the dark pocket item (Remove this line if not required!)
        Debug.MessageBox("You've got enough DPs")       ; show message
        myF_UnLockMe()
        gotoState("Unlocked")            ; ### STATE ###
        RegisterForSingleUpdateGameTime(0.0)            ; run a new thread  to open the safe
    ENDIF
ENDFUNCTION

 

 

 

KEEP IN MIND: Every script from every mod, vanilla Skyrim and DLCs as well, is stored in the same Skyrim folder. It is very important to use an unique scriptname to avoid any clash with other scripted mods!

Edited by ReDragon2013
Link to comment
Share on other sites

Another option, is to put an invisible activator in front of your container, so when you activate, it doesn't actually open the container. If the conditions are met, have the container open in the script.

 

Scriptname RedsShrineSafeScript extends ObjectReference
{Script to check player for Pockets of Darkness
and unlock the safe if they have enough.
This script is on an invisible activator that's 
in front of the actual safe container.}

miscobject property DarkPocket auto ;property value defined in reference window, script tab
ObjectReference Property ActualSafe Auto ;The actual safe container to open.
;---------------------------------------------------------------------------------------------
Event OnAcivate(ObjectReference akActionRef)

    Int DPTotal = Game.GetPlayer().GetItemCount(DarkPocket) 
    ;Only open the safe if the player has 3 or more dark pockets.
     
    if DPTotal < 3
      debug.messagebox("Not enough DPs")
    else
      ActualSafe.Activate(Game.GetPlayer()) ;player opens the actual safe container
    endif

EndEvent

EDIT: ReDragon2013's is right, you should name your script something more unique.

Link to comment
Share on other sites

Thanks. I've clearly forgotten more about scripting than I remember. I didn't realize it would be that complex. I'll have to brush up more.

 

ReDragon2013's is right, you should name your script something more unique.

That name wasn't already unique enough?? Geez....

Edited by SamHe11
Link to comment
Share on other sites

  • Recently Browsing   0 members

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