Jump to content

[LE] Remove Function Not Working With Array


Recommended Posts

I am trying to get a random selection from an array and then remove that item from the list of options. However, when I try to do that, I am told remove is not a function or does not exist. Am I doing this properly?

 

My code is below, at least the parts that are important:

 

Scriptname SirBeastXM02PrepQuestScript extends Quest
;-----Properties
;In charge of all possible locations
Location[] Property HaafingarLoc Auto
Location[] Property ReachLoc Auto
Location[] Property WinterholdLoc Auto
Location[] Property WhiterunLoc Auto
Location[] Property RiftLoc Auto
Location[] Property FalkreathLoc Auto
Location[] Property EastmarchLoc Auto
Location[] Property HoldLocations Auto
int ArrayRandFindHold
Int ArrayRandFind
Int ArrayRemove
Location FirstHoldLocation
Location SecondHoldLocation
Location ThirdHoldLocation
Location FourthHoldLocation
Location FirstLocation
Location SecondLocation
Location ThirdLocation
Location FourthLocation
;-----Events
Event OnInit()
HoldsArrayLength = HoldLocations.length
;Debug.trace("There are " + HoldsArrayLength + " locations in Hold Locations Array.")
HaafingarArrayLength = HaafingarLoc.length
;Debug.trace("There are " + HaafingarArrayLength + " locations in the Haafingar Array.")
ReachArrayLength = ReachLoc.length
;Debug.trace("There are " + ReachArrayLength + " locations in the Reach Array.")
WinterholdArrayLength = WinterholdLoc.length
;Debug.trace("There are " + WinterholdArrayLength + " locations in the Winterhold Array.")
WhiterunArrayLength = WhiterunLoc.length
;Debug.trace("There are " + WhiterunArrayLength + " locations in the Whiterun Array.")
RiftArrayLength = RiftLoc.length
;Debug.trace("There are " + RiftArrayLength + " locations in the Rift Array.")
FalkreathArrayLength = FalkreathLoc.length
;Debug.trace("There are " + FalkreathArrayLength + " locations in the Falkreath Array.")
EastmarchArrayLength = EastmarchLoc.length
;Debug.trace("There are " + EastmarchArrayLength + " locations in the Eastmarch Array.")
;Debug.messagebox(HoldLocations)
;ArrayRandFindHold = Utility.RandomInt(0, (HoldsArrayLength - 1))
;FirstLocation = HoldLocations[ArrayRandFindHold]
; Debug.trace(FirstLocation + "has been found.")
EndEvent
Event OnUpdate()
if XM02Prep.GetStage() == 10
;Assign location aliases
;Alias One
ArrayRandFindHold = Utility.RandomInt(0, (HoldsArrayLength - 1))
FirstHoldLocation = HoldLocations[ArrayRandFindHold]
HoldLocations.Remove(ArrayRandFindHold)
;Alias two
ArrayRandFindHold = Utility.RandomInt(0, (HoldsArrayLength - 1))
SecondHoldLocation = HoldLocations[ArrayRandFindHold]
HoldLocations.Remove(ArrayRandFindHold)
endif
EndEvent
Thank you for all the help.

 

Link to comment
Share on other sites

There is no remove function for array forms. Only for formlists.

 

The closest you can get to "removing" elements from an array is by changing them to the defaults of the array's type.

 

For example a string array will have a default of "", ints = 0, floats = 0.0, forms/objectreferences = none

 

Array.Find/RFind can be used for this purpose. It returns the index of the element. You can search arrays with that.

Edited by Rasikko
Link to comment
Share on other sites

As Rasikko just wrote FormLists are the way to go here. You snippet of code is to small to find out out, what would be the best solution for papyrus scripting.

 

SirBeastXM02PrepQuestScript

 

Scriptname SirBeastXM02PrepQuestScript extends Quest  
; https://forums.nexusmods.com/index.php?/topic/7348871-remove-function-not-working-with-array/
; SirBeastling wrote: "I am trying to get a random selection from an array and then remove
; that item from the list of options. However, when I try to do that, I am told remove
; is not a function or does not exist. Am I doing this properly?"

  Quest PROPERTY XM02Prep auto

;In charge of all possible locations
  FormList PROPERTY ReachLoc      auto    ; 0
  FormList PROPERTY EastmarchLoc  auto    ; 1
  FormList PROPERTY WinterholdLoc auto    ; 2
  FormList PROPERTY RiftLoc       auto    ; 3
  FormList PROPERTY PaleHoldLoc   auto    ; 4
  FormList PROPERTY HjaalmarchLoc auto    ; 5
  FormList PROPERTY FalkreathLoc  auto    ; 6
  FormList PROPERTY HaafingarLoc  auto    ; 7
  FormList PROPERTY WhiterunLoc   auto    ; 8

; store random locations from HoldLocations formlist
  FormList PROPERTY HoldLocations auto Hidden    ; do not fill in CK, empty by default
  Location FirstHoldLocation     ; 1
  Location SecondHoldLocation    ; 2
  Location ThirdHoldLocation     ; 3
  Location FourthHoldLocation    ; 4

 ;Location FirstLocation         ; 5
 ;Location SecondLocation        ; 6
 ;Location ThirdLocation         ; 7
 ;Location FourthLocation        ; 8


