• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

[SOLVED]Wander behavior script

C

Calyston00

Guest
Hi all!

I got a little problem, I got a wander behavior script that if I call it in the step event of my monster doesn't work.
If i do the same code (the whole script) in the step event of my monster it's work fine! The timer var doesnt go down in my script one know why ?

Here' is my script that i call in my step event of my monster :

Script Wander

// @Description Wander(time,force)
// @param time
// @param force
move = 0;
moveTime = argument0;
movementForce = argument1;


//FOR RANDOM MOVEMENT BEHAVIOR

show_debug_message("MoveTime :" + string(moveTime) + "move :" + string(move));
moveTime -=1; //this doesnt seem to work!!!!!
if(moveTime=0)
{
move = choose (0,1,2,3,4);
moveTime = argument0;
}

if(move = 0)
{

}

if(move = 1)
{
phy_position_y -= movementForce;
}
if(move = 2)
{
phy_position_x += movementForce;
}
if(move = 3)
{
phy_position_y += movementForce;
}
if(move = 4)
{
phy_position_x -= movementForce;
}

And here how i call it in the step event : (note you'll see that my original code that work really well in comment!!!!) :

/// @description Insert description here
// You can write your code in this editor
//CHECK HP
if(hp<=0)
{
instance_destroy();
}

Wander(120,2); //script calling!!!
/*
//FOR RANDOM MOVEMENT BEHAVIOR
moveTime -= 1;

if(moveTime=0) { move = choose (0,1,2,3,4); moveTime = 120;}

if(move = 0)
{

}

if(move = 1)
{
phy_position_y -= movementForce;
}
if(move = 2)
{
phy_position_x += movementForce;
}
if(move = 3)
{
phy_position_y += movementForce;
}
if(move = 4)
{
phy_position_x -= movementForce;
}
*/
 
You're setting moveTime to argument0 every single frame.
So if you call Wander(120,2), moveTime is always going to be 120 and it will never count down.

Same thing with your "move" variable.
Changing move to 0 every frame is keeping your monster from moving. You just want the move variable to stay how it was the last frame. No point of resetting it.

You have 2 un-needed variable assignments.
Simple fix :)

Also, let me recommend a switch statement for movement handling. And JSDoc comments (@param) need 3 backslashes to be recognized by GM.

NICE AND CLEANED UP VERSION:
Code:
/// @desc For random movement behavior
/// @func Wander(time,force)
/// @param time
/// @param force

var newMoveTime = argument0;
movementForce = argument1;

show_debug_message("MoveTime :" + string(moveTime) + "move :" + string(move));
moveTime -= 1;

if (moveTime = 0)
{
     move = choose (0, 1, 2, 3, 4);
     moveTime = newMoveTime;
}

switch (move) {
     case 0: break;
     case 1: phy_position_y -= movementForce; break;
     case 2: phy_position_x += movementForce; break;
     case 3: phy_position_y += movementForce; break;
     case 4: phy_position_x -= movementForce; break;
}
 
C

Calyston00

Guest
Wow thank you!!! it worked like a charm also you pointed my error so i could understand !! Ty really!
 
Top