Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: KamikazeHighland on Thu 05/11/2009 03:40:56

Title: [SOLVED] (Not) Random NPC Walking
Post by: KamikazeHighland on Thu 05/11/2009 03:40:56
What I want is for an npc character to walk around a square, but not just walking the same loop over and over.  My idea is that each time it hits a corner, it'll randomly decide where to go from there.

My corners are at A. 80,80 (NW) B. 200,80 (NE) C. 80,200 (SW) and D. 200,200 (SE).  So lets say it starts at point A.  Let's say each is true of the points.

A. 100% chance of walking to B.
B. 25% chance of walking back to A, 75% chance of walking to D.
D. 75% chance of walking to C, 25% chance of walking to B.
And C. 25% chance of walking straight to A, and a 75% chance of waiting 5 seconds first. ::)


I know I could make it completely random, but I've played Tale of Two Kingdoms and I don't want my npcs going all over.  Besides, I'd like to do other things with percent/fractional odds, and I don't know how to do a rand statement when the odds are 1:4.

Think you can help me? 8)
Title: Re: (Not) Random NPC Walking
Post by: discordance on Thu 05/11/2009 03:50:43
As far as percent odds go, you can just do this:


int chance = Random(100) + 1;

if (chance < 25) {
  // 25% chance
}
else {
  //other 75%
}


Then just check to see which point you're at and set up the odds accordingly.
Title: Re: (Not) Random NPC Walking
Post by: monkey0506 on Thu 05/11/2009 05:08:45
To clarify Random returns between 0 and MAX inclusive so "Random(100) + 1" would actually result in a range from 1-101 inclusive. If you want 1-100 then you would use "Random(99) + 1". ;)
Title: Re: (Not) Random NPC Walking
Post by: discordance on Thu 05/11/2009 21:09:02
Whoops, sorry. Wasn't thinking  :)
Title: Re: (Not) Random NPC Walking
Post by: KamikazeHighland on Fri 06/11/2009 00:38:28
Thanks! ;D

I haven't actually had time to fool around with it yet, but that is exactly what I was looking for.  Once I have it working I'll post the entire loop. 8)

Edit:
Okay, well tried getting the character to move.  I actually haven't worked with getting a character to move around in a room I've made from scratch.  I can get the Roger character from the Demo to move around, but since characters are initialized separately under character settings, they always say the character was already initialized, but it still says "unexpected cChar1".

How do you REinitialize a character at the beginning of an empty room script? ???
Title: Re: (Not) Random NPC Walking
Post by: monkey0506 on Fri 06/11/2009 04:37:50
What do you mean by initializing a character? The character's script name is a global variable which is accessible from every script, room scripts included. So if your character is named cChar1 then that variable will already exist in the room script.

If it's saying "unexpected cChar1" that sounds to me like you're trying to do something with the character outside of any functions. In order for AGS to know when you want to perform the action it has to be inside of a function. For example you might want to do something when the room is loaded for the first time, every time the player enters the room, etc.

You need to decide when you want the function to be called and then place that inside the appropriate event function.
Title: Re: (Not) Random NPC Walking
Post by: KamikazeHighland on Fri 06/11/2009 06:30:32
Well, no, cause when I make a function for cChar1.walk(x,y,eNoBlock, eAnywhere) nothing happens.  They load in the room at the coordinates I put in their character properties.

i.e.
function hHotspot1_AnyClick()
{
cCarl.Walk(80, 200, eNoBlock, eAnywhere);
}

I'd prefer them to just start moving once the room loads, but putting in First time enters room, or any of the room functions, confuses it.  That's why I was confused that there was no way to re-initialize the character before having it do something.
Title: Re: (Not) Random NPC Walking
Post by: Wonkyth on Fri 06/11/2009 10:02:15
If you want them to move when the room loads, then put it in the Room_Load event.
Title: Re: (Not) Random NPC Walking
Post by: Lufia on Fri 06/11/2009 14:57:39
Wouldn't you put it in the room repeatedly_execute if the character has to keep moving around?
Title: Re: (Not) Random NPC Walking
Post by: monkey0506 on Sat 07/11/2009 20:31:41
It sounds to me like what you're wanting to do would be something like this:

int xx[4]; // 4 different points, x co-ordinate
int yy[4]; // y co-ordinate
int timer = 0;

function room_Load() {
  xx[0] = 80; // 80,80 (NW)
  yy[0] = 80;
  xx[1] = 200; // 200,80 (NE)
  yy[1] = 80;
  xx[2] = 80; // 80,200 (SW)
  yy[2] = 200;
  xx[3] = 200; // 200,200 (SE)
  yy[3] = 200;
  int loc = Random(3);
  cChar1.x = xx[loc];
  cChar1.y = yy[loc];
}

