Wanting to script an event to trigger when all the creatures in an area are all killed. Thought adding something to each creature’s OnDeath event might work, by checking if there are other creatures still in the area.
This is what I have so far, but could use help getting it to work!
object oTarget = GetArea(OBJECT_SELF);
if (!GetObjectType(OBJECT_SELF) == OBJECT_TYPE_CREATURE) return;
object oObject = GetFirstObjectInArea(oTarget);
while (oObject != OBJECT_INVALID)
if (OBJECT_SELF == GetArea(oObject)) return;
else oObject = GetNextObjectInArea(oTarget);
//another script happens when there are no creatures left in the area
You need some way of excluding PCs and their associates. If all the enemies have one tag, filter on that. If they’re in one faction, GetFactionEqual would do it. Otherwise, GetIsPC and GetMaster could be used to filter out good guys.
The if statement before return is currently comparing the dead creature with its area, which can never be true. “If oObject is a bad guy and oObject is not equal to OBJECT_SELF” is more like the pseudo-code you need.
Well, you can try something like that:
const string AREA_TAG = "your_area_tag";
const string VAR_NUM_ENEMIES_KILLED = "NumEnemiesKilled";
//const string VAR_TOTAL_ENEMIES_SPAWNED = "NumEnemiesSpawned";
const int TOTAL_ENEMIES_TO_KILL = 100; // if this number is unknown in advance, move it to a variable
// on your area when enemies are spawning.
object oArea = GetObjectByTag(AREA_TAG);
int nIncrement = GetLocalInt(oArea, VAR_NUM_ENEMIES_KILLED) + 1;
SetLocalInt(oArea, VAR_NUM_ENEMIES_KILLED, nIncrement);
// SendMessageToPC(GetFirstPC(), "Kill count: " + IntToString(GetLocalInt(oArea, VAR_NUM_ENEMIES_KILLED)));
// int TOTAL_ENEMIES_TO_KILL = GetLocalInt(oArea, VAR_TOTAL_ENEMIES_SPAWNED);
if (GetLocalInt(oArea, VAR_NUM_ENEMIES_KILLED) >= TOTAL_ENEMIES_TO_KILL)
Add this to each creature’s OnDeath event script.
If you’re strictly looking for the existence of living enemy creatures of the PC, then GetNearestCreature using the player as a source could do the trick, too.
// Find the player closest to the dying creature:
object oPC = GetNearestCreature(CREATURE_TYPE_PLAYER_CHAR, PLAYER_CHAR_IS_PC, OBJECT_SELF);
// Look for the closest living enemy to the PC:
object oTarget = GetNearestCreature(CREATURE_TYPE_IS_ALIVE, TRUE, oPC, CREATURE_TYPE_REPUTATION, REPUTATION_TYPE_ENEMY);
// Abort if there are still living enemy creatures of the PC in the area.
// Since we didn't find any living enemy creatures, make the PC execute a script.
Doesn’t account for multiplayer, though, nor for neutral or friendly creatures. Aqvilinus’ method looks pretty solid to me; skips the entirety of the area-checking and creature-searching, just increments a counter per dead creature.
Maybe you could count the living enemy creatures in the area in the area’s OnEnter script and store that number on the area as a variable, which you read again in the OnDeath script of the creatures, so you don’t have to specify the exact number of creatures manually each time.
Thanks for the idea. I set it up to add an int when creatures spawn, then take it away when they die. Once it gets back to 0, my other scripts runs!
Alternatively, you can use an encounter. Each encounter has an OnExhausted event that fires when all the spawned creatures die. That way you don’t have to mess around with custom OnDeath checks. Just put your what-happens-when-all-spawned-creatures-die-event script in the OnExhausted event field of the encounter and there you go.