How do I get a for loop to wait?

Is there a function in codea that will stop a loop from continuing until a condition is satisfied to?

Just so you know, I am new to Codea.

Yes

for i=1, 100 do
  If a==25 then goto suite end
end
:: suite::

@hpsoft - better is

for i=1, 100 do
  If a==25 then break end
end

but I’m not sure this is what was asked

@jaj_TheDeveloper - can you be more specific?

I think the solution you are looking for is coroutines:

function setup()
    co = coroutine.create(dostuff)
    frame = 0
end
function draw()
    frame = frame + 1
    if frame%10 == 0 then
        coroutine.resume(co)
    end
end
function dostuff()
    for i=1,math.huge do
        print(i)
        if i%5==0 then
            coroutine.yield()
        end
    end
end

I am trying to create a for loop that pauses until touch.state == ENDED

I need it to only repeat it 15 times with the i = 1, 15 so a while loop will not be suitable.

@Monkeyman32123 what does coroutine.resume do, exactly?

Why does it need to be a for loop though? It’d be easier to have a variable that you increment yourself after the touch has ended. When I said we needed to see some code, I meant more than a little snippets! :slight_smile:

Do you want to know my golden rule of coroutines? If you think you need a coroutine, you probably don’t.

@yojimbo2000 +1

I think you might want a while loop

while condition == true do
    -- whatever loop should do --
end

@jaj nope, there is no such thing.
The closest answer is Monkeyman coroutine, but it is complex.
However, whatever you want to do, i bet you can do it WITHOUT this function

@jaj_TheDeveloper Would a while loop inside a for loop be what your wanting? Each time the for loop is executed it wouldn’t move on to the next for loop item until the while loop is satisfied.

Codea isn’t designed to wait, but to run continuously. If you stop a for loop until a condition is met, you’re also stopping the draw function from running and everything else too. If you want something to wait until a condition is met, then you write code to not execute whatever that is, but you allow Codea to run its normal code. That’s like saying a car has to stop every time you want to use the windshield wipers.

What if you had an “if” statement in the draw loop that would only run if the condition was/want met? Wouldn’t simply that work? Not sure I fully understand the question though.

Without knowing exactly what he wants to do and why, it will be just hit and miss trying to do anything.

@dave1707 true, but on the bright side weve given him five different solutions, so one of them is bound to be close

Yeah, we need details, some context, a bit of code.

thx.

As I said above, we need to know exactly what you’re trying to do. What kind of function will the for loop be in, draw(), a function called from draw(), or a function called when touch.state==BEGAN. What causes the for loop to start and after the for loop is paused and then resumes, does it continue to 15 and exit, or should it pause the for loop and then exit it when the touch.state ended. Do you need a for loop or just code that pauses when you touch the screen and resumes when you lift your finger. There’s a lot of things to consider when trying to write code and not knowing exactly what should happen makes it impossible. We can sit here and write hundreds of code snippets and keep asking questions, but if we don’t know what needs to be coded, it’s a waste of time.

If you care about being able to edit/read your code easily, goto would definitely be the way to go, but that is frowned upon by some people.