Search code examples
loopsluapause

How can i continue the loop after getting value from function?


as i write in title, I have small question it's about loop! i've small loop, ok? i want pause loop in each value and call a function and wait a response from function, if function give any value i want to continue the loop! anyone have idea or suggest to help me at this? please don't give codes includes/or needs LUA Librares


Solution

  • Function calls inside loops will block by default in Lua (and any other language I can think of). So you do not have to worry about that. The loop won't continue as long the function doesn't return a value.

    function is_done(x)
      if x == 5 then
        return true
      end
      return false
    end
    
    for i=1,10 do
      if is_done(i) then
        print('done!')
        break
      end
    end
    

    In the above example the loop breaks (stops) when i is equal to 5.