Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: hermidar on Tue 23/08/2016 14:15:41

Title: Help with a moving object
Post by: hermidar on Tue 23/08/2016 14:15:41
Hello to everybody, my english is not too good, but a will try to explain my problem. my problem is really stupid, im sure.
In my room, if you look the low-right corner you will see a little rat (is the object "oRat")

what i want:

the rat is moving around the room always until the character interact with it

My problem:

the rat not moving at all
i try
Code (ags) Select

oRat.move(550,266,4, eNoBlock, eAnywhere);   


nothing happens

i try other codes i see here in the forums, nothing...
i try made a rat character (cRat) and try with code walk... move... fuction repeatedlyexecute... the rat doesnt move, im done.

this is not a hardly thing to do... whats my problem?
Title: Re: Help with a moving object
Post by: Khris on Tue 23/08/2016 15:07:35
AGS is event based, which means that line of code needs to go inside a function that is called by AGS at some point.

To use repeatedly_execute in a room, you need to add the event to the room in the room editor. AGS will add a function called "room_RepExec()".
You can now put lines inside the function, and they will run 40 times per second.

Try this:

function room_RepExec() {
  // if oRat isn't moving, 20% chance per second of moving elsewhere
  if (!oRat.Moving && Random(100 * GetGameSpeed()) < 20) {
    oRat.Move(Random(Room.Width), Random(Room.Height), 3);
  }
}
Title: Re: Help with a moving object
Post by: hermidar on Tue 23/08/2016 18:13:31
Khris, your code works awesome!!!!!! but... what the h*** are you whrite in it??, i dont understand what you did, and a want to learn...

how can i change view? or make animations? i have the views...


and, of course, thank you so much.
Title: Re: Help with a moving object
Post by: Matti on Tue 23/08/2016 20:23:02
Changing views and doing animations is very basic stuff, so I suggest checking the manual and also densming's video tutorials (https://www.youtube.com/user/densming).
Title: Re: Help with a moving object
Post by: Khris on Tue 23/08/2016 22:02:43
It's not that complicated actually.
Random(x) returns a random number between 0 and x.

Here's a long version
  if (oRat.Moving == false) {
    // Here I'm calculating a 20% chance using "Random(100) < 20"
    // since the code is called 40 times per second though, I change that
    // to Random(4000), otherwise the rat would continue moving almost instantly
    // GetGameSpeed() is the number of frames per second, which is the number of
    // times room_RepExec is called per second
    int random_number = Random(100 * GetGameSpeed());   
    if (random_number < 20) { // 20% per second
      int random_x_coordinate = Random(Room.Width);
      int random_y_coordinate = Random(Room.Height);
      oRat.Move(random_x_coordinate, random_x_coordinate, 3);
    }
  }


Regarding views and animations, please refer to the manual first.
Make sure to read through the scripting section from top to bottom at least once, this makes it much easier to find commands in the future.

The basic idea is:
if you want to change a character's view, go to the scripting section for character, then look for a command called SetView or ChangeView, or look for a property called View.
It's not always this straightforward, but you'll often come close.