Page 1 of 1
How to reference variables from one .lua file to another...
Posted: Tue Sep 03, 2013 6:59 am
by Ser_Mitch
Hey all,
I'm having issues cross-referencing files. I'm trying to take the variables hero.x and hero.y, which live in hero.lua, and reference them in creep.lua for basic AI, but I get the error message "creep.lua:27: attempt to perform arithmetic on local 'x' (a nil value). I've messed around, trying to fix it a few different ways, still have no idea...
Could someone who can see what I'm trying to do give me a suggestion?
Thanks
Re: How to reference variables from one .lua file to another
Posted: Tue Sep 03, 2013 7:26 am
by veethree
Replace hero.getPosition() with this. It was previously changing the returned position to whatever arguments you gave it, then returning it.
Code: Select all
function hero.getPosition()
return hero.x, hero.y
end
Also, I believe it's a little faster to reference the hero's position directly instead of using a function. (Not too sure, And if it is faster, it's not by a lot.)
Re: How to reference variables from one .lua file to another
Posted: Tue Sep 03, 2013 7:34 am
by micha
The problem is the function hero.getPosition. It is not really a getter, but a mix of a getter and a setter.
Simply replace it by
Code: Select all
function hero.getPosition()
return hero.x,hero.y
end
And the code runs.
When you call this function you don't need to pass any variable.
Then you can avoid using this function altogether, because, if you can access hero.getPostition, then you can also access hero.x.
To do so you have to rearrange a bit. Replace this
Code: Select all
creep = {}
function creep.load()
creep = {}
creep.x = 100
creep.y = 400
creep.w = 16
creep.h = 16
creep.xv = 0
creep.yv = 0
creep.speed = 140
creep.view = 200
end
By this:
Code: Select all
creep = {
x = 100,
y = 400,
w = 16,
h = 16,
xv = 0,
yv = 0,
speed = 140,
view = 200,}
You don't need a load-function then. The same goes for the player: replace the load function by a table constructor. If you want to "reset" the player some time later, then you can make a creep.reset function and put all the values in there.
Edit: Okay, veethree was faster.
Re: How to reference variables from one .lua file to another
Posted: Wed Sep 04, 2013 7:42 am
by Ser_Mitch
Wow, thanks guys, it's working now. All I did was change the hero.getPosition() function, I didn't mess around with tables and constructors and whatnot. Just out of curiosity, why would it be better for me to do that?
Re: How to reference variables from one .lua file to another
Posted: Wed Sep 04, 2013 10:22 am
by micha
Ser_Mitch wrote:Just out of curiosity, why would it be better for me to do that?
If you run this code
Code: Select all
creep = {}
function creep.load()
creep = {}
creep.x = 10
-- other stuff
end
-- stuff
return creep
Then there will be two tables, called creep. The first one is the one that got returned. It containts all the functions, like creep.load and so on. In the main file you store this via the require-command in a global variable called creep.
But when you run
then the local table in the creep.lua-closure gets replaced by an empty table (this does not delete the table that is stored globally).
So whenever you access the creep-table inside creep.lua then you get access to this table that contains these numbers, but not the functions anymore.
In a nutshell: This is a bit messy and probably not what you want, even though it might work. At least remove the line, that replaces the table with an empty one. That way you know that the two tables (the local one and the global one) point to the same table (= the same piece of memory) and thus referencing data is easy globally.
Re: How to reference variables from one .lua file to another
Posted: Wed Sep 04, 2013 8:58 pm
by Robin
micha wrote:But when you run
then the local table in the creep.lua-closure gets replaced by an empty table (this does not delete the table that is stored globally).
Nope. It's the other way around: the global one is in creep.lua, and the local one is in main.lua (which isn't replaced).
micha wrote:So whenever you access the creep-table inside creep.lua then you get access to this table that contains these numbers, but not the functions anymore.
That is correct. And if you access the variable creep in main.lua, you can see the functions, but not the numbers added in creep.load().
Re: How to reference variables from one .lua file to another
Posted: Thu Sep 05, 2013 5:52 am
by Ser_Mitch
Okaay. I've just been doing it like this:
Code: Select all
creep = {}
function creep.load()
creep = {}
creep.x = whatever
return creep
Because that appeals to my sense of OCD. Can someone show me/give me a pointer to a tutorial that explains the better way of doing things? According to said sense of OCD, I have to understand why something does anything before I implement it.
Thanks heaps, guys
Re: How to reference variables from one .lua file to another
Posted: Thu Sep 05, 2013 8:41 am
by micha
The most confusing this for me here, is that there are effectively two variables with the same name (both called creep).
Recall that the line
Does not delete the table that was stored there previously. Instead it creates a new table and makes creep point to this new (empty) table.
I learned a lot from
Programming in Lua. In theory it contains all you need to know, but it is really densely written, which makes it not necessarily fun to read.
Re: How to reference variables from one .lua file to another
Posted: Fri Sep 06, 2013 7:13 am
by Ser_Mitch
I know you can do something with tables and the word 'self'. Is that worth looking at? I know I'll have to eventually, but I wanted to get some sort of engine down first.
Re: How to reference variables from one .lua file to another
Posted: Fri Sep 06, 2013 7:39 am
by micha
If by "engine" you mean code that handles multiple objects in your game environment, then: Yes, have a look at "self".
It is explained in the OOP-chapter in
Programming in Lua. The first page is enough to read.