blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 08 Jul 2014 03:44 PM |
I know I shouldn't be posting help topics here, but I am stumped and SH is as well. Anyway, how can I restart a loop when it's in the middle of a cycle? For example, if I have this script:
while true do wait() if (x == true) then --Operation A wait(2) if (x == false) then --return to beginning of loop else --Operation B end end end
I marked where I'd like it to return to the beginning of the loop, so that Operation B does not occur if x changes from true to false after the first "if" statement has been passed. Any ideas? |
|
|
| Report Abuse |
|
|
|
| 08 Jul 2014 03:56 PM |
Recursion is your friend. i.e.
function looper() while wait() do if (x == true) then --Operation A wait(2) if (x == false) then looper() break else --Operation B end end end end |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 08 Jul 2014 04:00 PM |
Wow, I completely forgot about that. I've been gone too long ;_; Anyway, thanks |
|
|
| Report Abuse |
|
|
|
| 08 Jul 2014 04:00 PM |
Theres not even any need for recursion
function loop() while true do wait() if (x == true) then --Operation A wait(2) if (x == false) then break end else --Operation B end end return true end
while true do local success = loop() end |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 09 Jul 2014 05:32 AM |
What you need is a continue statement, however Lua doesn't have that. A workaround I've seen is to use repeat until true.
while (true) do repeat ... if (shouldContinue) then break; end ... until (true); end |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2014 06:09 AM |
ugh, Lua sucks, why don't they have some of these things like ++/+=/*=//= and why don't they have continue and I'm probably missing a lot
|
|
|
| Report Abuse |
|
|
|
| 09 Jul 2014 06:12 AM |
"++/+=/*=//="
I heard that they are "too complex" and "ruin the simplicity"... |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2014 06:14 AM |
| Looking into it, metatables ruin everything. |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2014 06:23 AM |
| Whatever, it's super annoying and even C has them |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2014 06:25 AM |
| It is annoying, and the reasoning behind it implies "I was too lazy to do it"... |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2014 06:43 AM |
From stackoverflow
The way that the language manages lexical scope creates issues with including both goto and continue. For example,
local a=0 repeat if f() then a=1 --change outer a end local a=f() -- inner a until a==0 -- test inner a
The declaration of local a inside the loop body masks the outer variable named a, and the scope of that local extends across the condition of the until statement so the condition is testing the innermost a.
If continue existed, it would have to be restricted semantically to be only valid after all of the variables used in the condition have come into scope. This is a difficult condition to document to the user and enforce in the compiler. Various proposals around this issue have been discussed, including the simple answer of disallowing continue with the repeat ... until style of loop. So far, none have had a sufficiently compelling use case to get them included in the language. |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2014 10:08 AM |
""++/+=/*=//="
I heard that they are "too complex" and "ruin the simplicity"...
It is annoying, and the reasoning behind it implies "I was too lazy to do it"..."
If there was a like button, I would hit the fk out of it. |
|
|
| Report Abuse |
|
|