Hey guys,
I'm new to Love and Lua and all this, so please don't be mad if this is a noobish question...
I'm having trouble with the 'require' module... I want to require player.lua in main.lua. I think i have done it the same as i have seen in the official lua documentation and in other examples, mainly from these forums, but all i get is the error message:
Error
player.lua:3 attempt to index global 'player' (a nil value)
Traceback
player.lua:3 in main chunk
[C]: in function 'require'
main.lua:3 in main chunk
[C]: in function 'require'
[C]: in function 'xpcall'
(what does all this mean?)
I have looked through these forums, and the rest of the internet, but i cant seem to find what im doing wrong...
Someone help please :s
Help with "require" needed
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Help with "require" needed
- Attachments
-
- help.love
- (589 Bytes) Downloaded 316 times
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: Help with "require" needed
Well, the problem is not with require.
Lua says there is an error in player.lua, at line 3, so require cannot load this file.
In player.lua, define player as a local table first, before adding functions inside. Then return this table later on.
That should fix the problem, but require will still err, I guess, because inside player.update() function, game is an undefined variable. But that's not really my call.
Lua says there is an error in player.lua, at line 3, so require cannot load this file.
In player.lua, define player as a local table first, before adding functions inside. Then return this table later on.
Code: Select all
-- at the top of player.lua
local player = {}
-- same as before, but obviously, remove line 8 in player.load() (i.e player = {})
-- at the bottom of player.lua
return player
Re: Help with "require" needed
Thank you so much, it all works now But even better, I understand why
Re: Help with "require" needed
The require works fine, the problem is in player.lua. Do you know how tables work?
When you write "function player.load()" you are telling LÖVE to "grab" the table named "player" and put a function in its "load" field. The problem is you didn't ever create that table, you should write "player = {}" on top of your player.lua, or inside main.lua before requiring player.lua. I recommend the former, though.
EDIT: Aw, too slow >.<
Anyway, Roland's right, the table should be local ("local player = {}") and it should be returned at the end of the file ("return player")
EDIT #2: Awfully slow. T.T
When you write "function player.load()" you are telling LÖVE to "grab" the table named "player" and put a function in its "load" field. The problem is you didn't ever create that table, you should write "player = {}" on top of your player.lua, or inside main.lua before requiring player.lua. I recommend the former, though.
EDIT: Aw, too slow >.<
Anyway, Roland's right, the table should be local ("local player = {}") and it should be returned at the end of the file ("return player")
EDIT #2: Awfully slow. T.T
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Re: Help with "require" needed
All good nixola.. Thanks guys, I wasn't actually expecting to get an answer back so quick :P
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Help with "require" needed
It is known.Roland_Yonaba wrote:Obey...Khal Drogo...
When I write def I mean function.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 8 guests