Move an object multiple times

I’m working on a nifty project in GS1 :slight_smile: (you’ll have a peak at it soon).
I wanted to know how to move an object (image) multiple times using player’s chat.
Like so…

if (playerenters) {
setimg block.png;
x=36;
y=6;
}
if (playerchats && strequals(#c,/moveleft)) {
x=37;
y=6;
}

This works great, but it doesn’t loop, so it only moves the block once.
How could I get it to move the block multiple times.

Thanks!

Use a for loop

Wouldn’t a for loop execute automatically?

   for(this.x=0;this.x<30;this.i++){
   ... 
   }

Not if you put it in the right spot

Move an object multiple times

if (playerchats && strequals(#c,/moveleft)) {
 for(this.x=0;this.x<30;this.x++){
 ...
 }
}

Now ask why you increment this.i. shouldn’t it be this.x?

Move an object multiple times

Yes, thank you Console. (No offense, but lol)…
this.x isn’t the same as x anyhow.
Unless… I set this.x=x;

What I can’t seem to understand is how a for loop will help me, as it auto increments by 1 without having to say /moveleft again.

x += value;
or
x -= value;

Move an object multiple times

I could add that after the conditional statement without the need of a for loop? Say x += 1 to drag the block to the right? Or would it only work once like previously?
(I would try it myself but I just left home, sorry for the questions…)

x += 1; //x incremented by 1
You know what’ll happen if that’s executed twice.

Move an object multiple times

…I need to get some sleep. I feel like a nub.

Thanks lol!

All the code you need is in this thread. You just need to put it together in the right order.

Your code needs some sleeps in it too. Almost forgot.

Move an object multiple times

—I’ll add the sleeps after the increment.

Thanks again! Should work fine.

You don’t need any sleeps if you intend for movement to occur one time each time a command is issued by the player.

Sleeps were also something people back in the day always said to avoid using. I’m not sure why or if there is a valid reason for it. It may prevent other “events” from being triggered on the script for the duration of the sleep or something. I don’t know. (This is part of why using the Graal client sucks.)

Move an object multiple times

Right because the increment is basically doing the work.

I didn’t even know sleep existed outside of Graal until recently. Nevertheless it comes in handy and there’s not much to it.

Gscript can’t multitask. So sleeps are fine. Also you need them to make movement look smooth.

There are many things it could impact. You don’t have the answer any more than I do.

All sleep does is pause for some amount of cpu cycles. Unless you are trying to keep track of time then it doesn’t hurt anything.

But now I’m thinking about shoot and trigger hacks. So yeah I really don’t know.

I think you want something like this.

if (created) {
  this.mode = 4;
  timeout = 0.05;
}

if (playerchats) {
  tokenize #c;
  if (strequals(#t(0),/move)) this.mode = strtofloat(#t(1));
  else if (strequals(#t(0),/stop)) this.mode = 4;
  timeout = 0.05;
}

if (timeout) {
  x += vecx(this.mode);
  y += vecy(this.mode);
  timeout = 0.05;
}

“/move 0” makes it move up. “/stop” makes it stop. Pretty basic.