I have a qglobal set up to spawn an npc in certain zones. The idea is that he starts off in one zone, stays for a set length of time, depops and repops in a different zone, and repeats that process continuously. For purposes of illustration, I'll narrow it to Zone 1, Zone 2, and Zone 3.
The npc spawns in Zone 1, whereupon the script immediately deletes the previous global variable (1) and sets a new one (2). The timer keeps him in the zone for 2 minutes (again, just to simplify), and when he depops he respawns in Zone 2, deletes the previous variable (2) and sets the new variable (3). The process repeats and when he gets to Zone 3 the script deletes the previous variable and sets it at variable 1 again, and it repeats that entire sequence.
I have global watchers in each of the three zones that define under which variable condition they will spawn the npc. Both scripts appear below (these are the scripts for Zone 1):
Code:
#Wanderer NPC -- npcid 999228
sub EVENT_SPAWN
{
quest::delglobal("wanderer");
quest::setglobal("wanderer", 2, 7, "F");
quest::shout("I am now in the zone");
quest::settimer("depop",120);
}
sub EVENT_TIMER
{
if($timer eq "depop")
{
quest::shout("I am now leaving the zone");
quest::stoptimer("depop");
quest::depop();
}
}
Code:
my $EventStart;
sub EVENT_SPAWN
{
$EventStart = 0;
quest::depopall(999228);
}
sub EVENT_WAYPOINT
{
quest::shout("");
if (defined($qglobals{wanderer}))
{
my $x;
my $y;
my $z;
my $h;
$x = $npc->GetX();
$y = $npc->GetY();
$z = $npc->GetZ();
$h = $npc->GetHeading();
if ($qglobals{wanderer} == 1 && $EventStart == 0)
{
$EventStart == 1;
quest::spawn2(999228,0,0,$x+5,$y+5,$z,$h);
}
}
}
The script works fine but for one or two details. First, there's really nothing in place to start the sequence of events. Second, I'm not convinced the placement of the delglobal and setglobal is actually workable in a real-world scenario, and I'm not sure the alternative is either.
The first issue first. In order to start it I have to manually add the global to the db to get the npc to spawn initially. If I don't add it manually each time I boot up the server, the watchers will never see a variable and therefore will never spawn the npc.
There are several "fixes" that I've already thought through or tried and dismissed, including:
1. Dbspawn the wanderer in Zone 1 so that the global sets when a player enters that zone. I tried this but found that the wanderer would respawn in that same zone (Zone 1) whenever a player exited and reentered the (empty) zone, even though the npc was already spawned in Zone 2.
2. Use an event on a third npc to trigger the global initially (such as EVENT_SAY hail). But then the global would reset each time another player hails the npc.
Now for the second issue--placement of the delglobal and setglobal. The current placement deletes the global and resets it to the next variable immediately upon spawning the npc. Here it is . . .
sub EVENT_SPAWN
{
quest::delglobal("wanderer");
quest::setglobal("wanderer", 2, 7, "F");
quest::shout("I am now in the zone");
quest::settimer("depop",120);
}
However, if I want the npc to remain in each zone for an hour, and if the new global variable (new spawn zone) is set at the
beginning of that hour, then the npc could potentially be in two zones at one time--he'll stay in Zone 1 (if populated by a pc) until he depops, but since the global has neen reset, he'll also appear in Zone 2 if a pc enters that zone at the same time.
So, I tried placing the setglobal with the depop timer event, like so:
sub EVENT_SPAWN
{
quest::delglobal("wanderer");
quest::shout("I am now in the zone");
quest::settimer("depop",120);
}
sub EVENT_TIMER
{
if($timer eq "depop")
{
quest::setglobal("wanderer", 2, 7, "F");
quest::shout("I am now leaving the zone");
quest::stoptimer("depop");
quest::depop();
}
}
However, if the wanderer pops in a zone that is populate by a pc, and the pc leaves before the depop occurs, then the new global is never set. And since the old global has been deleted, there is nothing for the watchers to see that would cause a subsequent spawn.
Then I tried putting both the delglobal and the setglobal in the depop timer event, like so:
sub EVENT_SPAWN
{
quest::shout("I am now in the zone");
quest::settimer("depop",120);
}
sub EVENT_TIMER
{
if($timer eq "depop")
{
quest::delglobal("wanderer");
quest::setglobal("wanderer", 2, 7, "F");
quest::shout("I am now leaving the zone");
quest::stoptimer("depop");
quest::depop();
}
}
However, when I do this the watcher spawns another npc every five seconds (watchers are on a 5-sec grid). I assumed the $EventStart object (which works perfectly in another global) would prevent this, but it hasn't (unless I am doing something wrong with it).
In any case, these are the issues I am having. I need the wandering npc to (1) initially spawn apart from a manual process, (2) be a unique character in the world, (3) be in only one zone at a time, (4) wander from zone to zone continually.
Any help would be most appreciated.