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!
Problems with localising variables
- bobbymcbobface
- Citizen
- Posts: 78
- Joined: Tue Jun 04, 2019 8:31 pm
Problems with localising variables
I make games that run on potatoes :P
-
- Party member
- Posts: 563
- 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...
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.
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.
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
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
- zorg
- Party member
- Posts: 3470
- 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.
-
- Party member
- Posts: 234
- Joined: Mon Aug 29, 2016 8:51 am
Re: Problems with localising variables
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]
EDIT: I guess you could also use unpack()
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: Bing [Bot] and 6 guests