Spawning Random NPCs for background/foreground crowds

Started by strangeDetective, Tue 21/02/2017 22:24:21

Previous topic - Next topic

strangeDetective

Hoping someone can give me some guidance or point me in the right direction.

Wanted to do what I thought was a relatively simple event, to have a random event which spawns an NPC to walk across the screen in the background/foreground as random passerbys. Saw this in some of Shardlight's crowd scenes.

I figured it would go something like this:

Code: ags

function room_RepExec()
{

int ran=Random(1);

if (ran ==0 ) {
cFemale01.ChangeRoom(230, 960, 335, eDirectionLeft);
cFemale01.Walk (20, 590, eNoBlock, eWalkableAreas);
cFemale01.ChangeRoom (200);
}
else if (ran==1) {
  cFemale01.ChangeRoom(230, 20, 590);
cFemale01.Walk (960, 335, eNoBlock, eWalkableAreas);
cFemale01.ChangeRoom (200);
}
else {
  //DO NOTHING FOR NOW...;
}
}


This was a rushed test to see if it worked, but nothing happens, but I don't understand it exactly.

I thought that this would make a NPC appear and walk across screen spawning at different location depending on if the random number was 0 or 1, then move them out of the room again.

Does anyone have an idea how I can fix this to work? Thanks in advance for any help or replies.


Crimson Wizard

#1
As opposed to eBlock, when you order a eNoBlock action, it schedules the action and continues to run commands in same function. The actual walk only begins when the function ends.
This is why your character goes to different room immediately after ordered to walk.

The usual solution is to start non-blocking action in a function and let it go, and then check for its completion in "repeatedly execute" every time its called.

Something like:
Code: ags

function room_RepExec()
{
   if (cFemale01.Room != 230) // NPC is in the different room
   {
       cFemale01.ChangeRoom(230, 960, 335, eDirectionLeft);
       cFemale01.Walk (20, 590, eNoBlock, eWalkableAreas);
   }
   else // NPC is in current room (230)
   {
       if (!cFemale01.Moving) // has stopped walking
           cFemale01.ChangeRoom (200); // move her back to another room
   }
}

The example above makes an infinite loop of same NPC appearing, walking, and dissapearing, and over and over again.

Try to think of room_RepExec as a place where you check for the state of an entity periodically, like: is it walking? did it make it to the end yet? And what should you do if particular check returns positive.

strangeDetective

Thanks this works great for what I wanted, and looks like it will be pretty easy to randomize and scale up. :)

SMF spam blocked by CleanTalk