Jump to content

help turning a form into an objectreference


richarg

Recommended Posts

what Im trying to do is have an activate script which will randomly teleport the player to a location and then remove that location from the list.

 

First I tried using an array, which works, but unfortunately I can't get it to remove the location afterwords. (If any one knows how to do that I'd apprieciate it.)

 

 

 

Scriptname ActivateTeleport extends ObjectReference
ObjectReference[] Property destinationMarkers Auto
ObjectReference Property playerRef Auto
Event OnActivate(ObjectReference akActionRef)
Int randomIndex = Utility.RandomInt(0, destinationMarkers.Length - 1)
playerRef.MoveTo(destinationMarkers[randomIndex])
Debug.Notification("Teleporting to destination #" + (randomIndex + 1))
EndEvent

but what I'm trying to do now is use form lists instead, however I can't work out how to make the form thing selected be the location to teleport to.
Scriptname FormActivateTeleport extends ObjectReference
FormList Property destinationMarkers Auto
ObjectReference Property playerRef Auto
Event OnActivate(ObjectReference akActionRef)
Int randomIndex = Utility.RandomInt(0, destinationMarkers.GetSize() - 1)
Form destinationForm = destinationMarkers.GetAt(randomIndex)
ObjectReference destinationRef = ObjectReference(destinationForm.GetNthForm(0))
playerRef.MoveTo(destinationRef)
Debug.Notification("Teleporting to destination #" + (randomIndex + 1))
destinationMarkers.RemoveAddedForm(destinationForm)
EndEvent
Starting 1 compile threads for 1 files...
Compiling "TeleportActivateRandomForm"...
E:\SteamLibrary\steamapps\common\Skyrim Special Edition\Data\Source\Scripts\temp\TeleportActivateRandomForm.psc(9,69): GetNthForm is not a function or does not exist
E:\SteamLibrary\steamapps\common\Skyrim Special Edition\Data\Source\Scripts\temp\TeleportActivateRandomForm.psc(9,37): ObjectReference is not a function or does not exist
No output generated for TeleportActivateRandomForm, compilation failed.
Batch compile of 1 files finished. 0 succeeded, 1 failed.
Failed on TeleportActivateRandomForm
Sorry Im new to modding and scripting in general I've just been obsessed about this I can't let it go, if any one could help I'd apprecieate it. Thanks
Link to comment
Share on other sites

All you need is a cast.

ObjectReference destinationRef = destinationMarkers.GetAt(randomIndex) as  ObjectReference
Just make sure it was valid:

If destinationRef
    playerRef.MoveTo (destinationRef)  
EndIf
A word of caution: you cannot remove items from FormLists via script if they were not added via script.

In other words, if you create a FormList in CK and add a bunch of teleport destination markers to it, you CANNOT remove those in a script.

 

What you can do:

In CK, make a FormList where you add all the destination references. Call it 'originalDestinationMarkers'

Also, create another FormList called 'destinationMarkers' and leave it empty.

I assume you have some sort of quest driving your mod. Sometime in the quest initialization stage, copy everything from originalDestinationMarkers to destinationMarkers:

FormList Property originalDestinationMarkers Auto
FormList Property destinationMarkers Auto

; this function uses SKSE64 functions added post-AE, so it likely will not work in  1.5.97.   If wanting better compatibility, use SE version
Function InitDestinationsAE()
    destinationMarkers.AddForms(originalDestinationMarkers.ToArray())
EndFunction

Function InitDestinationsSE()
    Int _idx = 0
    While(_idx < originalDestinationMarkers.GetSize())
        destinationMarkers.AddForm(originalDestinationMarkers.GetAt(_idx))
        _idx += 1
    EndWhile
EndFunction
Now that you have that done, you can:

Event OnActivate(ObjectReference akActionRef)
    Int randomIndex = Utility.RandomInt(0, destinationMarkers.GetSize() - 1)
    ObjectReference destinationRef = destinationMarkers.GetAt(randomIndex) as ObjectReference
    If destinationRef
        playerRef.MoveTo(destinationRef)
        Debug.Notification("Teleporting to destination #" + (randomIndex + 1))
        destinationMarkers.RemoveAddedForm(destinationRef as Form)
    EndIf
EndEvent
P.S. Oh, you should also consider some kind of clause when destinations are exhausted.

I.e. at start of OnActivate function:

If destinationMarkers.GetSize() < 1
    ; do whatever needed
    return
EndIf
Link to comment
Share on other sites

  • Recently Browsing   0 members

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