• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code Can't run game with a *Do* or *While* command?

M

MCKOY

Guest
I'm very new at this and trying to make a few simple things happen. I have two objects on screen and I'm trying to make one move towards the other but stop when it gets within a certain distance. So far I've been doing all the code inside Step. I can't get it to work correctly but when I do IF statements the game will at least run. If I try with a WHILE or DO statement, it won't run at all. Here's my examples.

Code:
if distance_to_point(obj_man, obj_orc) > 1000

{
move_towards_point(obj_orc.x, obj_orc.y, spd)
}

Code:
tdistance = distance_to_point(obj_man, obj_orc)
while (tdistance < 1000 && tdistance > 200)
{
move_towards_point(obj_orc.x, obj_orc.y, spd)
}

Code:
tdistance = distance_to_point(obj_man, obj_orc)
do (move_towards_point(obj_orc.x, obj_orc.y, spd))
until (tdistance < 200)
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
move_towards_point adjusts speed and direction, it doesn't instantly change coordinates. Furthermore you are not re-measuring your tdistance variable so even if it did, the loop condition would forever remain unsatisfied (because it's not polled again)
 
M

MCKOY

Guest
move_towards_point adjusts speed and direction, it doesn't instantly change coordinates. Furthermore you are not re-measuring your tdistance variable so even if it did, the loop condition would forever remain unsatisfied (because it's not polled again)
I thought if the code was entered with Step that it would be rerun every frame so the variable would be constantly updated. Is that incorrect?
 

JackTurbo

Member
I think you're misunderstanding how do and while work.

They don't iterate over multiple frames, they'll just keep running the code inside them over and over till the condition is true. Your game is basically getting stuck on one frame forever
 
M

MCKOY

Guest
I think you're misunderstanding how do and while work.

They don't iterate over multiple frames, they'll just keep running the code inside them over and over till the condition is true. Your game is basically getting stuck on one frame forever
Oh, I see. Is there something similar to it so I can make an action happen each frame until a condition is true? I'm still stuck on the same problem. Trying to make an object move to a certain distance away from another.

edit:
Actually I just managed it using the following code. Do you guys know of any tutorials relating to movement a little more in depth than the very basic ones? I really appreciate the help so far.

Code:
if distance_to_object(obj_man) > 1
{
    speed = 5
}

if distance_to_object(obj_man) < 200
{
    speed = 0
}
 
Last edited by a moderator:
Top