Question about Random() Function

Not quite sure I fully understand how to use it properly. I’ve got a custom function that is supposed to spawn Random(3) dire rats, but it seems to always only spawn 2. What gives?

object oSpawn;

#include "nw_i0_generic"

void MultiSpawn()
{
    string sCreature = "ratdire002";
    int nRandom = Random(3);
    location lLoc = GetLocation(GetWaypointByTag("RAT_LOC1"));
    object oSpawn = CreateObject(OBJECT_TYPE_CREATURE, sCreature, lLoc);
    AssignCommand(oSpawn, SetSpawnInCondition(nRandom, TRUE));
}

void main()
{
    object oPC = GetEnteringObject();
    if (!GetIsPC(oPC)) return;

    string sCreature = "ratdire002";

        location lLoc = GetLocation(GetWaypointByTag("RAT_LOC1"));

    oSpawn = CreateObject(OBJECT_TYPE_CREATURE, sCreature, lLoc);

    DelayCommand(0.5, MultiSpawn());
    DelayCommand(1.0, AssignCommand(oSpawn, ActionMoveToObject(oPC)));
    AssignCommand(oSpawn, DetermineCombatRound(oPC));
}
Thanks for any help. :)

EDIT: Not sure why this is in gener natter? Must of clicked wrong. Apologies…

Random(x); returns numbers in range of 0 to x-1.

if you want to return 1-x then you need to use Random(x)+1; or in your case you can simply use d3()

if you want to return 0-x then you need to use Random(x+1);

1 Like

Shadoooow has explained the Random-function perfectly, but I’m puzzled, that you expect that this would spawn multiple copies of a NPC.

Multispawn will spawn just one creature. There is another CreateObject in main(), so the whole scripting would always spawn two rats.

Try the following:

// Epic "rats in the basement" spawner

void main()
{
    object oPC = GetEnteringObject();
    if (!GetIsPC(oPC)) return;

// Maybe add a "do once" here??

    string sCreature = "ratdire002";
    location lLoc = GetLocation(GetWaypointByTag("RAT_LOC1"));

    object oSpawn;
    int i, n = d4();
    for (i=1; i<=n; i++)
      {
        oSpawn = CreateObject(OBJECT_TYPE_CREATURE, sCreature, lLoc);
        DelayCommand(0.2, AssignCommand(oSpawn, ActionAttack(oPC)));
// with rats, ActionAttack should be enough, save the rats have a feat to go berserk.
      }      
}
4 Likes

Yeah, I think i might misunderstand how to use SetSpawnInCondition() as well. I thought that stated to spawn Randon (3) rats, not a copy of the PC. What the hell am I doing? Did I mention I’m not a coder…? :slight_smile:
I suppose I simply could have painted an encounter of rats down and let the game handle it, but I wanted to challenge myself by attempting to create a custom function. Epic fail!!! :frowning:

Don’t give up.

2 Likes

You’d need to use a loop in order to spawn multiple creatures since the function only creates a single creature at a time. Only stackable items can have multiples created at a time.
There is an alternative but it requires putting some extra coding into the OnSpawn event for the creature, and that gets even more complicated.