• Generic Lua question: Inline breaks/returns
    11 replies, posted
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) [code]local foobar = false if foo == bar then - code here - foobar = true end if not foobar then - backup code - end[/code] [code]repeat - code here, with breaks in it - - backup code - until true[/code] 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: [code](function() - code here, with returns in it - - backup code - end)()[/code]
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?
[QUOTE=ralle105;44337767]I guess you could do [lua]do --code with break --backup code end --rest of code[/lua][/QUOTE] 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:
[QUOTE=_Kilburn;44337842]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.[/quote] This is correct. I was kinda disappointed it didn't work. [QUOTE=_Kilburn;44337842]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:[/QUOTE] ...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.
[code]do return end[/code] 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: [code] 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[/code] where melse <depth> 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: [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[/code] [editline]asd[/editline] Nevermind, bad example, I need to go to bed: [code]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[/code] [editline]24th March 2014[/editline] [code]lua_run ::nope:: goto nope[/code] 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?
[QUOTE=mcd1992;44342903]I'm not following you completly but I know garry has implemented 'continue' into glua. Would that be of any use?[/QUOTE] [code]> do continue end... [ERROR] lua_run:1: no loop to continue near 'end' 1. unknown - lua_run:0[/code]
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?
[QUOTE=DarkShadow6;44337897]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?[/QUOTE] LuaJIT has labels and gotos, despite sticking to mostly Lua 5.1 standards.
[QUOTE=Willox;44344386]LuaJIT has labels and gotos, despite sticking to mostly Lua 5.1 standards.[/QUOTE] Interesting. Didn't know that.
Sorry, you need to Log In to post a reply to this thread.