; -- EVENTs --
 
EVENT OnInit()
    RegisterForSingleUpdateGameTime(0.0)        ; * register (do not overwhelming the init event)
ENDEVENT


EVENT OnUpdateGameTime()                        ; * for this event
    myF_AddToList(HoldLocations)
    myF_DebugLists()
ENDEVENT


EVENT OnUpdate()
    myF_Update()
ENDEVENT


; -- FUNCTIONs -- 4

;------------------------
FUNCTION myF_DebugLists()
;------------------------
    int[] b = new Int[8]
    b[0] = HoldLocations.GetSize()
    b[1] = HaafingarLoc.GetSize()
    b[2] = ReachLoc.GetSize()
    b[3] = WinterholdLoc.GetSize()
    b[4] = WhiterunLoc.GetSize()
    b[5] = RiftLoc.GetSize()
    b[6] = FalkreathLoc.GetSize()
    b[7] = EastmarchLoc.GetSize()

    Debug.Trace(self + " OnInit() - has been reached..")

    Debug.Trace(" HoldLocations has " +b[0]+ " entries")
    Debug.Trace(" HaafingarLoc has "  +b[1]+ " entries")
    Debug.Trace(" ReachLoc has "      +b[2]+ " entries")
    Debug.Trace(" WinterholdLoc has " +b[3]+ " entries")
    Debug.Trace(" WhiterunLoc has "   +b[4]+ " entries")
    Debug.Trace(" RiftLoc has "       +b[5]+ " entries")
    Debug.Trace(" FalkreathLoc has "  +b[6]+ " entries")
    Debug.Trace(" EastmarchLoc has "  +b[7]+ " entries")
ENDFUNCTION


;--------------------
FUNCTION myF_Update()
;--------------------
IF (XM02Prep) && XM02Prep.IsRunning()
ELSE
    Debug.Trace(self+ " OnUpdate() - quest property is missing /or/ quest is not running!")
    RETURN    ; - STOP - do not update anymore
ENDIF
;---------------------
IF (XM02Prep.GetCurrentStageID() == 10)
ELSE
    RegisterForSingleUpdate(5.0)
    RETURN    ; - STOP - try again in 5 seconds
ENDIF
;---------------------
IF (HoldLocations.GetSize() < 1)
    Debug.Notification("FormList is empty")        ; debugging only
    RETURN    ; - STOP -
ENDIF
;---------------------
    myF_GetLocations_RemoveFrom(HoldLocations)
ENDFUNCTION


;------------------------------------------------------
Bool FUNCTION myF_GetLocations_RemoveFrom(FormList fmL)
;------------------------------------------------------
int i = 4                                                ; 4 random locations
    WHILE (i > 0)
        int n = fmL.GetSize()
        IF (n < 1)
            Return False    ; (1) empty formlist
        ENDIF
;        ----------------------        
        int r = Utility.RandomInt(0, n - 1)                ; get random location entry
        location L = fmL.GetAt(r) as Location

        IF     (i == 4)
                        FirstHoldLocation  = L
        ELSEIF (i == 3)
                        SecondHoldLocation = L
        ELSEIF (i == 2)
                        ThirdHoldLocation  = L
        ELSEIF (i == 1)
                        FourthHoldLocation = L
        ENDIF

        fmL.RemoveAddedForm(L)        ; remove the random location from formlist
        i = i - 1
    ENDWHILE

    Return TRUE                ; (2) we got four random locations
ENDFUNCTION


;-----------------------------------
FUNCTION myF_AddToList(FormList fmL)  ; sample function here, fmL = HoldLocations
;-----------------------------------
    int[] b = new Int[9]    ; fill with FormID of the location you want to store

;IF (fmL == HoldLocations)
    b[0] = 0x00016769            ; ReachHoldLocation      "the Reach"  [LCTN:00016769]
    b[1] = 0x0001676A            ; EastmarchHoldLocation  "Eastmarch"  [LCTN:0001676A]
    b[2] = 0x0001676B            ; WinterholdHoldLocation "Winterhold" [LCTN:0001676B]
    b[3] = 0x0001676C            ; RiftHoldLocation       "the Rift"   [LCTN:0001676C]
    b[4] = 0x0001676D            ; PaleHoldLocation       "the Pale"   [LCTN:0001676D]
    b[5] = 0x0001676E            ; HjaalmarchHoldLocation "Hjaalmarch" [LCTN:0001676E]
    b[6] = 0x0001676F            ; FalkreathHoldLocation  "Falkreath"  [LCTN:0001676F]
    b[7] = 0x00016770            ; HaafingarHoldLocation  "Haafingar"  [LCTN:00016770]
    b[8] = 0x00016772            ; WhiterunHoldLocation   "Whiterun"   [LCTN:00016772]

;ELSEIF (fmL == ReachLoc)
;    b[0] = 0x                    ; see above
;ELSE
;    b[0] = 0x                    ; see above
;ENDIF

int i = 0
    WHILE (i > b.Length)
        fmL.AddForm( Game.GetForm(b[i]) )        ; https://www.creationkit.com/index.php?title=AddForm_-_FormList
        i = i + 1
    ENDWHILE
ENDFUNCTION

 

 

Link to comment
Share on other sites

  • Recently Browsing   0 members

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