• gmod_src: Added continue keyword to Lua
    22 replies, posted
[B]Revision 649[/B] [INDENT]Added continue keyword to Lua[/INDENT] [B]Changed Files:[/B] [LIST] [*]trunk/src_garrysmod/public/Lua/llex.c [*]trunk/src_garrysmod/public/Lua/llex.h [*]trunk/src_garrysmod/public/Lua/lparser.c [/LIST] Committed By [B]Garry Newman[/B]
Nice, thanks!
:woop:
:neckbeard: Finally, gonna re-write my code for this, looks alot nicer :3:
[lua]for i=1, 10 if ( i < 5 ) then continue end print( i ) end[/lua] would print 5 onwards [editline]05:21PM[/editline] Also, real world example.. [lua]function gmsave.EntitySaveList( ents ) local tabEnts = {} local savedEnts = {} local SaveName = 1 for k, v in pairs( ents ) do if ( !IsValid( v ) ) then continue end local t = gmsave.EntitySave( v ) if ( !gmsave.ShouldSaveEntity( v, t ) ) then continue end v.GMSaveName = "ent" .. SaveName; tabEnts[ v.GMSaveName ] = t; savedEnts[ v.GMSaveName ] = v SaveName = SaveName + 1 end return tabEnts, savedEnts; end[/lua]
[QUOTE=iRzilla;25175792]Can anybody explain how this is used?[/QUOTE] It skips to the next iteration in the loop. [lua]for i=1, 5 do if i == 3 then continue end Msg(i, ", ") end[/lua] The output would be "1, 2, 4, 5, ".
continue is useful thanks garry.
Wow fuck yes, I was talking to a shitload of people about howto achieve this like a week ago, it's like you read my mind! :D
Awesome. No more if blocks in entire loops.
:love:
Yes :razz:!
Fuck yeah, this is amazing
Nice to see this addition. Still unclear to me, why lua hasn't this from default.
it should be named "skip" :D
Awesome!
Lua has never had native continue afaik.
-Snip-
[QUOTE=iRzilla;25271747]Garry rewrote Lua to work with CStyle syntax, maybe he removed it then?[/QUOTE] he didn't rewrote lua (else he would have done it in C++ rather than in C as lua is written in C) he only added the C-Style synatx chars which is pretty easy to add to the interpreter [cpp]case '|': { // C-Style OR next(ls); if (ls->current == '=') { next(ls); return '|='; } else if (ls->current == '|') { next(ls); return '||'; } else return '|'; }[/cpp] this would be the example of adding || to lua (it wouldn't work with just that as some may have noticed, but I can't remember where the rest of the code for it was) and as Kogitsune already said, continue is not standard in Lua, even in the new 5.2
[QUOTE=dingusnin;25261447]it should be named "skip" :D[/QUOTE] No, it skips the remaining stuff in the loop and [B]continues[/B] to the next iteration step of the loop. continue > skip.
Not totally amazing, we've been doing without it for years, but it's a feature none-the-less. Good job.
Sorry, you need to Log In to post a reply to this thread.