I'm having a bit of trouble with waypoints and NPCs - specifically this blasted fellow:
(http://i126.photobucket.com/albums/p108/dormant374/ThatDamnedCrab.png)
All I want him to do is scuttle around on some rocks. After searching through other people's threads and a few attempts to code this I can't seem to get him to go anywhere. It's probably something simple that I'm doing wrong - Here's my attempt:
function room_RepExec()
{
cCrab.Walk(113, 185, eNoBlock, eAnywhere);
cCrab.AddWaypoint(14, 186);
cCrab.AddWaypoint(15, 149);
cCrab.AddWaypoint(14, 186);
cCrab.AddWaypoint(113, 185);
}
When I put the blocking thingy as 'eBlock', he walked to the first point and stopped, but didn't go to his next destination.
Can anybody give me any pointers?
EDIT: I should mention - he tries to walk, i.e. his walking animation is playing, but he doesn't move off his comfy little spot.
Sorry - Wasted your time. I've fixxed it :o :o
The queue of the waypoints is constantly being reset, because you're running the code constantly.
function room_RepExec() {
if (Game.DoOnceOnly("firstwalk")) cCrab.Walk(113, 185, eNoBlock, eAnywhere);
if ((cCrab.x==113) && (cCrab.y=185)) {
cCrab.AddWaypoint(14, 186);
cCrab.AddWaypoint(15, 149);
cCrab.AddWaypoint(14, 186);
cCrab.AddWaypoint(113, 185);
}
}
First of all, you definitely want non-blocking movement, after all other things should happen while the crab moves in the background (I assume). You're problem is that you're calling your code in the rooms repeatedly execute function. This means you're basically spaming the poor thing with walk-to commands but never let it actually perform the move.
Instead try this:
function room_RepExec()
{
if(!cCrab.IsMoving())
// if the crab is not moving (ie. we just loaded the room or it reached a waypoint
cCrab.Walk(100+Random(100), 100+Random(100), eNoBlock, eAnywhere);
}
This checks if the crab is not moving (anymore) and, in that case, assigns it a new walk command. The way I used the random in there means it finds a random location between 100 and 200 on both the x- and y-axis, you should adapt this to the region you want it to move in.
Notice how the check for cCrab.IsMoving means we do not spam it with orders that it can't fulfill but rather wait for it to do the things and only then give it a new direction.
Hope this helps, code is absolutely untested and may contain typos. Feel free to ask if something remained unclear!
EDIT: Beaten but whatever, I have a different solution/explanation...
Thanks guys!
A nice approach, dkh definitely. I'm not sure how much my code below is also useful. But based on your idea and the waypoints.
function room_RepExec(){
if(!cCrab.IsMoving()) {
int x,y;
x=14+Random(99); //14-113
y=149+Random(37);//149-186
cCrab.Walk(x, y, eNoBlock, eAnywhere);
}
}