Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: xboxown on Sun 18/06/2023 06:57:14

Title: Issue with monster moving left and right
Post by: xboxown on Sun 18/06/2023 06:57:14
     Hello guys,

I need help in a basic scenario for me. I have a sea monster with two tentacles one on the left and one on the right. Their position is as follow:

Main monster head: 103, 107
Right tentacle: 158,111
Left tentacle: 56,112

This is the current code for the monster moving to the right:

if (!cSecondTentacle.Moving)
   
    cSecondTentacle.Move( 238, cSecondTentacle.y, eNoBlock, eAnywhere);
  if (!cSeaMonster.Moving)
 
    cSeaMonster.Move(190, cSeaMonster.y, eNoBlock, eAnywhere);
  if(!cTentacle.Moving)
   
    cTentacle.Move(145, cTentacle.y, eNoBlock, eAnywhere);

Right now the speed of the monster is super fast. How do I slow it down correctly? Second of all, how do I make it that monster move randomly between 0 to 238 with their tentancles and body in corrent distance between each other. i want it..when it reaches a certain waypoint or point it stops for like 5 seconds before picking another random position to go too. thanks in advance.
Title: Re: Issue with monster moving left and right
Post by: AndreasBlack on Sun 18/06/2023 13:48:32
if you write random inside a script and push f1 you will see what the manual says about how randomness works! As for the monsters speed you can type forexample

cMonsters.SetWalkSpeed(1,1);
Title: Re: Issue with monster moving left and right
Post by: Khris on Mon 19/06/2023 07:35:47
First of all, put this in repeatedly_execute_always:

  cTentacle.x = cSeaMonster.x - 47;
  cSecondTentacle.x = cSeaMonster.x + 57;

This way you don't have to worry about the tentacles at all.

Now implement a timer, like
  if (IsTimerExpired(1)) {
    int newX = cSeaMonster.x;
    // move at least by 30 pixels
    while (newX - SeaMonster.x < 30 || SeaMonster.x - newX < 30) {
      newX = Math.Random(160) + 40; // range 40 - 200
    }
    cSeaMonster.Move(newX, cSeaMonster.y, eNoBlock, eAnywhere);
    SetTimer((Math.Random(5) + 5) * GetGameSpeed()); // 5 - 10 seconds
  }

This code goes inside Room_RepExec, and you need to kick it off by calling SetTimer(1, 1); in room_Load.
Title: Re: Issue with monster moving left and right
Post by: xboxown on Tue 20/06/2023 23:11:29
Thank you guys so much! I will implement the code today and try it out.
Title: Re: Issue with monster moving left and right
Post by: xboxown on Tue 20/06/2023 23:29:15
There is a bug in this line of code:

SetTimer((Math.Random(5) + 5) * GetGameSpeed()); // 5 - 10 seconds

room3.asc(73): Error (line 73): Not enough parameters in call to function


There where other bugs as well. I changed the code to like this:

cTentacle.x = cSeaMonster.x - 47;
  cSecondTentacle.x = cSeaMonster.x + 57;
  if (IsTimerExpired(1)) {
    int newX = cSeaMonster.x;
    // move at least by 30 pixels
    while (newX - cSeaMonster.x < 30 || cSeaMonster.x - newX < 30) {
      newX = Random(160) + 40; // range 40 - 200
    }
    cSeaMonster.Move(newX, cSeaMonster.y, eNoBlock, eAnywhere);
    SetTimer((Random(5) + 5) * GetGameSpeed(), 1); // 5 - 10 seconds
   
  }

However, I don't know how to solve the last bug with the SetTimer saying not enough parameter.
Title: Re: Issue with monster moving left and right
Post by: xboxown on Tue 20/06/2023 23:33:49
I ran the code and I got this error message

(https://i.ibb.co/bK15pdC/error.png)
Title: Re: Issue with monster moving left and right
Post by: Khris on Wed 21/06/2023 00:13:28
Yeah, I typed this up pretty quickly. Try this version:

  if (IsTimerExpired(1)) {
    int newX, diff;
    do {
      newX = Random(160) + 40; // range 40 - 200
      diff = newX - cSeaMonster.x; if (diff < 0) diff = -diff;
    } while (diff < 30); // move by at least 30 pixels
    cSeaMonster.Move(newX, cSeaMonster.y, eNoBlock, eAnywhere);
    SetTimer(1, (Math.Random(5) + 5) * GetGameSpeed()); // 5 - 10 seconds
  }

Edit: code fixed (for good, I hope)
Title: Re: Issue with monster moving left and right
Post by: xboxown on Wed 21/06/2023 01:34:35
But math does not support random.
Title: Re: Issue with monster moving left and right
Post by: xboxown on Wed 21/06/2023 02:34:06
I removed Math from .Random and it works beautifully! THANK YOU FOR THIS AMAZING SOURCE CODE! THANK YOU THANK YOU THANK YOU! You did it! I appreciate all your help! THANK YOU!
Title: Re: Issue with monster moving left and right
Post by: Khris on Wed 21/06/2023 06:17:13
Sorry, JavaScript-Brain :)
Glad it's working now.