Author Topic: Background object move loop aka patrolling?  (Read 276 times)

Background object move loop aka patrolling?
« on: 12 Sep 2021, 20:30 »
Hello again!

I has room with cars on the background and I need to make them move in a loop. I've made few objects with sprite that has multiple flying cars on it. I've need them to move from one side of the screen to another on repeat. For example here's the one of the attempts that looks basic for me, but i've failed

Code: Adventure Game Studio
  1. function room_RepExec()
  2. {
  3.   oFlyCars2.Move (-30, 26, 10, eNoBlock, eAnywhere);
  4.   oFlyCars2.SetPosition (330, 26);
  5.  
  6. }
  7.  


Slasher

  • .
    • I can help with AGS tutoring
    • I can help with scripting
    • I can help with story design
    • Lifetime Achievement Award Winner
    • Slasher worked on one or more games that won an AGS Award!
    •  
    • Slasher worked on one or more games that was nominated for an AGS Award!
Re: Background object move loop aka patrolling?
« Reply #1 on: 13 Sep 2021, 03:43 »
You need to change Move to AddWaypoint command which is also non blocking in the background...
« Last Edit: 13 Sep 2021, 03:46 by Slasher »

arj0n

  • Mittens Vassal
  • art consists in drawing a line somewhere
    • arj0n worked on one or more games that won an AGS Award!
    •  
    • arj0n worked on one or more games that was nominated for an AGS Award!
Re: Background object move loop aka patrolling?
« Reply #2 on: 13 Sep 2021, 08:13 »
You need to change Move to AddWaypoint command which is also non blocking in the background...
AddWaypoint is for characters, not for objects.

Below is an example:
First it sets 3 cars at initial startppoint in the leftside, outside the screen (in room_Load).
Then a loop starts to move 1 car (out of 3 in this example) each time (in room_RepExec):
(1) pick one random car out of the 3
(2) move that car to the rightside outside screen
(3) set moved car back to start position
(4) wait 3 seconds (change that with the waitUntilNextCarStarts value) and loop again

Try something like this (not tested, pseudo code):
Code: Adventure Game Studio
  1. // room script file
  2. bool carIsMoving;
  3. int waitUntilNextCarStarts;
  4.  
  5. function room_Load()
  6. {
  7.   oFlyCars1.X = -50;
  8.   oFlyCars1.Y = 26;
  9.  
  10.   oFlyCars2.X = -50;
  11.   oFlyCars2.Y = 26;
  12.  
  13.   oFlyCars3.X = -50;
  14.   oFlyCars3.Y = 26;
  15.  
  16.   carIsMoving = false;
  17.   waitUntilNextCarStarts = 120; //40 is 1 second
  18. }
  19.  
  20. function room_RepExec()
  21. {
  22.  
  23.   if (carIsMoving == false) int ranCar = Random (2);
  24.  
  25.   if (ranCar == 0)
  26.   {
  27.     oFlyCars1.Move (380, 26, 10, eNoBlock, eAnywhere);
  28.     carIsMoving = true;
  29.   }
  30.   else if (ranCar == 1)
  31.   {
  32.     oFlyCars2.Move (380, 26, 10, eNoBlock, eAnywhere);
  33.     carIsMoving = true;
  34.   }
  35.   else if (ranCar == 2)
  36.   {
  37.     oFlyCars3.Move (380, 26, 10, eNoBlock, eAnywhere);
  38.     carIsMoving = true;
  39.   }
  40.  
  41.   if ((oFlyCars1.X == 380) || (oFlyCars2.X == 380) || (oFlyCars3.X == 380))
  42.   {
  43.     oFlyCars1.SetPosition (-50, 26);
  44.     oFlyCars2.SetPosition (-50, 26);
  45.     oFlyCars3.SetPosition (-50, 26);
  46.     SetTimer (1, waitUntilNextCarStarts);
  47.   }
  48.  
  49.   if (IsTimerExpired (1)
  50.   {
  51.     carIsMoving = false;
  52.   }
  53. }
« Last Edit: 13 Sep 2021, 09:04 by arj0n »

Slasher

  • .
    • I can help with AGS tutoring
    • I can help with scripting
    • I can help with story design
    • Lifetime Achievement Award Winner
    • Slasher worked on one or more games that won an AGS Award!
    •  
    • Slasher worked on one or more games that was nominated for an AGS Award!
Re: Background object move loop aka patrolling?
« Reply #3 on: 13 Sep 2021, 09:33 »
You need to change Move to AddWaypoint command which is also non blocking in the background...
AddWaypoint is for characters, not for objects.
Yes, of course..  (nod)

Matti

Re: Background object move loop aka patrolling?
« Reply #4 on: 13 Sep 2021, 10:51 »
You don't need a variable to check whether the cars are moving, you can use Object.Moving. You can also shorten the code if you make the car objects consecutive and use their IDs.

Untested:

Code: Adventure Game Studio
  1.   // this goes into the RepExec, you need to set the car's coordinates in room_load like arj0n did
  2.   for (int i = 1; i < 4;  i++) // assuming that the object numbers are 1,2,3
  3.   {
  4.     if (object[i].Moving == false)
  5.     {
  6.       if (object[i].X == -30) object[i].X = 330;
  7.       r = random(100); // randomizing the new loop for each car instead of having a fixed timer
  8.       if (r == 10)
  9.         object[i].Move (-30, 26, 10, eNoBlock, eAnywhere);
  10.     }
  11.   }

For example here's the one of the attempts that looks basic for me, but i've failed

That's because you're constantly resetting the car's position so it never has a chance to actually move.
« Last Edit: 13 Sep 2021, 11:22 by Matti »

DeadSuperHero

  • There's no sense crying over spilled tears.
    • I can help with backgrounds
    • I can help with characters
    • I can help with making music
    • I can help with scripting
Re: Background object move loop aka patrolling?
« Reply #5 on: 16 Sep 2021, 00:46 »
This is just my 2 cents, but...given that you want to have multiple things moving around in the background, would it maybe make sense to use characters instead of objects?
Check out my game: Life is a Party!

The fediverse needs great indie game developers! Find me there!