Page 1 of 2

using continue

Posted: Mon Apr 13, 2009 1:00 pm
by Sparx
Hi there,

I need to use a continue command. When I started löve i was new to lua. I read that you can add the continue functionality to lua by external libs or something....
How can I integrate it?

Thanks

By the way: Is there a possibility of breaking out of 2 loops? Like in PHP break(2); ?

Re: using continue

Posted: Mon Apr 13, 2009 8:43 pm
by Xcmd
You might want to reconsider your code flow and such. If there's absolutely no other way to do it (such as iterating a two dimensional array, as happens a lot in gaming), you might want to consider the break command. I know it works in for / do loops. I don't know if it works in while / do or repeat / until loops.

EDIT:

To break two loops, you need to check a conditional and set break-points in each loop. For instance:

Code: Select all

function breakLoop()
	local breakCondition
	for y = 1 to 50 do
		for x = 1 to 50 do
			if x == 49 then
				breakCondition = true
				break
			end
		end
		if breakCondition = true
			break
		end
	end
end
This is an arbitrary example, but basically in the nested loop, if a condition is met that needs to break the loop, then you set a local variable (scope is important, make the variable global if you need to know elsewhere in the program if a loop as been broken) to true and check it in the parent loop. Hope that makes sense.

Re: using continue

Posted: Tue Apr 14, 2009 1:24 am
by Star Crunch
If you don't need to do a break, you can simulate a continue as follows:

Code: Select all

for i = 1, NumberOfThingsToIterate do  -- OUTER LOOP
  repeat   -- "LOOP" to break out of in order to continue
    if WantsToContinue() then
      break -- "continue"
    end

    DoRegularStuff()
  until false
end

Re: using continue

Posted: Tue Apr 14, 2009 1:54 am
by osgeld
after some testing in core lua I retract my statement about while loops being faster than repeat until

Re: using continue

Posted: Tue Apr 14, 2009 2:52 am
by Star Crunch
There was a reason for the repeat-until loop, but I messed up the example. :P It should read repeat until true... this "loops" once, but you can still break out, thereby faking the continue. If you don't break, it proceeds normally.

If you need to mix breaks and continues, though, this isn't the way to go.

External libs:

Metalua has continue:

http://metalua.luaforge.net/src/index.html#lib

If you're willing / able to customize Lua (for a Löve app, probably not), there are patches for continue and break N here:

http://lua-users.org/wiki/LuaPowerPatches

Re: using continue

Posted: Tue Apr 14, 2009 6:18 am
by mike
[Moved to support]

Re: using continue

Posted: Tue Apr 14, 2009 10:50 am
by Sparx
How can I integrate the external lib in my LÖVE app?
I considered the other solutions myself but i think that shouldn't be the way!

Re: using continue

Posted: Tue Apr 14, 2009 11:37 am
by athanazio
@Sparx
I think I found how in the Lua documentation with the loadlib(), but it seems to be unable in LOVE,
take a look http://love2d.org/forum/viewtopic.php?f=3&t=665#p6288

Re: using continue

Posted: Tue Apr 14, 2009 11:58 am
by bartbes
@Sparx & anthanazio:
Check my post http://love2d.org/forum/viewtopic.php?f=3&t=665
More importantly if it's already a lib built for lua it's just following the docs for that lib. (most of them just tell you to use require)
You have to copy the distribution of that lib to your .love (may consist of .lua's and .dll's/.so's), require will search in the .love.

EDIT: Btw, I do want to mention I consider this somewhat bad practice.

Re: using continue

Posted: Sun Apr 19, 2009 1:23 am
by Sparx
I startet Lua 3 weeks ago. You can find your way arround continue and breaking out of more than 1 slope... BUT:
These things are sooo basic to me and I don't likke integrating an external lib just for a continue.... It should be somehow integrated into LÖVE....