Jump to content

[LE] Can I Spawn a NPC and delete them when done?


Cheyron

Recommended Posts

I'm a little confused if I have to make a NPC in the creation kit to use in my quest or if I can just spawn an existing NPC and use it then delete them when I am done with them. I just need a whiterun guard to use in a scene for my quest. I want them to arrest another npc and escort them to prison? When I spawn a guard using this code... it seems the reference does not get filled because they don't participate in the scene and the guard just walks away doing his own thing...

Actor Function SpawnNPCOffsetFromRef(ObjectReference ref, Form npcBaseForm, Float offsetX, Float offsetY, Float offsetZ, Float rotationOffset)
    Actor npc = ref.PlaceAtMe(npcBaseForm, 1, abInitiallyDisabled = True) As Actor
    Float zAngle = ref.GetAngleZ() + rotationOffset
    offsetX *= Math.Sin(zAngle)
    offsetY *= Math.Cos(zAngle)
    npc.MoveTo(ref, offsetX, offsetY, offsetZ)
    npc.SetAngle(0, 0, npc.GetHeadingAngle(Game.GetPlayer()))
    npc.Enable(True)
EndFunction

;BEGIN FRAGMENT Fragment_0
Function Fragment_0(ObjectReference akSpeakerRef)
Actor akSpeaker = akSpeakerRef as Actor
;BEGIN CODE
akSpeaker.RemoveFromFaction(WhiterunBreezhomeFaction)
Actor guard = SpawnNPCOffsetFromRef(FrontDoor, NPCToSummon.GetBaseObject(), 0, 10, 0, 0)
guard.SetRelationshipRank(akSpeaker, -4)
guard.SetAlert(True)
GuardRef.ForceRefTo(guard)
Utility.Wait(0.01)
ApprehendScene.ForceStart();//the guard is supposed to chase (follow) a npc but he ignores... I spawned the npc GuardWhiterunImperialPatrolDay1
FleeBreezehomeScene.ForceStart();//this scene makes the npc run out the house where the guard is
;END CODE
EndFunction
;END FRAGMENT

Link to comment
Share on other sites

Should be fine to use an existing NPC as long it's not unique. The problem I think is with your function, which has a return value, but it doesn't return anything. So add this line at the end of the function:

return npc

Also, the function shouldn't be part of the fragment script, but in a separate script attached to the quest. Then call it like so

Actor guard = (GetOwningQuest() as MyQuestScript).SpawnNPCOffsetFromRef(FrontDoor, NPCToSummon.GetBaseObject(), 0, 10, 0, 0)
Edited by Ghaunadaur
Link to comment
Share on other sites

 

Should be fine to use an existing NPC as long it's not unique. The problem I think is with your function, which has a return value, but it doesn't return anything. So add this line at the end of the function:

return npc

Also, the function shouldn't be part of the fragment script, but in a separate script attached to the quest. Then call it like so

Actor guard = (GetOwningQuest() as MyQuestScript).SpawnNPCOffsetFromRef(FrontDoor, NPCToSummon.GetBaseObject(), 0, 10, 0, 0)

 

lmao I forgot to put the return value... wow thanks papyrus compiler for warning me!

 

thanks for pointing that out, maybe im too tired but I never noticed that … well no wonder the guard never listened to me LMAO

 

why would that function not be ok in the fragment... I know those fragments are garbage collected after the are done being used but I actually only use that function in the fragment, does it affect something else im not aware of?

 

Thanks for posting, I simply never noticed that!

Link to comment
Share on other sites

why would that function not be ok in the fragment... I know those fragments are garbage collected after the are done being used but I actually only use that function in the fragment, does it affect something else im not aware of?

 

From what I understand, fragment scripts are supposed to be edited through the CK interface only, not directly. It may not cause any issues if done correctly, but I'd rather avoid doing so. Moving the custom functions to a quest script makes it easier to call it from other scripts on your quest and, in my experience, helps to keep things organized...but of course that's up to you!

 

If you say the function code is only used once and in a single script, there's no reason to make it a function in the first place. :smile:

Link to comment
Share on other sites

 

why would that function not be ok in the fragment... I know those fragments are garbage collected after the are done being used but I actually only use that function in the fragment, does it affect something else im not aware of?

 

From what I understand, fragment scripts are supposed to be edited through the CK interface only, not directly. It may not cause any issues if done correctly, but I'd rather avoid doing so. Moving the custom functions to a quest script makes it easier to call it from other scripts on your quest and, in my experience, helps to keep things organized...but of course that's up to you!

 

If you say the function code is only used once and in a single script, there's no reason to make it a function in the first place. :smile:

 

 

it's a function simply for readability, I just moved it to a quest script anyway, thanks

Link to comment
Share on other sites

  • Recently Browsing   0 members

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