## Problems with localising variables

General discussion about LÖVE, Lua, game development, puns, and unicorns.
bobbymcbobface
Citizen
Posts: 78
Joined: Tue Jun 04, 2019 8:31 pm

### Problems with localising variables

hay all,

i need some help with a variable I'm trying to use from one file to another

i don't know exactly what I've done but i know my variable can't be called from the second file even know I'm using the return function
can someone please have look at my code and see what I've done wrong because I'm stumped on this one

~Thank you!
Snake.love
I make games that run on potatoes :P

MrFariator
Party member
Posts: 193
Joined: Wed Oct 05, 2016 11:53 am

### Re: Problems with localising variables

As far as I can tell, you are doing something along the lines of...

Code: Select all

-- bottom of Game.lua

-- in main.lua
local Game = require("Game") -- 'Menu.Snake' from Game.lua is not assigned to anything
And this is repeated across most of the files. If you have multiple return values, you need to assign each of those separately. Otherwise it will pick the first value returned, and ignore the rest.

Code: Select all

-- example
function sample()
return 0, 1, 2, 3
end

local a,b,c,d,e = sample()
print(a) -- 0
print(b) -- 1
print(e) -- nil, because only a,b,c,d received values

Reading your code, I don't know which variable you are having trouble with specifically, or where for that matter (I couldn't get the .love to crash when I ran it), so you'll have to elaborate if you need further help.

zorg
Party member
Posts: 3044
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: Problems with localising variables

One more complication, require can only return 1 result.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

KayleMaster
Party member
Posts: 223
Joined: Mon Aug 29, 2016 8:51 am

### Re: Problems with localising variables

zorg wrote:
Wed Jan 22, 2020 12:05 am
One more complication, require can only return 1 result.
That's why you return a table with your results:

Code: Select all

-- bottom of Game.lua

-- in main.lua
local Game = require("Game")[1]

Don't worry about require being called twice - its result is 'cached' after the first call.
EDIT: I guess you could also use unpack()

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: Problems with localising variables

If your module needs to return multiple values, consider that it might actually be two modules crammed into one, and you just need to split them.

bobbymcbobface
Citizen
Posts: 78
Joined: Tue Jun 04, 2019 8:31 pm

### Re: Problems with localising variables

tnx guys! i'll get back to you if any have any problems if not, again, thanks
I make games that run on potatoes :P

### Who is online

Users browsing this forum: No registered users and 29 guests