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
[Solved] Access table information from a different file?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
[Solved] Access table information from a different file?
Last edited by Rockford on Sat Apr 26, 2014 9:04 pm, edited 1 time in total.
- DaedalusYoung
- Party member
- Posts: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: How to access table information from a different file?
In the file where you want to access these values, you put:
Now, you can just read and write the player.x and player.y values where needed.
Code: Select all
local player = require 'player'
Re: How to access table information from a different file?
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
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
Re: How to access table information from a different file?
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.
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 177 times
- SneakySnake
- Citizen
- Posts: 94
- Joined: Fri May 31, 2013 2:01 pm
- Contact:
Re: How to access table information from a different file?
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
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
-
- Party member
- Posts: 730
- Joined: Sat Apr 26, 2014 7:46 pm
Re: How to access table information from a different file?
as long as you require everything at the top of main.lua you would be to access and modify the values.
Re: How to access table information from a different file?
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
That worked and I understand a little bit more about the whole thing. I will delve into the links you provided.
Thanks
Who is online
Users browsing this forum: Google [Bot], IsraelSX and 6 guests