Heartbeat script question
TheTinman
Member Posts: 75
How taxing would this script be on an area?
// Tinmans Heartbeat area respawner test script
// Created 12\8\2023
void main()
{
// Get the object type of the object that runs the script
int nObjectType = GetObjectType(OBJECT_SELF);
// Check if the object is a placeable
if (nObjectType == OBJECT_TYPE_PLACEABLE)
{
// Use the Tag of a Standard Creature
// Use the resref of a Custom Creature
string sTag = GetTag(OBJECT_SELF);
// Check if the object has a local variable named “DO_ONCE”
if (GetLocalInt(OBJECT_SELF, "DO_ONCE") == 0)
{
// Set the variable to 1, so the script will not fire again
SetLocalInt(OBJECT_SELF, "DO_ONCE", 1);
// We run this section only once
CreateObject (OBJECT_TYPE_CREATURE, sTag, GetLocation (OBJECT_SELF), FALSE);
}
// Check if the object is an area
if (nObjectType == OBJECT_TYPE_PLACEABLE)
{
nObjectType = GetObjectType(OBJECT_SELF);
// Get the area object
object oArea = GetArea(OBJECT_SELF);
// Get the first creature in the area
object oCreature = GetFirstObjectInArea(oArea);
// The delay time for the spawner script
float fDelay = 20.0;
// Loop through all creatures in the area
while (GetIsObjectValid(oCreature))
{
// Check if the creature is dead
if (GetIsDead(oCreature))
{
// Increment the delay time by 7 seconds
//fDelay += 7.0;//Not sure i will use this option
// Get the tag of the creature
string sTag = GetTag(oCreature);
// Find the closest placeable with the same tag as the creature
object oClosestPlaceable = GetNearestObjectByTag(sTag, oCreature);
// Check if the placeable is valid
if (GetIsObjectValid(oClosestPlaceable))
{
// Reset the variable of the closest placeable to 0
DelayCommand(fDelay, SetLocalInt(oClosestPlaceable, "DO_ONCE", 0));
}
}
// Get the next creature in the area
oCreature = GetNextObjectInArea(oArea);
}
}
}
}
0
Comments
For another, it looks like you're using a placeable as the object to generate a creature and you're using this script to reset the object 20 seconds after the creatures dies, but I can't see a reason why you wouldn't just use the standard encounters since they already have all of that functionality.
In any case, if you're always generating the same creature or the same subset of creatures, it would be far more time-efficient to just use the on-death script of the creature to do this job instead of using an area heartbeat.
If you want to be able to spawn any creature from a placeable, then you'd modify the standard OnDeath which applies to all creatures.
1. Have the OnDeath look for an object variable stored on the creature which just died and if the object pointed to is valid, assign the delayed command to the object to erase the do-once variable so it can retrigger.
2. In the creature creation script which runs on the object, have it create the creature then get its own object into a variable and then place that object into the variable on the creature; the same variable which is examined by the on-death script.
If done right, this will be the shortest, neatest and least CPU-time-consuming method to do what you want.
For my part, I was aiming for a system that leaves all default scripts untouched. I wanted something drop-in simple: placeables are always present, easy to rename, and don’t require modifying core event scripts. That way, it stays modular and future-proof without risking compatibility.
Appreciate the insight though — your approach is definitely cleaner from a performance standpoint.