Lua local question

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
slime
Solid Snayke
Posts: 3156
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Lua local question

Post by slime »

benloran wrote:
slime wrote:

Code: Select all

function my_func(someArg)
	someArg = someArg == nil and someDefaultValue or someArg
end
Unless someDefaultValue is false, in which case the OP's solution is the only way to do it.

Code: Select all

function my_func(someArg)
	someArg = someArg ~= nil and someArg or someDefaultValue
end
:P
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Lua local question

Post by Robin »

slime wrote:

Code: Select all

function my_func(someArg)
	someArg = someArg ~= nil and someArg or someDefaultValue
end
:P
If someArg is false:

Code: Select all

someArg = false ~= nil and false or someDefaultValue
so it is exactly the same as

Code: Select all

someArg = someArg or someDefaultValue
So to deal with the argument like this, being false and nil in different ways, you need an if-statement.
Help us help you: attach a .love.
User avatar
slime
Solid Snayke
Posts: 3156
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Lua local question

Post by slime »

Dammit! You've won the battle, but not the war!
User avatar
benloran
Prole
Posts: 19
Joined: Tue Jul 05, 2011 4:52 pm

Re: Lua local question

Post by benloran »

Heh, Robin beat me to it. Anyway, the point is that and/or assignments can lead to hard-to-track bugs if you're using them with boolean values. :monocle:
pancakepalace
Prole
Posts: 40
Joined: Wed Aug 03, 2011 3:13 pm

Re: Lua local question

Post by pancakepalace »

I tested this profusely. The if/then structure seems like the only one which works in this case and it's easy to read. My default value can be false, or some string.

If only Lua supported ternary conditionals. If only...
User avatar
benloran
Prole
Posts: 19
Joined: Tue Jul 05, 2011 4:52 pm

Re: Lua local question

Post by benloran »

Well it does. You just can't use them if you need nil and false to be separate values.

In Lua,

Code: Select all

result = C and A or B
is the same as

Code: Select all

result = C ? A : B
in other languages, except for the single situation in which you want to assign a value of false to the result if C is true (in which case you have to test for nil instead).
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Lua local question

Post by Robin »

If A is nil and B is true, you could do:

Code: Select all

result = not C and B or A
If A is false, you can also do:

Code: Select all

result = not C and B
But if both A and B are or might be false, none of that works.
Help us help you: attach a .love.
User avatar
Hydra
Prole
Posts: 5
Joined: Wed Aug 17, 2011 1:59 am
Location: Canada
Contact:

Re: Lua local question

Post by Hydra »

pancakepalace wrote:Hi guys,

I read all the docs I could find on the Lua language, but the use of local keyword when creating default values for function parameters is still unclear to my mind.
This is actually a pretty important subject with lua, i'll do my best to explain this for you.

Lua script uses two environments, local and global.

Variables, functions etc. in the local environment can only be accessed within the file in which they are created. For example:

Code: Select all

local msg = "Hello World!"
This message cannot be called outside of its file with "msg", whereas this:

Code: Select all

msg = "Hello World!"
Is global, and CAN be accessed anywhere within your program.

As well local variables that are created within functions cannot be used outside of that function, another example:

Code: Select all

local doSomething = function()
	local x = 1
end

print(x)
this would return nil, because "x" is local within that function, even if you were to call doSomething() before trying to print "x", it's not going to return any value.

If a variable/function/table doesn't have the word "local" in front of it, it's automatically going to be in the global environment.

It's worth noting that lua script runs faster in the local environment (the code will be executed faster and with less processing time), so using local variables and functions wherever possible is a good way to improve your code.

I'm pretty tired right now so hopefully that clears things up and I didn't just make an ass of myself.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: Lua local question

Post by TechnoCat »

You guys aren't far from just talking about boolean algebra anymore.
http://en.wikipedia.org/wiki/De_Morgan's_laws
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Lua local question

Post by T-Bone »

One thing I've always wondered. If you create a local variable inside a .lua-file, that variable will only be available there. If you create a local variable inside a function, it will only be available inside the function. Is there a way to create a "local" variable (local as in local for that .lua-file) from withing a function? :neko:

Like this

Code: Select all

a="global"
local a = "local level 1"

function lol()
   local b = "local level 2"
   semilocal c = "local level 1"
end

print(b) --nil
print(c) --"local level 1"
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest