Skip to content

Heartbeat script question

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);
    }
}

}
}

Comments

  • MelkiorMelkior Member Posts: 222
    It may be a bit late, but the answer is that it depends a lot on how many objects you have in the area. For one thing, I wouldn't be trying to look through all objects in the area (which would include decorations). What I'd be trying to do is to find creatures only. That would speed up the loop by quite a lot.
    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.
Sign In or Register to comment.