Author Topic: Npc Behaviour when leaving and re-entering room. [SOLVED]...Yay!  (Read 1057 times)

RetroJay

  • Posts: 537
  • Frankly, my dear, I'm a Kerbal and Proud of it.
    • RetroJay worked on a game that was nominated for an AGS Award!
Hi Peeps.

In my game I have npc's that walk from left to right and up and down.
I have the code below inside the rooms Rep_Exc_Always.

Code: Adventure Game Studio
  1.    // NPC MOVEMENT SCRIPT.
  2.    //---------------------
  3.    // This section makes GHOSTS and other CREATURES move.
  4.    if (!cZomb.Moving) {
  5.      if (cZomb.x < 200) {
  6.        // If the Zombie is on the left hand side of the screen,
  7.        // start it moving towards the right.
  8.        cZomb.Walk(612, cZomb.y, eNoBlock, eAnywhere);
  9.        }
  10.        else {
  11.          // Otherwise, move it towards the left
  12.          cZomb.Walk(28, cZomb.y, eNoBlock, eAnywhere);
  13.        }
  14.    }
  15.  
  16.    if (!cZomb2.Moving) {
  17.      if (cZomb2.x < 200) {
  18.        // If the Zombie is on the left hand side of the screen,
  19.        // start it moving towards the right.
  20.        cZomb2.Walk(398, cZomb2.y, eNoBlock, eAnywhere);
  21.        }
  22.        else {
  23.          // Otherwise, move it towards the left.
  24.          cZomb2.Walk(162, cZomb2.y, eNoBlock, eAnywhere);
  25.        }
  26.    }
  27.  
  28.    if (!cZomb3.Moving) {
  29.      if (cZomb3.x < 300) {
  30.        // If the Zombie is on the left hand side of the screen,
  31.        // start it moving towards the right.
  32.        cZomb3.Walk(612, cZomb3.y, eNoBlock, eAnywhere);
  33.        }
  34.        else {
  35.          // Otherwise, move it towards the left.
  36.          cZomb3.Walk(286, cZomb3.y, eNoBlock, eAnywhere);
  37.        }
  38.    }
  39.  
  40.    if (!cGhost.Moving) {
  41.      if (cGhost.x < 100) {
  42.        // If the Ghost is on the left hand side of the screen,
  43.        // start it moving towards the right.
  44.        cGhost.Walk(582, cGhost.y, eNoBlock, eAnywhere);
  45.        }
  46.        else {
  47.          // Otherwise, move it towards the left
  48.          cGhost.Walk(54, cGhost.y, eNoBlock, eAnywhere);
  49.        }
  50.    }
  51.  
  52.    if (!cGhost2.Moving) {
  53.      if (cGhost2.x < 100) {
  54.        // If the Ghost is on the left hand side of the screen,
  55.        // start it moving towards the right.
  56.        cGhost2.Walk(607, cGhost2.y, eNoBlock, eAnywhere);
  57.        }
  58.        else {
  59.          // Otherwise, move it towards the left.
  60.          cGhost2.Walk(32, cGhost2.y, eNoBlock, eAnywhere);
  61.        }
  62.    }
  63.  
  64.    if (!cSpidey.Moving) {
  65.      if (cSpidey.y < 300) {
  66.        // If the Spider is towards the bottom half of the screen,
  67.        // start it moving towards the top.
  68.        cSpidey.Walk(cSpidey.x, 300, eNoBlock, eAnywhere);
  69.        }
  70.        else {
  71.          // Otherwise, move it towards the bottom.
  72.          cSpidey.Walk(cSpidey.x, 264, eNoBlock, eAnywhere);
  73.        }
  74.    }
  75.  
  76.    if (!cSpidey2.Moving) {
  77.      if (cSpidey2.y < 300) {
  78.        // If the Spider is towards the bottom half of the screen,
  79.        // start it moving towards the top.
  80.        cSpidey2.Walk(cSpidey2.x, 300, eNoBlock, eAnywhere);
  81.        }
  82.        else {
  83.          // Otherwise, move it towards the bottom.
  84.          cSpidey2.Walk(cSpidey2.x, 264, eNoBlock, eAnywhere);
  85.        }
  86.    }
  87.  
  88.    if (!cMummy1.Moving) {
  89.      if (cMummy1.x < 200) {
  90.        // If the Zombie is on the left hand side of the screen,
  91.        // start it moving towards the right.
  92.        cMummy1.Walk(258, cMummy1.y, eNoBlock, eAnywhere);
  93.        }
  94.        else {
  95.          // Otherwise, move it towards the left
  96.          cMummy1.Walk(28, cMummy1.y, eNoBlock, eAnywhere);
  97.        }
  98.    }
  99.  }
  100.   // END OF GHOST AND CREATURE MOVEMENTS SCRIPT.
  101.   //--------------------------------------------

