• 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!

Legacy GM Unable to stop move_towards_object

A

Aaron Fryer

Guest
Hi Folks,

First time posting here so apologies if I do something wrong.

Ive just picked up GM after a couple of years with Construct 2 and am getting stuck with some of the GML. I'm very keen to complete my current project only using code, and I've run into an issue, that everyone I have asked cannot figure out either,

Basically, I have a sprite for an Impala, which I would like to visit an Acacia tree when it gets hungry, then to wander off in a random direction for 50-150 pixels (im not actually too fussed on the distrance, as long as its away from the tree)

I have tried multiple iterations of the below code, and copied many attempts from other posts i've already found, but every time the Impala visits the tree, it wanders off and doesn't stop.


Code:
if (hunger <=2){
    newTarget = instance_nearest(x,y,obj_veg_Acacia);
    if (newTarget != noone){
        //image_angle = point_direction(x,y,newTarget.x,newTarget.y);  //--------- Left out as not sure if want sprite to rotate
        move_towards_point(newTarget.x,newTarget.y,3);
        if (distance_to_object(newTarget)< 2){
            with(newTarget){
                portions = portions - 1;
            }
            hunger = 5;
            restRange = 150;
            restPointX = random_range(x-restRange,x+restRange);
            restPointY = random_range(y-restRange,y+restRange);
            if point_distance(x,y,newTarget.x,newTarget.y) < restRange {
                move_towards_point(restPointX,restPointY,2);             
            }
            else speed = 0;
        }
    }
If anyone could suggest a way to achieve my goal it would be awesome, The variables are all only in use locally so I dont need to use any of them - they can be removed/changed.

Thanks in advance,

Aaron
 
Last edited by a moderator:

jazzzar

Member
Try to check if it reached the destination put this part like this :
Code:
If (x!=newTarget.x && y!=newTarget.y)
{
move_towards_point(newTarget.x,newTarget.y,3);
}
Please acknowledge that i didn't fully read your code as it's really hurting my eyes, maybe put them as a code using insert-->code
 
A

Aaron Fryer

Guest
Hi Jazzzar,

Thanks muchly for the quick response, apologies for the code, i'll edit the OP in a second.

The Impala does make it to the tree and realises it is there. I know this because he then changes direction straight away and moves off in a random direction (as planned) the issue is stopping, he just wanders out of the room at a steady pace.

Again, any help would be awesome!

Thanks,
Aaron
 
A

Aaron Fryer

Guest
Thats brilliant thanks The Shatner. I saw this yesterday in the wiki but obviously didnt check it.

[EDIT]
Unfortunately its still doing the exact same thing, which i find incredible as its an entirely different instruction its been given!

Have added the code as below, if it helps

Code:
        if (distance_to_object(newTarget)< 2){
            with(newTarget){
                portions = portions - 1;
            }
            hunger = 5;
            restRange = 150;
            restPointX = random_range(x-restRange,x+restRange);
            restPointY = random_range(y-restRange,y+restRange);
            mp_linear_step(restPointX, restPointY, 2, false);
           
        }
Amazed at the speed of the replies, thanks so much for your help thus far!!
 

jazzzar

Member
Ok when it reaches the destination,what it is supposed to do? And what is happening? So i can help you achieve it with variables
 
T

The Shatner

Guest
Yes, as jazzzar said, variables could do the trick. For example, you could add a "dothingy" variable. A simple, quick and dirty example:
Code:
//dothingy = 0 means that the object is going towards the tree
if (dothingy = 0) {
     move_towards_point(obj_tree.x,obj_tree.y,spd);
     if (place_meeting(x,y,obj_tree)) { //collision with the obj_tree
           speed = 0;
           dothingy = 1; //this would stop the move_towards_point set before and set the speed to 0
           }
}
if (dothingy = 1) {
//do whatever you want after the object hit the tree
}
All of this in the step event should do the trick. I am puzzled, however, as to why mp_linear_step didn't work out to you... Is the tree object flagged as solid?
 
A

Aaron Fryer

Guest
Hi Gents,

Sorry for the late reply was away last week so didnt manage to try these out.

Mr Shatner I tried the method you suggested, remarkably the same situation applies, the impala just will not stop.

He gets to the tree fine, recognises he is at the tree, then moves away to a random location as ordered, however he just never stops.

The code with your ammendments is below (for dothingy read hungerFull), this is driving me completely insane so if you do have anything else to suggest that would be epic!

Code:
if (hunger <=2){
    newTarget = instance_nearest(x,y,obj_veg_Acacia);
    if (newTarget != noone){
        hungerFull = 0;
        if (hungerFull = 0) {
            move_towards_point(newTarget.x,newTarget.y,3); //Move towards closest tree
            if (place_meeting(x,y,newTarget)) {
            with(newTarget){
               portions = portions - 1; //when it gets to the tree, take away an available portion from the tree
            }
            speed = 0;
            hunger = 5;
            hungerFull = 1;
            }
        }
    }
}
if (hungerFull = 1) { // Tells the impala it is full and to move away from the tree
    restRange = 150;
    restPointX = irandom_range(newTarget.x - restRange,newTarget.x + restRange); //finds a new spot to "rest" within 150 pixels
    restPointY = irandom_range(newTarget.y - restRange,newTarget.y + restRange); //finds a new spot to "rest" within 150 pixels
    if point_distance(x, y, restPointX, restPointY) > 5 {
        move_towards_point(restPointX, restPointY, 2); //moves towards rest point.
    }
    else speed = 0; //stops impala when it reaches the rest point <<<<<< This is the bit that wont work
    hungerFull = 2;
}
Thanks again,
Aaron
 
Last edited by a moderator:

NazGhuL

NazTaiL
You hsould use STATES for that.
Kind of:

Code:
//Create event
State = 'wait';//can be wait, search, eat and move
HP = 10;
Target_X = 0;
Target_Y = 0;
Hungry = 0;
Speed = 3;


//Step event

if(State == 'wait')
{
    if(Hungry)
    {
    State = 'search';
    }
}
else if(State == 'search')
{
var tmp_priority = ds_priority_create();
var tmp_distance;
var x1 = x;
var y1 = y;
var x2, y2;

    //add all acacias id using the distance as the priority
    with(obj_acacia)
    {
    x2 = x;
    y2 = y;
    tmp_distance = point_distance(x1, y1, x2, y2);
    ds_priority_add(tmp_priority, id, tmp_distance);
    }
 
    if(ds_priority_empty(tmp_priority))
    {
    State = 'wait';
    }
    else
    {
    var new_target = ds_priority_find_min(tmp_priority);
    Target_X = new_target.x;
    Target_Y = new_target.y;
    State = 'move';
    }
 
ds_priority_destroy(tmp_priority);
}
else if(State = 'move')
{
var x1 = x;
var y1 = y;
var x2 = Target_X;
var y2 = Target_Y;

    if(point_distance(x1, y1, x2, y2) < 32)//change the 32 for the distance needed you want him to stop
    {
    State = 'eat';
    }
    else
    {
    var tmp_direction = point_direction(x1, y1, Target_X, Target_Y); 
    x = x+lengthdir_x(Speed, tmp_direction);
    y = y+lengthdir_y(Speed, tmp_direction);
    }
}
else if(State = 'eat')
{
//eat

    if(stomach_is_full)
    {
    State = 'wait';
    }
}
 

csanyk

Member
I have tried multiple iterations of the below code, and copied many attempts from other posts i've already found, but every time the Impala visits the tree, it wanders off and doesn't stop.

If anyone could suggest a way to achieve my goal it would be awesome, The variables are all only in use locally so I dont need to use any of them - they can be removed/changed.
Trace the code in the debugger as it executes, and pretty quickly you should be able to see where you went wrong.

Another technique if stepping through the debugger is too tedious is to draw the variables you're using to the screen in debug mode, and see them change in real time.
 
A

Aaron Fryer

Guest
Thanks both for the speedy response. Absolutely delighted to confirm after 3 weeks of frustration the impala has actually stopped!!

I've combined alot of ideas to get the result, and I'm sure there's some irrelevant code in the below, which I will step through later in the project but for now in the interest of a resolution, the below works!!!


Code:
if (hungerFull = 1) {
    restRange = 150;
    restPointX = irandom_range(newTarget.x - restRange,newTarget.x + restRange);
    restPointY = irandom_range(newTarget.y - restRange,newTarget.y + restRange);
    if point_distance(x, y, restPointX, restPointY) > 5 {
        move_towards_point(restPointX, restPointY, 2);
    }
    else speed = 0;
    hungerFull = 2;
}

if (distance_to_object(newTarget) >= restRange && hungerFull = 2){
    speed = 0;
    hungerFull = 3;
}
Again thank you so much everyone for your assistance, to get this working has completely opened up the rest of what I want to do!!!

Aaron
 
Top