GetRandomObjectByType(int, float)

Get a random nearby object within the specified distance with the specified type.

object GetRandomObjectByType(
    int nObjType,
    float fMaxDistance
);

Parameters

nObjType

OBJECT_TYPE_*

fMaxDistance

Used to determine the nNth object to look for.


Description

Get a random nearby object within the specified distance of the specified type.



Remarks

Returns a valid object on success, OBJECT_INVALID on failure.

This uses GetNearestObject to find the object.

You can use the DISTANCE_* constants if you want for fMaxDistance.

Known Bugs

There are some quirks in how fMaxDistance is used to find the requested object. If fMaxDistance is set to DISTANCE_SHORT (3.0), then d2 is sent to GetNearestObject as the nNth parameter. If fMaxDistance is set to DISTANCE_LARGE (10.0), then d4 is sent. ALL other values of fMaxDistance result in d6 being sent.

Meaning, if you send in a distance of DISTANCE_TINY (1.0) you could actually end up with an object that is 6.0 distance away.

Requirements

#include "x0_i0_anims"


Version

???

Example

// Find a nearby creature and noogie them.  A silly example but fun to watch.
#include "x0_i0_anims"

void main()
{
    // Find a random creature within 2 meters.
    object oTarget = GetRandomObjectByType(OBJECT_TYPE_CREATURE, 2.0);

    // Validate that oTarget was found.
    if(OBJECT_INVALID != oTarget)
    {
        // Check to see if oTarget is not hostile to us and that we have not noogied them already.
        if(FALSE == GetIsEnemy(OBJECT_SELF, oTarget) && FALSE == GetLocalInt(oTarget, "BEEN_NOOGIED"))
        {
            // Move closer to oTarget.
            AssignCommand(OBJECT_SELF, ActionMoveToObject(oTarget, TRUE, 0.5));

            // Sass them!
            AssignCommand(OBJECT_SELF, SpeakString("*NOOGIES*  Ha ha!  Gotcha!"));

            // Now run before they can noogie you back!
            AssignCommand(OBJECT_SELF, ActionMoveAwayFromObject(oTarget, TRUE, 25.0));

            // Set a flag on the player so they only get noogied once.
            SetLocalInt(oTarget, "BEEN_NOOGIED", TRUE);
        }
    }
}

See Also

functions:  GetNearestObject | GetNearestObjectByTag | GetRandomObjectByTag
constants:  DISTANCE_* | OBJECT_TYPE_*
categories:  Get Data from Object Functions


author: Baragg, editor: Mistress