How to reference variables from one .lua file to another...
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
How to reference variables from one .lua file to another...
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
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
- Attachments
-
- Halp.love
- (7.9 KiB) Downloaded 848 times
Re: How to reference variables from one .lua file to another
Replace hero.getPosition() with this. It was previously changing the returned position to whatever arguments you gave it, then returning it.
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.)
Code: Select all
function hero.getPosition()
return hero.x, hero.y
end
Re: How to reference variables from one .lua file to another
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
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
By this:
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.
Simply replace it by
Code: Select all
function hero.getPosition()
return hero.x,hero.y
end
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
Code: Select all
creep = {
x = 100,
y = 400,
w = 16,
h = 16,
xv = 0,
yv = 0,
speed = 140,
view = 200,}
Edit: Okay, veethree was faster.
Check out my blog on gamedev
Re: How to reference variables from one .lua file to another
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
If you run this codeSer_Mitch wrote:Just out of curiosity, why would it be better for me to do that?
Code: Select all
creep = {}
function creep.load()
creep = {}
creep.x = 10
-- other stuff
end
-- stuff
return creep
But when you run
Code: Select all
creep = {}
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.
Check out my blog on gamedev
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: How to reference variables from one .lua file to another
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:But when you runthen the local table in the creep.lua-closure gets replaced by an empty table (this does not delete the table that is stored globally).Code: Select all
creep = {}
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().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.
Help us help you: attach a .love.
Re: How to reference variables from one .lua file to another
Okaay. I've just been doing it like this:
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
Code: Select all
creep = {}
function creep.load()
creep = {}
creep.x = whatever
return creep
Thanks heaps, guys
Re: How to reference variables from one .lua file to another
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.
Recall that the line
Code: Select all
creep = {}
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.
Check out my blog on gamedev
Re: How to reference variables from one .lua file to another
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
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.
It is explained in the OOP-chapter in Programming in Lua. The first page is enough to read.
Check out my blog on gamedev
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 4 guests