function room_RepExec() {
  if (timer) { // if the character is waiting...
    timer--; // decrease our timer
    if (!timer) cChar1.Walk(xx[0], yy[0], eNoBlock, eAnywhere);
    return;
  }
  if (cChar1.Moving) return;
  if ((cChar1.x == xx[0]) && (cChar1.y == yy[0])) { // point A
    // 100% chance of walking to B
    cChar1.Walk(xx[1], yy[1], eNoBlock, eAnywhere);
  }
  else if ((cChar1.x == xx[1]) && (cChar1.y == yy[1])) { // point B
    // 25% chance of walking to A
    // 75% chance of walking to D
    int chance = Random(99) + 1; // 1-100
    if (chance <= 25) cChar1.Walk(xx[0], yy[0], eNoBlock, eAnywhere);
    else cChar1.Walk(xx[3], yy[3], eNoBlock, eAnywhere);
  }
  else if ((cChar1.x == xx[2]) && (cChar1.y == yy[2])) { // point C
    // 25% chance of walking to point A
    // 75% chance of waiting 5 seconds first
    int chance = Random(99) + 1;
    if (chance <= 25) cChar1.Walk(xx[0], yy[0], eNoBlock, eAnywhere);
    else timer = (GetGameSpeed() * 5);
  }
  else if ((cChar1.x == xx[3]) && (cChar1.y == yy[3])) { // point D
    // 75% chance of walking to C
    // 25% chance of walking to B
    int chance = Random(99) + 1; // 1-100
    if (chance <= 75) cChar1.Walk(xx[2], yy[2], eNoBlock, eAnywhere);
    else cChar1.Walk(xx[1], yy[1], eNoBlock, eAnywhere);
  }
}


I'm not sure if you wanted more than just cChar1 walking around like this. If so it might be worth adding a few custom functions (i.e., for checking if they are stopped at a given point, move them to a given point, etc.).

But I think what you were talking about with initializing the characters was defining their entry point in the room, yes? In the above code I've set the entry point for cChar1 to a random point every time the player enters the room, and then the aforementioned pseudo-random logic will apply the appropriate movements.

Edit: Fixed "cChar2" typo. Thanks Lufia.
Title: Re: (Not) Random NPC Walking
Post by: KamikazeHighland on Sun 08/11/2009 04:31:18
Thanks!  Lol, I had given up trying to get the character to move and have just been working on the walking cycle.

When I said I couldn't figure out how to initialize the character, what I was having trouble with was that although the starting point was in the character page, and the character was initialized with the game, I couldn't figure out how to call the character for use into the code I was trying, since I couldn't re-initialize him (ags pointed out that was redundant) and when I called him to walk in my function nothing happened.  But looking at your code I see how AGS handles it.

I've just started using AGS, and I feel like it's going to be far more user-friendly than the other programs. ;)


[Edit]:  Sigh.  I still can't get the character to move.  All it does is start at the coordinates in its character page and stay there, no matter which coordinates I start it at.  I like the idea about him starting at a random location everytime I enter the room, but even changing that didn't make him move... :-\

Thanks again for your help! 8)
Title: Re: (Not) Random NPC Walking
Post by: Wonkyth on Sun 08/11/2009 12:59:20
Someone should whip up a module that handles this kind of action, as I'm sure it would be used to pieces.

And yes, AGS is definitely very user friendly.
I've tried Game Maker, Five different RPG making tools and many other game making tools, but so far, I haven't found one as good as AGS.
I'd take it for 9/10 situations.
Title: Re: (Not) Random NPC Walking
Post by: TerranRich on Sun 08/11/2009 16:42:46
RickJ created a background character walking module some years ago. Not sure if that would help.
Title: Re: (Not) Random NPC Walking
Post by: Lufia on Sun 08/11/2009 17:04:22
Well, I copy pasted mokey's code as such to see if there was a problem, and apart from a typo (cChar2 instead of cChar1) it works fine. My guess is that you haven't assigned the two functions to the room events: room before fade-in and repeatedly execute, in order.

As for AGS being user-friendly... It requires you to learn a programming language, so it's not exactly destined to a broad audience. Multimedia Fusion 2 is a polyvalent tool that doesn't require "real" programming, I'd call that user-friendly.
Title: Re: (Not) Random NPC Walking
Post by: KamikazeHighland on Sun 08/11/2009 18:08:37
Success! ;D

This whole time, all I had to do was go to the room events list and click the "..." to make the game recognize the script. ;)


Thanks everyone! 8)
Title: Re: (Not) Random NPC Walking
Post by: Wonkyth on Mon 09/11/2009 11:05:46
I apologise in advance for hijacking this thread, but as the problem is solved...

Quote from: Lufia on Sun 08/11/2009 17:04:22
Multimedia Fusion 2 is a polyvalent tool that doesn't require "real" programming, I'd call that user-friendly.
Personally, I think that learning the little bit of simple coding required could be done by a six-year-old(e.g. my brother).
Besides, once you've put a good deal of time and effort into game design, it doesn't take much to learn a little script.