Ok so I've made this system for my PW module that when a PC enters an area, NPCs are spawned to points marked with waypoints. When a player leaves the area, all the NPCs are destroyed.
That worked fine until I expanded the script, I made it so that if an NPC is killed, a local int will be stored to his personal waypoint by the name "dead" with value 1, and there's a code in the NPC's OnDeath script to restore the int back to 0 with a delay of 20 seconds.
However, this doesn't seem to work and I suspect it is because the DelayCommand gets disturped when the NPC is destroyed and local int "dead" stays with the value of 1 forever.
Here are the scripts:
Area OnEnter:void main()
{
object oPC = GetEnteringObject();
if (!GetIsPC(oPC)) return;
object oWP = GetNearestObject(OBJECT_TYPE_WAYPOINT, oPC);
int iNth = 1;
if (GetLocalInt(GetArea(oPC),"spawned")==FALSE)
{
while (GetIsObjectValid(oWP))
{
SendMessageToPC(oPC,""+GetTag(oWP)+", dead: "+IntToString(GetLocalInt(oWP,"dead")));
if (GetLocalInt(oWP,"dead")!=1)
{
object oCreate = CreateObject(OBJECT_TYPE_CREATURE,GetTag(oWP),GetLocation(oWP));
SetLocalObject(oCreate, "spawn",oWP);
}
oWP = GetNearestObject(OBJECT_TYPE_WAYPOINT, oPC, ++iNth);
}
SetLocalInt(GetArea(oPC),"spawned",TRUE);
}
}
Area OnExit:
/*
This function returns TRUE if oArea currently
has a PC in it and FALSE it doesn't.
*Note: Returns FALSE if oArea is not valid.
*/
int GetIsAreaOccupied(object oArea)
{
if (GetIsObjectValid(oArea))
{
object oPlayer = GetFirstPC();
while ((GetArea(oPlayer) != oArea) && (oPlayer != OBJECT_INVALID))
oPlayer = GetNextPC();
if (oPlayer != OBJECT_INVALID)
return TRUE;
else
return FALSE;
}
else
return FALSE;
}
// Destroying the NPCs.
void main()
{
object oPC = GetExitingObject();
object oLoop = GetFirstObjectInArea(GetArea(oPC));
object oPlayer = GetFirstObjectInArea(GetArea(oPC));
int UnSpawn=TRUE;
if (!GetIsAreaOccupied(OBJECT_SELF))
{
while (GetIsObjectValid(oLoop))
{
if (GetObjectType(oLoop)==OBJECT_TYPE_CREATURE && (GetCurrentHitPoints(oLoop)==GetMaxHitPoints(oLoop) || GetIsObjectValid(GetLocalObject(oLoop,"spawn"))))
{
SendMessageToPC(oPC,GetName(oLoop));
DestroyObject(oLoop);
}
SetLocalInt(GetArea(oLoop),"spawned",FALSE);
oLoop = GetNextObjectInArea(GetArea(oPC));
}
}
}
NPC OnDeath://:://////////////////////////////////////////////////
//:: NW_C2_DEFAULT7
/*
Default OnDeath event handler for NPCs.
Adjusts killer's alignment if appropriate and
alerts allies to our death.
*/
//:://////////////////////////////////////////////////
//:: Copyright © 2002 Floodgate Entertainment
//:: Created By: Naomi Novik
//:: Created On: 12/22/2002
//:://////////////////////////////////////////////////
#include "x2_inc_compon"
#include "x0_i0_spawncond"
void main()
{
ExecuteScript("wob_xp",OBJECT_SELF);
ExecuteScript("od_droprandom",OBJECT_SELF);
if (GetIsObjectValid(GetLocalObject(OBJECT_SELF,"spawn")))
{
object oWP = GetLocalObject(OBJECT_SELF,"spawn");
SetLocalInt(oWP,"dead",1);
DelayCommand(20.0,SetLocalInt(oWP,"dead",0));
}
int nclass = GetLevelByclass(class_TYPE_COMMONER);
int nAlign = GetAlignmentGoodEvil(OBJECT_SELF);
object oKiller = GetLastKiller();
// If we're a good/neutral commoner,
// adjust the killer's alignment evil
if(nclass > 0 && (nAlign == ALIGNMENT_GOOD || nAlign == ALIGNMENT_NEUTRAL))
{
AdjustAlignment(oKiller, ALIGNMENT_EVIL, 5);
}
// Call to allies to let them know we're dead
SpeakString("NW_I_AM_DEAD", TALKVOLUME_SILENT_TALK);
//Shout Attack my target, only works with the On Spawn In setup
SpeakString("NW_ATTACK_MY_TARGET", TALKVOLUME_SILENT_TALK);
// NOTE: the OnDeath user-defined event does not
// trigger reliably and should probably be removed
if(GetSpawnInCondition(NW_FLAG_DEATH_EVENT))
{
SignalEvent(OBJECT_SELF, EventUserDefined(1007));
}
craft_drop_items(oKiller);
}
I don't know how I could make it so the NPC's would respawn after a certain amount of time.
Please help!
Thanks,
~BadEagle
Modifié par BadEagle, 14 août 2011 - 03:43 .