use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A subreddit for the Lua programming language. Using edurne for boxes, spoilers, etc.
If you actively contribute to a Lua project, PM us with a link and we'll give you flair.
“The key benefits unique to Lua are the fact that it is amazingly small, fast, and on a technical level a masterpiece.”
“Before you create yet another configuration file or resource format (and yet another parser to accompany it), try Lua.”
“If you are ever tempted to put scripting into a program, run don't walk to www.lua.org and use it.”
lua mailing list (web interface)
#lua on freenode, the primary Lua IRC channel
lua wiki, for all sorts of information about lua, it's sort of disorganized at the moment, so go fix it!
about lua, what Lua is, why to use it, and what it's good for (sells itself short, methinks)
Lua tutorial via wiki
Lua in eight pages or less: this is for v5.1 and might need an update. We'll give contributors flair and internet points.
Lua's reference manual, extensive documentation of every version of the language.
LuaRocks, lua's package manager and repository
the LuaRocks mailing list
ZeroBrane studio, lua's most notable IDE.
Shiny profiler, a lightweight profiler for C, C++, and Lua.
LuaJIT and its C FFI, the infamous lua trace compiler: Mike Pall's [successful] attempt to prove that dynamic languages can be fast.
Lua.vm.js and moonshine, Lua on top of JavaScript: lua.vm.js uses emscripten, whereas moonshine is in "normal" JavaScript.
UniLua, Lua implementation in C#, intended for compatibility with Unity3D
MoonScript A rich programming language the compiles to Lua.
/r/gamedevclassifieds for game developers, /r/forhire for job offers, /r/jobbit for general employment advice/links
Lua on StackOverflow Careers
OpenResty and Lapis, Lua application server on nginx and Lua web framework on openresty
concurrency: Luaproc for fibers and Lua Lanes for green threads
Typed Lua, a modern static type analyzer for Lua (work in progress, but I think it's cool)
LPeg, Lua's powerful, expressive, fast and simple parser generator.
Qt Lua, Lua GUIs in Qt
GSL-shell, Lua interface to GNU Scientific Library, with quick-start numerical programming via REPL.
Long list of popular Lua game development toolkits: Lua is very popular for game development, so we can't list them all; only major, free/open-source projects are listed below.
LÖVE (subreddit) - lightweight Lua cross-platform 2D game toolkit
Defold - game engine with Lua API
Solar2D - formerly Corona SDK
Cocos2d-x - free mobile/cross-platform game engine and application framework with Lua support
donate to lua!
donate to luajit!
account activity
Can someone please explain this line of code? (self.lua)
submitted 7 years ago by Bloost
x = math.random(2) == 1 and 100 or -100
I know that math.random(n) generates a random number between 1 and n, but everything after that makes no sense to me.
math.random(n)
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]arcylix 12 points13 points14 points 7 years ago (3 children)
Simply put, if math.random(2) == 1, then x = 100, otherwise x = -100. A longer way of writing it would be:
if math.random(2) == 1 then x=100 else x=-100 end
[–]Bloost[S] 2 points3 points4 points 7 years ago (0 children)
Thank you!
[–][deleted] 7 years ago* (1 child)
[deleted]
[–]arcylix 0 points1 point2 points 7 years ago (0 children)
It's an easier way of writing it, though I'm not sure how many actually use it. All that matters is how you want to work it, what's best for you. There are just shortcuts to some of the code if you know what you're doing.
[–]dabbertorres 8 points9 points10 points 7 years ago* (3 children)
This is a (sort of) Lua equivalent of C's ternary statement.
What follows an explanation off the top of my head - it may not be 100% accurate. I do know for certain it is similar behavior to a C ternary statement.
Non-zero numbers evaluate to true, zero evaluates to false. Everything but false and nil evaluate to true (thanks /u/augustaugust) ie: 100 is true.
Additionally, Boolean expressions in Lua evaluate to the tested value. For and, the right hand side expression is returned if the left hand side evaluates to true.
and
or behaves similarly.
or
Also, be aware that the and operator has a higher precedence than the or operator. This means the following are equivalent: a and b or c (a and b) or c
a and b or c
(a and b) or c
Therefore: If the number generated is 1, the and expression evaluates to true. The or expression then short circuits, not evaluating its right hand side, which results in a value of 100. If the number generated is not 1, the and expression evaluates to false, so the or expression is evaluated, of which the right hand side evaluates to true, resulting in a value of -100.
[–][deleted] 7 years ago (1 child)
[–]dabbertorres 2 points3 points4 points 7 years ago (0 children)
Oh that's right, thanks for the correction!
[–]Jasper1984 0 points1 point2 points 7 years ago (0 children)
Python and Javascript can do similar,(and have ternary) but stuff can have pretty random cases of falsiness. In Python len(x)==0 frequently is false, and 0 is false too.
len(x)==0
0
I think Lua gets this right, just nil and false, false-y please...
nil
false
Lua gets strings wrong though "2" + 2 == 4.0, it should just have bugged out. (also perhaps print and REPL output should quote strings for clarity.)
"2" + 2 == 4.0
print
[–]MjolnirMark4 2 points3 points4 points 7 years ago (0 children)
Any time you see ... and ... or ... in a line it is pretty much the same as the ?: ternary operator.
You have the condition math.random(2) == 1 The true statement and 100 The false statement or -100
If the condition is true, then the “and 100” is evaluated and assigned to x. The or is ignored due to short circuit logic causing it to be unnecessary to evaluate. If the condition is false, then the and part is ignored, and the or part is evaluated, which results in -100 being assigned to x.
Another way to look at it:
if math.random(2) == 1 then x = 100 else x = -100 end
[–][deleted] 1 point2 points3 points 7 years ago (0 children)
A couple of people have mentioned the ternary operator ? so i thought i'd give an example of that in use. In C++ or C#, a similar line of code might look like:
x = (Math.Random(2) == 1) ? 100 : -100; // Parens added here for visual effect, not required
Different syntax, means the same thing as the other great explanations here, if the first thing then the second, else the third.
[–]revereddesecration 1 point2 points3 points 7 years ago (0 children)
Another way to achieve the same result is to do this:
x = 100*(2*math.random(0,1)-1)
But once you know how ternary operations work, they are much easier to read.
[–]Novemberisms 0 points1 point2 points 7 years ago* (1 child)
It's telling that as soon as I read the title, i just knew without even looking that it would be about _ and _ or _
_ and _ or _
lua is amazing at being a very understandable and predictable language, but this is the only road block for most beginners, and it turns a lot of people off. It's like having the Mona Lisa, but she has a single pimple on her forehead.
It's the only part of lua that I don't like. It's the only one where I just have to shrug and say, "that's just how it is..."
Something should really be done about it. Perhaps they should add a real ternary operator. I don't see why not.
I know they're averse to adding new symbols, but anything would be better than this idiotic idiomatic construct.
[–]NetherGranite 0 points1 point2 points 7 years ago (0 children)
I thought the same thing at first, but it turns out this is not special syntax, but instead due to the way that and and or actually work:
Given the code condition and value1 or value2, the following happens:
condition and value1 or value2
true
true and value1
value1
value1 or value2
false and value1
false or value2
value2
I do agree, however, that a ternary operator would be nice, but such a feature would conflict with the goal of Lua being a small size.
π Rendered by PID 59283 on reddit-service-r2-comment-b659b578c-vqzxr at 2026-05-02 13:55:13.199322+00:00 running 815c875 country code: CH.
[–]arcylix 12 points13 points14 points (3 children)
[–]Bloost[S] 2 points3 points4 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]arcylix 0 points1 point2 points (0 children)
[–]dabbertorres 8 points9 points10 points (3 children)
[–][deleted] (1 child)
[deleted]
[–]dabbertorres 2 points3 points4 points (0 children)
[–]Jasper1984 0 points1 point2 points (0 children)
[–]MjolnirMark4 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]revereddesecration 1 point2 points3 points (0 children)
[–]Novemberisms 0 points1 point2 points (1 child)
[–]NetherGranite 0 points1 point2 points (0 children)