Jump to content

Scripting, moveto, and AI packages


Recommended Posts

Within a mod for Fallout 4, I have a jail which as twelve jail cells to house prisoners.  These jail cells are identified with xmarkers located at the center of each cell.  These markers are labeled FO4_CellMarker01 through FO4_CellMarker12.  I have created six NPC prisoners which are located in a temporary holding cell called CoveTempHoldingCell.  The six prisoners are labeled NPC_Prisoner01 through NPC_Prisoner06.  I have created a Papyrus script that achieves the following: 1) When the player character enters a certain trigger, the six prisoners are randomly placed into the jail cells; and 2) When the player leaves the trigger, the six prisoners are returned to the temporary holding cell.  In execution, I am noticing a significant dps drop when the player enters the trigger.  Also, the npcs do not appear to be using the packages when placed by the script.  If I hand place the npcs into various jail cells, the npcs use their packages with no issue.  Any insight would be welcomed and appreciated.

================================================================

Scriptname FO4_JailCellScript extends ObjectReference

; Define properties for your NPC prisoners and cell markers
Actor Property FO4_NPC_Prisoner01 Auto
Actor Property FO4_NPC_Prisoner02 Auto
Actor Property FO4_NPC_Prisoner03 Auto
Actor Property FO4_NPC_Prisoner04 Auto
Actor Property FO4_NPC_Prisoner05 Auto
Actor Property FO4_NPC_Prisoner06 Auto
ObjectReference[] Property JailCells Auto
ObjectReference Property CoveTempMarker01 Auto
ObjectReference Property CoveTempMarker02 Auto
ObjectReference Property CoveTempMarker03 Auto
ObjectReference Property CoveTempMarker04 Auto
ObjectReference Property CoveTempMarker05 Auto
ObjectReference Property CoveTempMarker06 Auto

; Define a trigger to detect player entry and exit
Event OnTriggerEnter(ObjectReference akActionRef)
    If akActionRef == Game.GetPlayer()
        PlacePrisoners()
    EndIf
EndEvent

Event OnTriggerLeave(ObjectReference akActionRef)
    If akActionRef == Game.GetPlayer()
        ReturnPrisoners()
    EndIf
EndEvent

; Function to randomly place prisoners in the cells
Function PlacePrisoners()
    ; Initialize the prisoners array
    Actor[] prisoners = new Actor[6]
    prisoners[0] = FO4_NPC_Prisoner01
    prisoners[1] = FO4_NPC_Prisoner02
    prisoners[2] = FO4_NPC_Prisoner03
    prisoners[3] = FO4_NPC_Prisoner04
    prisoners[4] = FO4_NPC_Prisoner05
    prisoners[5] = FO4_NPC_Prisoner06

    ; Array to keep track of used cells
    Int[] usedCells = new Int[0]
    ; Debug.Notification("Placing prisoners...") ; Debug message

    While prisoners.Length > 0
        Int cellIndex = Utility.RandomInt(0, JailCells.Length - 1)
        If usedCells.Find(cellIndex) == -1
            prisoners[0].MoveTo(JailCells[cellIndex])
            prisoners[0].EvaluatePackage() ; Force NPC to evaluate AI packages
            ; Debug.Notification("FO4_JailCellScript: Moved prisoner to cell: " + cellIndex) ; Debug message

            ; Add cell index to usedCells
            Int[] newUsedCells = new Int[usedCells.Length + 1]
            Int i = 0
            While i < usedCells.Length
                newUsedCells = usedCells
                i += 1
            EndWhile
            newUsedCells = cellIndex
            usedCells = newUsedCells

            ; Remove the first prisoner from the array
            Actor[] newPrisoners = new Actor[prisoners.Length - 1]
            i = 1
            While i < prisoners.Length
                newPrisoners[i - 1] = prisoners
                i += 1
            EndWhile
            prisoners = newPrisoners
        EndIf
    EndWhile
EndFunction

; Function to return prisoners to the temporary holding cell
Function ReturnPrisoners()
    FO4_NPC_Prisoner01.MoveTo(CoveTempMarker01)
    FO4_NPC_Prisoner02.MoveTo(CoveTempMarker02)
    FO4_NPC_Prisoner03.MoveTo(CoveTempMarker03)
    FO4_NPC_Prisoner04.MoveTo(CoveTempMarker04)
    FO4_NPC_Prisoner05.MoveTo(CoveTempMarker05)
    FO4_NPC_Prisoner06.MoveTo(CoveTempMarker06)
    ; Debug.Notification("FO4_JailCellScript: Prisoners returned to holding cell.") ; Debug message

EndFunction

 

Link to comment
Share on other sites

I have found that if actors are not 3d loaded after the moveto it can affect AI packages establishing even after forcing EvaluatePackage() 

You may need to add an OnLoad() EvaluatePackage() to their collection to work around that.

Link to comment
Share on other sites

You didn't meantion what kind of AI Package the NPCs should perform (i.e. AI Procedure >>Sit) but you probably want their AnimGraph managers to be initialized (and if Furnitures are involved, theirs too). Is3DLoaded() == True doesn't necessarily mean the AnimGraph is also available, however, after checking Is3DLoaded(), Utility.Wait(0.2) is usually enough for EvaluatePackage() to work as expected. (I added Bool Function HasAnimGraphManager(ObjectReference akReference) native global to Garden of Eden SE in v19.4 if you're interested).

  • Like 1
Link to comment
Share on other sites

The six prisoner NPCs have two simple AI packages: 1) Sandbox package with a 128 radius; and 2) Sleep package.  Inside jail cell is 256 x 256 game units, and contains a chair, a bed, a toilet, and a sink.  I have hand placed a few furniture markers in each of the cells (e.g., NPCNewspaperLeanLeft, etc).  Thanks for the input SKKmods, and LarannKiar, your sage wisdom is invaluable.  

Link to comment
Share on other sites

  • 2 weeks later...

Don’t you just love the brute force approach to scripting.  🙂 Anyways, here is what I have done: 1) Set up a quest; 2) moved the majority of the scripting to the quest; 3) created three scripts two of which reside on the quest while the other one resides on the default trigger.  The AI packaging issue seems to be resolved but there is still a drop in FPS when entering the trigger.  Seems odd for only moving six npcs.  Here are the scripts for referenced.  Still taking a look see at vanilla scripts to (e.g. DLC04MQ05QuestScrip) to see if I can implement the MoveTo better.

  Reveal hidden contents
  Reveal hidden contents
  Reveal hidden contents

 

Link to comment
Share on other sites

  • Recently Browsing   0 members

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