Generic Lua question: Inline breaks/returns

What, in your opinion, is the best way to accomplish inline breaks/returns (i.e. to block only a specific piece of code, and not the rest of the block, essentially a nested elseif)


local foobar = false
if foo == bar then
    - code here -
    foobar = true
end

if not foobar then
    - backup code -
end


repeat
    - code here, with breaks in it -
    - backup code -
until true

I used to use the first method before I came up with the second.
I’m sure both of those are infinitely better as opposed to:


(function()
    - code here, with returns in it -
    - backup code -
end)()

I guess you could do
[lua]do
–code with break
–backup code
end
–rest of code[/lua]

It depends; What are you trying to accomplish?

This used to work but I think it doesn’t work anymore. Last time I tried that, it whined about break not being inside a loop.

You can do it the outrageous way if you feel like being an asshole.

[lua]if condition then
goto nope
end

print(“doing something”)

::nope::

print(“done”)[/lua]

It works, give it a try. :v:

This is correct. I was kinda disappointed it didn’t work.

…Only if using Lua 5.2
Hey, does GM use 5.1 or 5.2?

Ah, yeah it will complain if there is an un-nested break/continue/return.

On files I don’t want to execute I’d do if ( true ) then return; end

If it’s conditional, you shouldn’t have any issue.


do return end

Shorter and cleaner.

[editline]24th March 2014[/editline]

Something I’ve never seen is a multi-else[if].
Sort of like finally I guess except it doesn’t always run.
Example:



local stuff = getStuff()
if stuff then
    local stuff2 = stuff:doSomething()
    if stuff2 then
        return stuff2:doSomethingElse()
melse(2)
    print("either didn't have stuff or didn't have stuff2")
end

where melse is the number of blocks to go up (i.e. melse(1) == else)
I can see it not working out well but fuck would it be useful.

[editline]24th March 2014[/editline]

Just in case it’s not clear of my intentions, here’s a live snippet of code:


repeat
	if gmp.DynaviewFreelookEnabled:GetBool() then
		if gmp.DynaviewFreelookActive and not gmp.DynaviewFreelookAng then
			gmp.DynaviewFreelookAng = gmp.DynaviewData.angles
		end
		if gmp.DynaviewFreelookEnabled:GetBool() and gmp.DynaviewFreelookActive then
			gmp.DynaviewData.angles = gmp.DynaviewFreelookAng + AngAddCurrent
			break
		end
	end
	gmp.DynaviewData.angles = AngCurrent + AngAddCurrent
until true

[editline]asd[/editline]

Nevermind, bad example, I need to go to bed:


if gmp.DynaviewFreelookEnabled:GetBool() and gmp.DynaviewFreelookActive then
	if not gmp.DynaviewFreelookAng then
		gmp.DynaviewFreelookAng = gmp.DynaviewData.angles
	end
	gmp.DynaviewData.angles = gmp.DynaviewFreelookAng + AngAddCurrent
else
	gmp.DynaviewData.angles = AngCurrent + AngAddCurrent
end

[editline]24th March 2014[/editline]


lua_run ::nope:: goto nope

Well I guess GM uses 5.2…

[editline]24th March 2014[/editline]

THIS IS THE [EDITLINE] OF LEGENDS

[editline]24th March 2014[/editline]

I AM GOING TO SLEEP FUCK

I’m not following you completly but I know garry has implemented ‘continue’ into glua. Would that be of any use?


> do continue end...

[ERROR] lua_run:1: no loop to continue near 'end'
  1. unknown - lua_run:0

continue is used in loops to avoid having to wrap the entire insides of a for loop with an if statement.

What exactly are you trying to do? Are you just asking about how we prefer to write it ourselves in terms of coding style?

LuaJIT has labels and gotos, despite sticking to mostly Lua 5.1 standards.

Interesting. Didn’t know that.