Jump to content

[LE] Running a script on a formlist of actors Question


Recommended Posts

This is probably a dumb question as this is my first attempt at using form lists in a script. The loop counter and everything in the script works fine aside from finding the correct actor from the form list (it returns it as None). Am I just writing the line wrong? I copied the format from an example on the creation kit site, and it compiles fine. It just doesn't work.

FormList Property TeddyBearActorList Auto

Function BearRecruit()

    Int BearIndex = TeddyBearActorList.GetSize()

    While BearIndex >= 0
        BearIndex -= 1

        Actor akBear = TeddyBearActorList.GetAt(BearIndex) as Actor ;This is the line that won't work
        akBear.SetPlayerTeammate()
        Debug.Notification(akBear + " is your friend.")
    EndWhile

    TeddyBear_Dialogue.SetStage(10)
EndFunction
Link to comment
Share on other sites

The syntax is fine. The problem is that the objects you filled the formlist with do not yield a valid result when cast as Actor.

 

When you filled the formlist did you use actor records or the object reference records of pre-placed actors?

 

If it was actor records then those would cast into ActorBase rather than Actor.

If it was object reference records of pre-placed actors, then it should be able to cast into Actor without any issues.

 

As far as Papyrus is concerned, Actor is an extension of an ObjectReference while ActorBase refers to the actual actor record as found in the Creation Kit.

Link to comment
Share on other sites

On each loop you are actually running the one below the one that passed the test given you do -=1 right after.

 

Logic hurts my brain so I hope this appreciated

	Bool[] bWhatThe = new Bool [5]
	bWhatThe[0] = true
	bWhatThe[1] = true
	bWhatThe[2] = true
	bWhatThe[3] = true
	bWhatThe[4] = true

	Int nIndex = bWhatThe.length ; it's 5 see above
	debug.Notification("length is " + nIndex)
	While(nIndex)
		nIndex -=1
		Debug.Notification("Is number " + nIndex + " " + bWhatThe[nIndex] + "?")
	EndWhile

The 1st run of the Loop is 4, it works perfectly, cdcooley, sorry bloke your wrong as well. It needs to be:

While BearIndex >= 0

or

While BearIndex

else it won't go Zero since it looping backwards

 

Carry on Levionte, I agree with you.

Link to comment
Share on other sites

I dont really get what you are saying but cdcooley and I are right (though maybe badly explained on my end)

 

You contradict yourself when you say it needs to be "equal or higher to 0" or "anything but 0" because they dont involve the same numbers.

 

While index (i.e. different than 0) or while index "higher than" 0 work the same way in this case and its what you should use if going backwrds, "higher or equal" will attempt to access element -1 probably giving an error.

 

Edited: Comparison operators put weird symbols on my phone

Link to comment
Share on other sites

I would like to show you the difference between using of formlist or array in a script.

 

formlist handling

 

Scriptname TeddyBearQuestScript extends Quest
; https://forums.nexusmods.com/index.php?/topic/5511977-running-a-script-on-a-formlist-of-actors-question/

  Quest    PROPERTY TeddyBear_Dialogue auto        ;* Is this quest the same as this script should be attached?
  FormList PROPERTY TeddyBearActorList auto

; -- FUNCTION --

FUNCTION BearRecruit()
;---------------------
int i = TeddyBearActorList.GetSize()   ; i = BearIndex
    WHILE (i > 0)                                   ; "WHILE (i)" is shorter than "WHILE (i > 0)", but has the same logic
        i = i - 1
;;;     form fm = TeddyBearActorList.GetAt(i)
        actor aRef = TeddyBearActorList.GetAt(i) as Actor

        IF ( aRef )
            aRef.SetPlayerTeammate(TRUE)
            Debug.Notification(i+" got a new friend.. " +aRef)
        ENDIF
    ENDWHILE

    IF (self as Quest == TeddyBear_Dialogue)
        self.setStage(10)                          ;* answer: TRUE
    ELSE
        TeddyBear_Dialogue.setStage(10)            ;* answer: False
    ENDIF
ENDFUNCTION

 

 

 

 

array handling

 

Scriptname TeddyBearQuestScript extends Quest
; https://forums.nexusmods.com/index.php?/topic/5511977-running-a-script-on-a-formlist-of-actors-question/

  Quest   PROPERTY TeddyBear_Dialogue auto        ;* Is this quest the same as this script should be attached?
  Actor[] PROPERTY TeddyBearActorList auto        ; array instead of formlist

; -- FUNCTION --

FUNCTION BearRecruit()
;---------------------
int i = TeddyBearActorList.Length                 ; i = BearIndex
    WHILE (i > 0)                                 ; "WHILE (i)" is shorter than "WHILE (i > 0)", but has the same logic
        i = i - 1
        actor aRef = TeddyBearActorList[i]        ; aRef = akBear

        IF ( aRef )
            aRef.SetPlayerTeammate(TRUE)
            Debug.Notification(i+" got a new friend.. " +aRef)
        ENDIF
    ENDWHILE

    IF (self as Quest == TeddyBear_Dialogue)
        self.setStage(10)                         ;* answer: TRUE
    ELSE
        TeddyBear_Dialogue.setStage(10)           ;* answer: False
    ENDIF
ENDFUNCTION

 

 

 

assembled code: WHILE (i > 0)

;  .local ::temp0 int
;  .local ::temp1 bool

;  CALLMETHOD GetSize ::TeddyBearActorList_var ::temp0  ;@line 12
;  ASSIGN i ::temp0                                     ;@line 12

;  label0:
;  COMPAREGT ::temp1 i 0                                ;@line 13
;  JUMPF ::temp1 label1                                 ;@line 13

;  ISUBTRACT ::temp0 i 1                                ;@line 14
;  ASSIGN i ::temp0                                     ;@line 14

assembled code: WHILE (i)

; .local ::temp0 int

;  CALLMETHOD GetSize ::TeddyBearActorList_var ::temp0  ;@line 12
;  ASSIGN i ::temp0                                     ;@line 12

;  label0:
;  JUMPF i label1                                       ;@line 13

;  ISUBTRACT ::temp0 i 1                                ;@line 14
;  ASSIGN i ::temp0                                     ;@line 14

In case you would use next code, you will get an error:

WHILE (i >= 0)
    i = i - 1      ; last "i" will be -1 at this point, that is out of bound, first array entry is "a[0]", first formlist entry is "fml.GetAt(0)"
   ...
ENDWHILE

Unfortunately "PeterMartyr" you are not fully right with your statement.

"cdcooley" has given the right answer, imho.

Edited by ReDragon2013
Link to comment
Share on other sites

  • Recently Browsing   0 members

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