Randomly Spawning Character In Certain Areas

I have a character that is part of a campaign. I need him to randomly spawn in certain areas until he is killed.

This character needs to randomly spawn in certain areas, let’s say about seven different areas in the module, until he is slain. Once he is dead, I need the campaign to recognize that fact. Is there a way to store a boolean so other scripts can check it during the campaign?

when should he ?

  1. spawn
  2. change areas

ie. on what event, when player or plot does … what?

yes

1 Like
  1. He will have a chance to spawn, perhaps 5% each time the player enters the designated area. After that, he will wander around the area, giving the player a chance to encounter him or try to avoid him. I can handle the wandering behavior, but that is what I intend to do.

  2. If he is not killed and the player leaves the area he is wandering around in, they may encounter him there or in another one of the designated areas when they arrive there, until he is slain.

does he spawn initially in only 1 specific Area, or can he initially spawn when PC enters any of the designated areas?

 
ps. Does he have a name yet so I can label some variables? Right now am just calling him Wanderer and its kinda clumsy …

his Tag and Resref would be handy here.

1 Like

here’s the kind of thing im looking at so far …

// 'fs_ce_area'
/*
	Area OnClientEnter script.

	Spawns a Wanderer when PC enters a specific area (with a probability) and
	then jumps the Wanderer among several areas that have Waypoints with
	specific Tags.

	The format of the Waypoints' tags is
		sp_wandererX
	where X is a digit. Place a Waypoint where the Wanderer can be jumped to in
	each designated Area; there can be more than one Waypoint in any Area, which
	increases the chance of the Wanderer appearing there.

	The digits MUST be sequential. And the initial spawn point MUST be
		sp_wanderer0

	Set iWP_COUNT to the highest digit + 1
*/

// this is the tag/resref of the Wanderer
const string sTAG = "tag_wanderer";
// this is the tag-prefix of the Waypoints that the Wanderer can spawn at
const string sTAG_WP_PRE = "sp_wanderer";

// this is the count of Waypoints that the Wanderer can spawn at
const int iWP_COUNT = 7;

// the tag of the Area that the Wanderer initially spawns into
const string sTAG_START_AREA = "tag_of_inital_spawn_area";

// global variable set TRUE when Wanderer is slain
const string sWANDERER_DEAD = "sWandererDone";


// OBJECT_SELF is Area
void main()
{
	// does this need to check if the quest has started

	// check if the Wanderer has been slain already
	// set this TRUE in his OnDeath script ...
	if (!GetGlobalInt(sWANDERER_DEAD))
	{
		object oPc = GetFirstEnteringPC();
		if (GetIsObjectValid(oPc) && GetIsPC(oPc)) // safety. These should always be true in an OnClientEnter script.
		{
			object oWanderer = GetObjectByTag(sTAG);

			if (!GetIsObjectValid(oWanderer) // if Wanderer does not exist Create it
				&& GetTag(OBJECT_SELF) == sTAG_START_AREA)
			{
				if (!Random(20)) // 5% chance to spawn
				{
					object oSp = GetWaypointByTag("sp_wanderer0");
					oWanderer = CreateObject(OBJECT_TYPE_CREATURE, sTAG, GetLocation(oSp));
				}
			}
			else if (GetIsObjectValid(oWanderer))
			{
				// Wanderer is valid in an Area, jump it randomly to any Area with one of the Waypoints
				// it might be this area, it might not ...

				object oSp = GetObjectByTag(sTAG_WP_PRE + IntToString(Random(iWP_COUNT)));
				AssignCommand(oWanderer, ClearAllActions(TRUE));
				AssignCommand(oWanderer, JumpToObject(oSp));

//				if (GetArea(oWanderer) == OBJECT_SELF)
//				{
//					// start WalkWayPoints or ...
//				}
			}
		}
	}
}
1 Like

Hi, I am not going to write a full script for you beceaus eI don’t know your setting, I am going to give you pointer:

the 5% chances nothing to say about it

if(!Random(20))

the spawn of the NPC :

Use a marker to mark it has spawned. If it’s intra one module you can use SetLocalInt(GetModule(),…
if it can spawn across different modules in a same campaign use a GlobalInt.

You can also use SetLocalObject or SetGlobalObject and check its existence.

object oNPC;
if(!GetGlobalInt("MyNpcIsOn")){

SetGlobalInt("MyNpcIsOn",TRUE);
oNPC = CreateObject(OBJECT_TYPE_CREATURE,sResRef,lLoc,FALSE,MyNewTag);

}

Now depending on what you want to do if you don’t want the NPC to move one he is in place :

if(GetGlobalInt("MyNpcIsOn")) return;

If you want your NPC to move inside one module you can use the Jump function.

If you want your NPC to move across different module you ll have to destroy it and recreate it.

if(GetGlobalInt("MyNpcIsOn") && !GetIsObjectValid(GetObjectByTag("MyNPC")) {

//here you know your NPC has been spawn once but it s not alive in the module where you are.
//you may want to put a marker for death.


}

For the conversion if it’s inside one module a module conversation will work.
if its cross module you’ll have to use a campaign conversation.

What you want to do is easy but require to be “fluent” with NWN script. It’s a good exercice to becommes more proficient with it.

You need to :

1 check the condition for it to be here.
2 check its existence
3 put a mark for its death
4 if he al rdy exist and has not been slain move it around by despawning him and making him again, or by jumping around.
5 be carefull where you store your indicator, if its inside a single module no much problem, if its cross module you need to be sure to store your value in var that are cross module.
Don’t use campaign var they are not tied to save game.

2 Likes

Thanks for the input everyone, this will help a lot. I will test the solutions listed and let you all know what works best! :slight_smile:

1 Like