Hey all
I’m using a for i=1,50 type loop which works well. The only thing is when it’s finished I want to change a variable at that point.
Writing an else doesn’t work.
Writing a test of if i==50 doesn’t work as for some reason it cuts the i loop short
Any ideas?
Thank
@Majormorgan I’m a little confused about your question. If you want to change a variable after a “for” loop is finished, just put the variable assignment after the “for” loop. Am I missing something?
What do you want to use this for?
The code after the for loop is not executed until the for loop finishes, so is the following what you are looking for?
function setup()
done = false
end
function draw()
done = false
for i=0,50 do
end
done = true
end
@Majormorgan - That’s weird, it works perfectly fine for me. If you run this, you’ll see it prints 50:
function setup()
for i=1, 50 do
if i==50 then
print(i)
end
end
end
Thanks @dave1707 and @TheSoldierKing that makes sense now. So whilst it’s in the for loop it stays false and after its true. So the true after always overwrites the false previously. Question, shouldn’t done = false example in draw be inside the for loop so it stays false in the loop?
Thanks Saturn, but that bit was the bit that was always working anyway
are you trying to access the iterator (i) once the loop has finished? The iterator is local to the loop, it won’t get past the end statement. The for loop is not being cut short. But I also don’t know why you’d need to access it.
Hi @yojimbo2000 what id like to do is to use the end of the iterator to trigger the next iterator.
So the first one modifies the x position of an alien so it starts to accelerate away from its starting point. It finishes halfway across the screen by the time the loop finishes and it should then trigger the second iterator to decelerate the speed of the x position until it comes to a stop.
I’m combining this move with a downward motion in the Y position so it creates a sweeping arc movement.
Here’s the code I have currently:
if self.xpos==0 then
for i=1,200 do
self.xpos=0
--self.acc=self.acc
--self.xval=self.xval+self.acc
end
self.xpos=1
end
if self.xpos==1 then
for i=1,50 do
self.acc=self.acc*1.0006
self.xval=self.xval+self.acc
end
self.xpos=2
end
if self.xpos==2 then
for i=1,50 do
self.acc=self.acc*0.9994
self.xval=self.xval+self.acc
self.ypos=self.ypos*0.99
end
end
The idea is once xpos=0 is run then it triggers xpos=1 which triggers xpos=2 when it too is finished.
In its current form the for loop runs fully. It was cutting short when I added the line within the for loop:
If i == 50 then
self.xpos=1
End
For some reason it went straight into xpos 1 and not run the full 1,50 loop
Thanks!
Otherwise I’ll scrap the for loops and just run a timer variable that increments instead and then use the amount it gets to to trigger then next movement change
@Majormorgan The self.xval and self.yval values aren’t going to move your object in a smooth motion. When you set self.xpos to 0, the 3 for loops will execute immediately and you won’t have access to those values until the for loops are finished. So the values will be one thing when you start the for loops and they’ll be the final value when the for loops are finished. Your object will just jump from the start to the finished value.
Hi @dave1707
What I’m funding is because I have self.xval=self.xval+self.acc which is incrementing or decreasing slightly in each interaction it creates a smooth acelleration as I have in draw a listener that picks up the change in xval and translates it to the objects affected by it.
It’s more when the first for is executed it wouldn’t trigger for loop 2.
I think I’ll execute a self made variable timer
self.timer =self.timer +1
If self.timer>=50 then
if self.xpos==0 then self.xpos=1
elseif self.xpos==1 then self.xpos=2
self.timer=0
end
And that should do the trick.
Thanks!