I'm having some troubles with a function I've written. Basically it turns an object into a chicken that randomly around walks and pecks at the ground.
The function is called in a room's repeatedly_execute. It works just how I want it to, apart from one small problem. When the chicken stops walking, I need the walking animation to stop.
The AGS scripting language is new to me and I can't for the life of me figure out how I might go about this. If I could somehow store the destination coordinates for each object I could compare the current coordinates and call object[num].StopAnimating(); when they match.
There's probably a much easier way of doing this, but here's the function: :P
function Pecker(int num) {
// If chicken is not moving
if (object[num].Moving == false) {
int rnd = Random(100);
// Move
if (rnd == 1) {
// Get new coordinates
int new_x = object[num].X + PosNegInt(Random(20));
int new_y = object[num].Y + PosNegInt(Random(10));
// Get direction object is facing, start animating
if (new_x > object[num].X) {
object[num].SetView(5, 0);
object[num].Animate(0, 4, eRepeat, eNoBlock);
}
else {
object[num].SetView(5, 1);
object[num].Animate(1, 4, eRepeat, eNoBlock);
}
// Move to new coordinates
object[num].Move(new_x, new_y, 1, eNoBlock, eWalkableAreas);
}
// Peck ground
else if (rnd > 98) {
if (FacingLeft(object[num].Loop)) {
object[num].SetView(5, 8);
object[num].Animate(8, 4, eOnce, eNoBlock);
}
else {
object[num].SetView(5, 9);
object[num].Animate(9, 4, eOnce, eNoBlock);
}
}
}
}
You're making things difficult for yourself by using an object and not a character for the chicken. But check out the Object.Moving property.
It would have been much easier using a character, but am I right in thinking if I wanted 3/4 chickens running about I'd need to create a new character for each of them?
That is correct yes, but then you could use the same characters in multiple rooms.
I think I may have misunderstood your first question about the coordinates - my idea was that once the object reaches the coordinate, it's no longer moving - but the function is already cheking for (object[num].Moving == false). So why not just StopAnimating there, if the objects current view is the walking view?
Edit: By the way, you can have 300 characters in a game, so creating 3 or 4 chicken characters aren't a big deal since you're just assigning the same view to all the characters.
So when should it stop animating? If rnd isn't equal to 1, but is less than 98? Just add another condition:
function Pecker(int num) {
// If chicken is not moving
if (object[num].Moving == false) {
int rnd = Random(100);
// Move
if (rnd == 1) {
// Code as is
}
// Peck ground
else if (rnd > 98) {
//Code as is
}
//Don't move or peck
else object[num].StopAnimating();
}
}
Or have I misread the conditons?
Assuming Pecker(x) is called from rep_ex, it should work OK. One thing, though, you might need to make the check if ((object[num].Moving == false) && (object[num].Animating == false)) so it allows the pecking to happen.