Page 2 of 4

Re: Lua local question

Posted: Tue Aug 16, 2011 8:03 pm
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

Re: Lua local question

Posted: Tue Aug 16, 2011 11:39 pm
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.

Re: Lua local question

Posted: Wed Aug 17, 2011 12:02 am
by slime
Dammit! You've won the battle, but not the war!

Re: Lua local question

Posted: Wed Aug 17, 2011 12:34 am
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:

Re: Lua local question

Posted: Wed Aug 17, 2011 2:32 am
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...

Re: Lua local question

Posted: Wed Aug 17, 2011 6:34 am
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).

Re: Lua local question

Posted: Wed Aug 17, 2011 6:44 am
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.

Re: Lua local question

Posted: Wed Aug 17, 2011 7:51 am
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.

Re: Lua local question

Posted: Wed Aug 17, 2011 2:10 pm
by TechnoCat
You guys aren't far from just talking about boolean algebra anymore.
http://en.wikipedia.org/wiki/De_Morgan's_laws

Re: Lua local question

Posted: Fri Aug 19, 2011 9:16 am
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"