Jump to content

Spawning random NPC from leveled list


MasterMuhaha

Recommended Posts

There are a couple of syntax errors, but you guys aren't understanding what I meant by an infinite loop.

 

This is what you guys are trying to do:

 

You have a leveled character (a leveled list filled with actor bases).

The actor bases that make up this leveled character all have your script attached, in order to do the disabling/enabling/spawning.

 

This is what happens:

 

NPC01: Oh, it's time to disable myself and spawn a replacement.

*Spawns replacement (NPC02) and deletes self (NPC01)*

NPC02: I'm alive!!! Oh wait, it's time to disable myself and spawn my replacement.

*Spawns replacement (NPC03) and deletes self (NPC02)*

NPC03: I'm alive!!! Oh wait, it's time to disable myself and spawn my replacement.

etc.

 

In other words, disable the NPC, register for single update game time, spawn NPC on update game time, delete self.

Edited by fg109
Link to comment
Share on other sites

The actor bases that make up this leveled character all have your script attached, in order to do the disabling/enabling/spawning.

 

I think I made it perfectly clear that my script should be placed on a spawner object, not on any of the npc's.

Link to comment
Share on other sites

:dance:

FINAL WORKING SCRIPT

:dance:

 

 

Scriptname TimedSpawnControlV2 extends ObjectReference  
{Script to spawn an Actor from a leveled list between two different hours of the day.
THIS IS A SPAWNER SCRIPT. PLACE THIS SCRIPT ON AN INVISIBLE ACTIVATOR(SPAWNER OBJECT) WHERE YOU WANT THE NPC TO SPAWN.
The actor will be spawned, or disabled and deleted at the specified time.
This script can also cope with a disable time that is earlier than the enable time.
There's an option to either spawn a new actor (DeleteMe=TRUE) or to enable/disable an existing one (DeleteMe=FALSE) without further spawning. }
;***************************************************

GlobalVariable Property GameHour  auto
{You need to autofill this property from the CK}

float Property EnableTime = 6.0 auto
{Enable me at about 6am by default}

float Property DisableTime = 24.0 auto
{Disable me at about midnight by default}

bool Property DeleteMe = TRUE auto
{Delete me after disabling me}

ActorBase Property TestNPC auto
{This should point to your leveled list}

Actor myActor = NONE

;***************************************************

Event OnInit()
{A bit of idiot proofing...}

; make sure sensible values have been set
If EnableTime < 0.0 || EnableTime > 24.0
	EnableTime = 6.0 ; failsafe default
EndIf

; make sure sensible values have been set
If DisableTime < 0.0 || DisableTime > 24.0
	DisableTime = 24.0 ; failsafe default
EndIf

EndEvent

;***************************************************

Event OnLoad()
{start the good stuff...}

CheckTime() ; check the time and spawn/disable me as required
;	Debug.Notification("[TimedSpawnControl] OnLoad Completed.")

EndEvent

;***************************************************

Event OnUpdateGameTime()
{Continue checking at set intervals}

CheckTime() ; check the time and spawn/disable me at calculated intervals
;	Debug.Notification("[TimedSpawnControl] OnUpdateGT Completed.")

EndEvent

;***************************************************
;***************************************************







;***************************************************
;** FUNCTIONS
;***************************************************

Function CheckTime()
{Decide whether to spawn the actor or disable and delete it}

;	Debug.notification("[TimedSpawnControl] CheckTime was called.")

If IsActiveTime()
	If !myActor
;			Debug.Notification("[TimedSpawnControl] Spawning an actor...")
		myActor = PlaceActorAtMe(TestNPC) ; spawn a reference from the leveled list
		If myActor.IsDisabled()
;				Debug.Notification("[TimedSpawnControl] Actor was disabled. Enabling.")
			myActor.Enable()
		EndIf
	Else
		If myActor.IsDead()
;				Debug.Notification("[TimedSpawnControl] Resetting dead actor...")
			myActor.Reset() ; if it's dead, restore it
		Else
;				Debug.Notification("[TimedSpawnControl] Actor exists, enabling it...")
			myActor.Enable()
		EndIf
	EndIf
Else
	If myActor && DeleteMe
;			Debug.Notification("[TimedSpawnControl] Deleting actor...")
		myActor.Disable()
		myActor.Delete()
		myActor = NONE
	ElseIf myActor && !DeleteMe
;			Debug.Notification("[TimedSpawnControl] Disabling actor...")
		myActor.Disable()
	EndIf
EndIf

;	Debug.Notification("[TimedSpawnControl] About to call GetInterval")

float interval = GetInterval() ; calculate the next time interval to check
RegisterForSingleUpdateGameTime(interval)

EndFunction

;***************************************************

bool Function IsActiveTime()
{Returns TRUE if current time is within the active time range}

bool bTimeRange = false
float fGHour = GameHour.GetValue()

If (DisableTime >= EnableTime)
	bTimeRange = (fGHour >= EnableTime) && (fGHour < DisableTime)
Else
	bTimeRange = (fGHour >= EnableTime) || (fGHour < DisableTime)
EndIf

;	Debug.Notification("[TimedSpawnControl] IsActiveTime returned: " + bTimeRange)

Return bTimeRange

EndFunction

;***************************************************

float Function GetInterval()
{Calculate the time interval for the next update call}

float int1
float int2
float newinterval
float fGHour = GameHour.GetValue()

;	Debug.Notification("[TimedSpawnControl] GetInterval called")

If EnableTime >= fGHour
	Int1 = EnableTime - fGHour
Else
	Int1 = EnableTime - fGHour +24
EndIf
               
If DisableTime >= fGHour
	Int2 = DisableTime - fGHour
Else
	Int2 = DisableTime - fGHour +24
EndIf

If Int1 <= Int2 ; choose the smallest time interval
	newinterval = Int1
Else
	newinterval = int2
EndIf

If newinterval < 0.167
	newinterval = 0.167 ; set the minimum update interval to about 10 game minutes
EndIf

;	Debug.Notification("[TimedSpawnControl] The next update will be in " + newinterval + " hours.")
Return newinterval

EndFunction

;***************************************************

 

 

I tested this using vanilla leveled list "lvlHorse". A random colored horse spawed at about 6am and was disabled and deleted at about midnight. A different colored horse spawned the next day at 6am... and so on.

 

INSTRUCTIONS:

 

1) Place a Trigger Primitive where you want your npc to spawn.

2) Attach this script to the Trigger reference in the Render Window (Activator Form).

3) Click the script Properties button and "auto-assign".

4) Set the value of Property "TestNPC" to be your leveled list.

DONE 8) .

 

EDIT: commented out the debug messages.

Edited by steve40
Link to comment
Share on other sites

The actor bases that make up this leveled character all have your script attached, in order to do the disabling/enabling/spawning.

 

I think I made it perfectly clear that my script should be placed on a spawner object, not on any of the npc's.

 

Sorry, I guess I didn't take a good look at your script before. It looks good. The only thing I can think of to change would be to get rid of the check for IsDisabled() after the PlaceActorAtMe.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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