14 posts
Location
Melbourne, Australia
Posted 05 February 2016 - 01:00 PM
im still pretty new to lua so some things like booleans and numbers are new to me.
i have searched elsewhere and this is what i come up with:
- nil returns false
- booleans return it's value
- other values return true
but that does not explain this line of code:
local at = a[l] + ((w[ln] == nil) and d or 1)
w is a table of numbers,
d is a boolean,
a is another table of numbers,
at is a number
so, how does it work?
Edited on 05 February 2016 - 12:02 PM
2427 posts
Location
UK
Posted 05 February 2016 - 01:08 PM
local at = a[l] + ((w[ln] == nil) and d or 1)
is a short hand for several if statement checks (infact it's not just shorter for use reading it, but also quicker for Lua to 'read' too)
I'll try to expand it for you:
local at = nil
if (w[ln] == nil) and d then
at = a[l]
else
at = a[l] + 1
end
Edited on 05 February 2016 - 12:09 PM
724 posts
Location
Kinda lost
Posted 05 February 2016 - 01:10 PM
local at = a[l] + ((w[ln] == nil) and d or 1)
is a short hand for several if statement checks (infact it's not just shorter for use reading it, but also quicker for Lua to 'read' too)
I'll try to expand it for you:
local at = nil
if (w[ln] == nil) and d then
at = a[l]
else
at = a[l] + 1
end
Correction. It is:
local at = nil
if (w[ln] == nil) then
at = a[l] + d
else
at = a[l] + 1
end
You missed order of parentheses.
Edited on 05 February 2016 - 12:13 PM
14 posts
Location
Melbourne, Australia
Posted 05 February 2016 - 01:25 PM
thanks.
weird how lua excludes a++ but then includes stuff like this
edit:
so its luas version of a conditional statement? in C# it would be something like:
int at = a[l] + (w[ln] == null) ? d : 1
edit2:
dont worry, i found this:
http://www.lua.org/pil/3.3.html
Edited on 05 February 2016 - 01:04 PM
7083 posts
Location
Tasmania (AU)
Posted 07 February 2016 - 06:24 AM
so its luas version of a conditional statement? in C# it would be something like:
It tends to be used in Lua code to make up for the lack of a proper ternary, but it's also possible in other languages.