0

So basically in a step command line I have this code

if BugType = 1 {
if instance_exists(Obj_Food_Small){
move_towards_point(Obj_Food_Small.x, Obj_Food_Small.y, 3)
}
} else {
    move_towards_point(Obj_Ant_Home.x, Obj_Ant_Home.y, 3);
}

And I want it to test if there is a food object and if there is move towards the food which turns the bug type into 2 and then the ant will go back to the home, except the ant when created just moves to the right, where there isn't any food. Thanks if anyone can help.

Will
  • 11
  • 2
  • 1
    the problem is surely somewhere else cause this lines of codes makes perfect sense, can you show me the rest of the code you have in your objects? – YOUSFI Mohamed Walid Feb 19 '19 at 09:09

2 Answers2

0

You have one too many brackets. I've removed the extract bracket and formatted and changed the code. Instead of checking if BugType is 1 or 2 I recommend just assigning it 1 or 0 when there is food and if there isn't.

FROM THE DOCS:

A boolean is simply a value that can either be true or false. Note that currently GameMaker: Studio does not support "true" boolean values, and actually accepts any real number below 0.5 as a false value, and any real number equal to (or greater than) 0.5 as being true.

So you can just check if BugType is true(0.5 or greater) or false(less than 0.5).

if (BugType){
    if instance_exists(Obj_Food_Small){
        BugType = 1;
        move_towards_point(Obj_Food_Small.x, Obj_Food_Small.y, 3)
    } else {
        BugType = 0;
        move_towards_point(Obj_Ant_Home.x, Obj_Ant_Home.y, 3);
}
olminkow
  • 121
  • 13
0

There's two things going on here! fixing the first should fix the second.

First is that you say it turns into bug type 2 before returning home, now if we look at the code we see that move_towards_point(Obj_Ant_Home.x, Obj_Ant_Home.y, 3); is actually within the scope of if BugType = 1 {.

So when the bug gets to the food, they become BugType 2, and then your code to return home never actually runs.

so 2nd thing:

The reason your ant keeps on moving is likely because move_towards_point() actually changes the object's built-in speed variable, which is probably never being set back to 0. SO, once BugType becomes 2, the return home code doesn't run, and the bug continues along at the speed they were last going.

all that to say you might want something like this:

if (instance_exists(Obj_Food_Small))
{
    BugType = 1;
    move_towards_point(Obj_Food_Small.x, Obj_Food_Small.y, 3)
}
else
{
    BugType = 2;
    move_towards_point(Obj_Ant_Home.x, Obj_Ant_Home.y, 3);
}

but the important thing is not to have the return home code in an area that is only executed when the bug is in "go get food" mode

may
  • 1