[Solved] Access table information from a different file?

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
Rockford
Prole
Posts: 26
Joined: Fri Mar 14, 2014 9:40 pm
Location: USA

[Solved] Access table information from a different file?

Post by Rockford »

Hi,

Quick question: how would I go about accessing table information from a file other than the file the table is in?

I have player.lua, with this information:

player = {}
player.x = 250
player.y = 250


Then in collisions.lua, I need to use player.x in my collision functions. I tried putting require = "player" at the top of the collisions
file but it didnt solve it.

Rockford
Last edited by Rockford on Sat Apr 26, 2014 9:04 pm, edited 1 time in total.
User avatar
DaedalusYoung
Party member
Posts: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: How to access table information from a different file?

Post by DaedalusYoung »

In the file where you want to access these values, you put:

Code: Select all

local player = require 'player'
Now, you can just read and write the player.x and player.y values where needed.
Rockford
Prole
Posts: 26
Joined: Fri Mar 14, 2014 9:40 pm
Location: USA

Re: How to access table information from a different file?

Post by Rockford »

Thanks!

Do you mind if I ask why it needs to be like that? Right now with love and lua I am having the most trouble with learning how things relate/use each other.

Thanks
Rockford
Prole
Posts: 26
Joined: Fri Mar 14, 2014 9:40 pm
Location: USA

Re: How to access table information from a different file?

Post by Rockford »

Hi,

I implemented the fix you suggested but now I am getting another error that I cant figure out.

It tells me that it cant index the upvalue player because it is a boolean? There are no booleans in the player table.

I am really not sure what it means. I searched for a bit but cant find out what it means (almost every other lua website/forum are quite technical, so its hard for me to grasp).

I have attached my love file as thought it might be helpful.

By the way, there will be more errors after that, but dont worry about them, I am having a fun time trying to figure them out - its helping me learn - but these 2 I just cant figure out. I originally had the "game" working, but all the code was jumbled up, so I set out to organize it. So I have been working through the errors that came up due to me moving stuff around, all up to now I have been able to fix.

Any help on this would be very much appreciated, even just a point in the right direction.
Attachments
Game.love
(71.69 KiB) Downloaded 170 times
User avatar
SneakySnake
Citizen
Posts: 94
Joined: Fri May 31, 2013 2:01 pm
Contact:

Re: How to access table information from a different file?

Post by SneakySnake »

Short answer:
Use `require('player')` instead of `local player = require('player')`.

Long answer:
If you use `local player = require('player')`, you are creating a local `player` variable, which will shadow the global `player` variable in the scope it was declared in.
Why is the local `player` true?
To understand that, you must understand what `require()` returns.
For the full answer, see: http://www.lua.org/manual/5.1/manual.html#pdf-require

The simple answer is, if the loaded module returns a value, `require()` will return that value, otherwise it will return true, which is a boolean value. Since `player.lua` doesn't return any value, require('player') will return true.

So why should you ever use `local modname = require('module')`?
To understand that, you must understand the benefits of local variables. Here is some reading on them: http://www.lua.org/pil/4.2.html

Here is a tutorial on creating modules: http://lua-users.org/wiki/ModulesTutorial
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: How to access table information from a different file?

Post by bobbyjones »

as long as you require everything at the top of main.lua you would be to access and modify the values.
Rockford
Prole
Posts: 26
Joined: Fri Mar 14, 2014 9:40 pm
Location: USA

Re: How to access table information from a different file?

Post by Rockford »

Awesome, thank you Snake!

That worked and I understand a little bit more about the whole thing. I will delve into the links you provided.

Thanks
Post Reply

Who is online

Users browsing this forum: No registered users and 80 guests