(Lua) Passing variable from one function to another?

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.
Post Reply
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

(Lua) Passing variable from one function to another?

Post by stout »

Let's say you have

Code: Select all

function foo()
     var = "bloop"
end

print(var)
nil
Why? Lua docs talk about how all variables are global unless specified otherwise, but that simple test would suggest otherwise.. unless I'm very confused about how it uses the word "global" (I suspect this is the case). How do I get one function to see another function's variables?
User avatar
Dattorz
Citizen
Posts: 66
Joined: Sat Oct 27, 2012 12:32 am
Contact:

Re: (Lua) Passing variable from one function to another?

Post by Dattorz »

var hasn't been assigned yet because you haven't called foo(). The function keyword only defines the function - it doesn't actually execute the code inside of it until you call the function. You need to put foo() above print(var).

Also, you probably shouldn't use global state unless you have good reason to. Better to have foo() return "bloop" than set the value of a global variable.
User avatar
qaisjp
Party member
Posts: 490
Joined: Tue Sep 04, 2012 10:49 am
Location: United Kingdom
Contact:

Re: (Lua) Passing variable from one function to another?

Post by qaisjp »

stout wrote:Let's say you have

Code: Select all

function foo()
     var = "bloop"
end

print(var)
nil
Why? Lua docs talk about how all variables are global unless specified otherwise, but that simple test would suggest otherwise.. unless I'm very confused about how it uses the word "global" (I suspect this is the case). How do I get one function to see another function's variables?
global means it exists in every file and every scope (unless that scope or file has a local defined, in which that local of the same name will be used in the scope)
it is nil because foo() has not been executed. try this:

Code: Select all

function foo()
     local new = "haha"
     print(new)
     var = "bloop"
end

print(var)
-->nil

foo()
--> "haha"

print(new)
-->"nil"

print(var)
-->"bloop"

Lua is not an acronym.
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

Re: (Lua) Passing variable from one function to another?

Post by stout »

Ahhhh. Well that explains some random issues I've had. Thanks. (I'm sure I've figured this out before.. maybe this time it'll stick)
User avatar
qaisjp
Party member
Posts: 490
Joined: Tue Sep 04, 2012 10:49 am
Location: United Kingdom
Contact:

Re: (Lua) Passing variable from one function to another?

Post by qaisjp »

stout wrote:Ahhhh. Well that explains some random issues I've had. Thanks. (I'm sure I've figured this out before.. maybe this time it'll stick)
heh, it happens. you're welcome.
Lua is not an acronym.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: (Lua) Passing variable from one function to another?

Post by Lafolie »

You could also do:

Code: Select all

function foo()
     return "text"
end

print(foo())
This doesn't store a (global) variable, but it allows you to 'pass' information.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: (Lua) Passing variable from one function to another?

Post by T-Bone »

I always found it strange that Lua uses global variables by default. I use "local" before like 90% of all variables. It would save plenty of characters to have locals by default and explicitly write "global" when you want global variables. Then again, only for local variables does it matter where you define it, so I guess it makes some sense.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: (Lua) Passing variable from one function to another?

Post by Robin »

T-Bone wrote:I always found it strange that Lua uses global variables by default. I use "local" before like 90% of all variables. It would save plenty of characters to have locals by default and explicitly write "global" when you want global variables. Then again, only for local variables does it matter where you define it, so I guess it makes some sense.
Yeah, exactly. Python does it the other way around and had to introduce the "nonlocal" keyword to be able to assign to "upvalues". It seems nice, local by default, but it gets messy if you try to make use of scoping.

JavaScript does it like Lua (with "var" instead of "local"), except that it's pretty much broken there. Déjà Vu uses Lua-style scoping as well.
Help us help you: attach a .love.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: (Lua) Passing variable from one function to another?

Post by kikito »

<troll>
And then there is ruby, which does it right.
</troll>
When I write def I mean function.
spir
Citizen
Posts: 76
Joined: Wed Oct 17, 2012 1:12 pm

Re: (Lua) Passing variable from one function to another?

Post by spir »

Actually, a piece of code where you'd better see what a global var means, and why it's bad ;-), looks like:

Code: Select all

var = 1

inc_var = function ()
   var = var + 1
end

print(var)
inc_var()
print(var)
Typically, if you have data outside functions, then they certainly mean something in your "world". Then, they probably belong to the state of some world element, thus are eg a field in a table (a table used as "object"). Even more if you manipulate it.

Denis
... la vita e estrany ...
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 39 guests