This works just great.
The problem is when I leave this room and re-enter.
If I leave the room when npc's are half way across the screen and then re-enter, the npc's scurry back to their start positions. (This ruins the timing of creatures for this room).

I tried to make a 'Global Bool' and moved my movement script to GlobalScript.asc (Rep_Exc_Always) and call it from the rooms Rep_Exc_Always.
I got this to work but the npc's still behave the same.

Now I know why this is happening but is there a way to either stop them in their tracks, when the player leaves the room, and to continue in the correct direction when player re-enters.
Or to continuously have them moving even when the player is not in that room?

If I could just get this to work, it would be a great leap forward.

Any help would be much appreciated.

Yours.
Jay.
« Last Edit: 21 Aug 2014, 23:32 by RetroJay »
  Master Decorator

Khris

  • Posts: 10,676
  • having to deal with what games are going through
    • Lifetime Achievement Award Winner
    • I can help with play testing
    • I can help with scripting
    • I can help with translating
    • Khris worked on a game that was nominated for an AGS Award!
Re: Npc Behaviour when leaving and re-entering room.
« Reply #1 on: 21 Aug 2014, 02:43 »
You could set all NPC's initial positions in room_Load.

Code: Adventure Game Studio
  1. // above room_Load
  2. int npcx[20], npcy[20];
  3.  
  4. void SetupNPCs() {
  5.   bool game_starts = Game.DoOnceOnly("store NPC positions");
  6.   Character *npc;
  7.   int ch = 0;
  8.   while (ch < Game.CharacterCount) {
  9.     npc = character[ch];
  10.     if (npc.Room == player.Room && npc != player) {
  11.       // if this is the start of the game, store all initial positions for later use
  12.       if (game_starts) { npcx[ch] = npc.x; npcy[ch] = npc.y; }
  13.       // otherwise, move characters back to initial positions
  14.       else { npc.x = npcx[ch]; npc.y = npcy[ch]; }
  15.     }
  16.     ch++;
  17.   }
  18. }
  19.  
  20.   // inside room_Load
  21.   SetupNPCs();

This is just a quick fix; if you want them to continue where they left off it's going to get slightly more complicated.
« Last Edit: 21 Aug 2014, 21:37 by Khris »

RetroJay

  • Posts: 537
  • Frankly, my dear, I'm a Kerbal and Proud of it.
    • RetroJay worked on a game that was nominated for an AGS Award!
Re: Npc Behaviour when leaving and re-entering room.
« Reply #2 on: 21 Aug 2014, 04:08 »
Hi Khris.

Sorry, I pasted your script but am getting an error with line 6 in your example.
AGS says:
Quote
Cannot declare local instance of managed type
Whatever the hell that means.???

