Problems with localising variables

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

Problems with localising variables

Post by bobbymcbobface » Tue Jan 21, 2020 8:48 pm

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
(4.59 MiB) Downloaded 111 times
I make games that run on potatoes :P

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

Re: Problems with localising variables

Post by MrFariator » Tue Jan 21, 2020 9:25 pm

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

Code: Select all

-- bottom of Game.lua
return Game, Menu.Snake

-- 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.

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

Re: Problems with localising variables

Post by zorg » Wed Jan 22, 2020 12:05 am

One more complication, require can only return 1 result.
Me and my stuff :3True 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

Post by KayleMaster » Wed Jan 22, 2020 4:20 pm

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
return {Game, Menu.Snake}

-- in main.lua
local Game = require("Game")[1]
local Menu.Snake= require("Game")[2]
Don't worry about require being called twice - its result is 'cached' after the first call.
EDIT: I guess you could also use unpack()

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

Re: Problems with localising variables

Post by raidho36 » Wed Jan 22, 2020 4:39 pm

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.

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

Re: Problems with localising variables

Post by bobbymcbobface » Thu Jan 23, 2020 7:56 pm

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

Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests