How to reference variables from one .lua file to another...

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Ser_Mitch
Prole
Posts: 12
Joined: Wed Oct 17, 2012 12:06 am

How to reference variables from one .lua file to another...

Post 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
Attachments
Halp.love
(7.9 KiB) Downloaded 848 times
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: How to reference variables from one .lua file to another

Post 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.)
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: How to reference variables from one .lua file to another

Post 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.
User avatar
Ser_Mitch
Prole
Posts: 12
Joined: Wed Oct 17, 2012 12:06 am

Re: How to reference variables from one .lua file to another

Post 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?
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: How to reference variables from one .lua file to another

Post 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

Code: Select all

creep = {}
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.
User avatar
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

Post by Robin »

micha wrote:But when you run

Code: Select all

creep = {}
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().
Help us help you: attach a .love.
User avatar
Ser_Mitch
Prole
Posts: 12
Joined: Wed Oct 17, 2012 12:06 am

Re: How to reference variables from one .lua file to another

Post 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 :)
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: How to reference variables from one .lua file to another

Post 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

Code: Select all

creep = {}
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.
User avatar
Ser_Mitch
Prole
Posts: 12
Joined: Wed Oct 17, 2012 12:06 am

Re: How to reference variables from one .lua file to another

Post 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.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: How to reference variables from one .lua file to another

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

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 6 guests