I have this, at the moment.
Code: Adventure Game Studio
  1. int npcx[20], npcy[20];
  2.  
  3. void SetupNPCs() {
  4.   bool game_starts = Game.DoOnceOnly("store NPC positions");
  5.   Character npc;
  6.   int ch = 0;
  7.   while (ch < Game.CharacterCount) {
  8.     npc = character[ch];
  9.     if (npc.Room == player.Room && npc != player) {
  10.       // if this is the start of the game, store all initial positions for later use
  11.       if (game_starts) { npcx[ch] = npc.x; npcy[ch] = npc.y; }
  12.       // otherwise, move characters back to initial positions
  13.       else { npc.x = npcx[ch]; npc.y = npcy[ch]; }
  14.     ch++;
  15.   }
  16. }
  17.  
  18.  
  19. function room_Load() {
  20.   SetupNPCs();
  21.   gui[0].Visible = true;
  22.   mouse.Visible = false;   // This disables MOUSE controls.
  23.   RemoveWalkableArea(2);
  24.   oGrandfather_Clock.SetView(6, 2);
  25.   oGrandfather_Clock.Animate(2, 19, eRepeat, eNoBlock, eForwards);
  26.   if (cDan.PreviousRoom == 4) {
  27.     cDan.FaceLocation (640, 400);
  28.   }
  29.   else {
  30.     cDan.FaceLocation(0, 400);
  31.   }
  32. }

This looks very interesting and, more than likely, just what I need.
It must be said that I don't understand some of it, though.

Yours.
Jay.
« Last Edit: 21 Aug 2014, 04:31 by RetroJay »
  Master Decorator

Khris

  • Posts: 10,676
  • having to deal with what games are going through
    • Lifetime Achievement Award Winner
    • I can help with play testing
    • I can help with scripting
    • I can help with translating
    • Khris worked on a game that was nominated for an AGS Award!
Re: Npc Behaviour when leaving and re-entering room.
« Reply #3 on: 21 Aug 2014, 15:11 »
Sorry, I forgot the asterisk:
Code: Adventure Game Studio
  1.   Character *npc;

The code iterates over the NPCs and when it is called for the first time, it stores their position in variables.
Whenever the room is entered after that, the character's positions are reset to the stored values.

RetroJay

  • Posts: 537
  • Frankly, my dear, I'm a Kerbal and Proud of it.
    • RetroJay worked on a game that was nominated for an AGS Award!
Re: Npc Behaviour when leaving and re-entering room.
« Reply #4 on: 21 Aug 2014, 21:02 »
Hi Khris.

I'm so sorry but I can't get this to work.:(
This game is really doing my head in.

I hope you don't mind but I have sent you a PM regarding this matter.

Once this is resolved I will post the solution to my problem.
It may help other people to achieve what I am so obviously failing to achieve.

Yours.
Jay. 
  Master Decorator

Khris

  • Posts: 10,676
  • having to deal with what games are going through
    • Lifetime Achievement Award Winner
    • I can help with play testing
    • I can help with scripting
    • I can help with translating
    • Khris worked on a game that was nominated for an AGS Award!
Re: Npc Behaviour when leaving and re-entering room.
« Reply #5 on: 21 Aug 2014, 21:38 »
I also forgot the closing brace for the if block... sorry :P :-[
There should be an additional closing brace directly above the "ch++;" line.

Btw, this is a nice example why proper indentation is awesome; line 14 moves back left without a closing brace, so that's where it is missing.
« Last Edit: 21 Aug 2014, 21:43 by Khris »

RetroJay

  • Posts: 537
  • Frankly, my dear, I'm a Kerbal and Proud of it.
    • RetroJay worked on a game that was nominated for an AGS Award!
Re: Npc Behaviour when leaving and re-entering room.
« Reply #6 on: 21 Aug 2014, 23:31 »
Hi Khris.

Ha Ha!
That has done the trick.
It works beautifully.:~(:~( (These are tears of joy.)

Please, Khris, no need to apologise.
I couldn't have got this far without your help.
By the time I'm finished I might just as well say 'Game created by Khris' in the credits.;)

All I need to do now is remember where all the keys went on my yekdraob and purchase a new mouse (It landed somewhere in the neighbours garden.) :P

As always, Khris, Thank you for your help and time.
It is much appreciated.

Yours.
Jay.
  Master Decorator