REALLY basic thing i cant figure out

A

Andres Molina

Guest
Hey, im trying to learn GML so i started creating a really basic game. My objective now is to create a moving enemy, make it move 2 seconds to the left, idle 1 sec and 2 seconds to the right, then repeat. This is the thing, ive write this code and what happens is that the object starts teleporting to the left / right every step. I still cant figure out whats wrong. Tell me please what is wrong with it, dont make any code for me, i want to figure out how to do this (i know there should be like 1000+ ways to do it, but maybe im too stupid who knows)
This is my code:

//Movement
if (idle == 1)

{
for (i=0;i<=idle_ms;i+=1) //idle_ms == time in ms to idle (since my room is capped to 60steps/s this equals to 60)
{
if (i==idle_ms) //This executes once, at the end of the for loop
{
idle = 0;
orientation *=-1; //where 1 is right, and -1 is left.
}
}
}

if (idle == 0)
{
for (i=0;i<=move_ms;i+=1)
{
if (orientation == 1)
{
x+=movespd; // movespd = 1. 1 pixel/s.
}
if (orientation == -1)
{
x-=movespd;
}
if (i==move_ms)
{
idle = 1;
}
}
}

the enemy_o create code:

movespd = 1;
move_ms = 0;
idle_ms = 0;
idle = 0;
orientation = 1;
move_s = 2;
idle_s = 1;

move_ms = move_s*60;
idle_ms = idle_s*60;

Thanks!
 

Slyddar

Member
The problem is you are running a for loop and changing the x value inside of it. The whole loop is run inside the event, so Gamemaker never gets a chance to move the object. The loop ends and GM updates it's x position to be the last value in the loop. Then it runs again and does the same thing the other way. For loops are not good for what you are trying to do.

You can use [.CODE.] and [/.CODE.] without the fullstops to paste code in your posts.

Code:
//Movement
if (idle == 1) {
    idle_ms--;
    if idle_ms <= 0 {
        idle = 0;
        idle_ms = idle_s*60;
        orientation *= -1;
    }
}
if (idle == 0) {
    x+=movespd*orientation;
    move_ms--;
    if move_ms <= 0 {
        idle = 1;
        move_ms = move_s*60;
    }
}
 
Last edited:
A

Andres Molina

Guest
The problem is you are running a for loop and changing the x value inside of it. The whole loop is run inside the event, so Gamemaker never gets a chance to move the object. The loop ends and GM updates it's x position to be the last value in the loop. Then it runs again and does the same thing the other way. For loops are not good for what you are trying to do.

You can use [.CODE.] and [/.CODE.] without the fullstops to paste code in your posts.

Code:
//Movement
if (idle == 1) {
    idle_ms--;
    if idle_ms <= 0 {
        idle = 0;
        idle_ms = idle_s*60;
        orientation *= -1;
    }
}
if (idle == 0) {
    x+=movespd*orientation;
    move_ms--;
    if move_ms <= 0 {
        idle = 1;
        move_ms = move_s*60;
    }
}
Thanks a lot. So, i if i do the same process i tried but using "if" only and not a for loop, would it work?
I think it would result in a long code for just an AI automatic walk.

Thanks again for all.
 

Slyddar

Member
You could also do something like this. In the end all it's mostly doing per step is decreasing a counter and running two checks, so other then adding an else between them, like I've done in this example, it's not too much code for the result you require.

Code:
//CREATE
movespd = 2;              //speed of enemy
speed = movespd;
idle = 0;                 //is the enemy idle?
idle_s = 1;               //how long to idle in seconds
move_s = 1;               //how long to walk for in seconds
idle_ms = idle_s*60;      //convert to milliseconds
move_ms = move_s*60;


//STEP
//Movement
if (idle) {
    idle_ms--;
    if idle_ms <= 0 {
        idle = 0;
        idle_ms = idle_s*60;
        direction += 180;
        speed = movespd;
    }
} else
if (!idle) {
    move_ms--;
    if move_ms <= 0 {
        idle = 1;
        move_ms = move_s*60;
        speed = 0;
    }
}
 
Last edited:
Top