Help with "require" needed

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

Help with "require" needed

Post by Ser_Mitch »

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
Attachments
help.love
(589 Bytes) Downloaded 316 times
User avatar
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

Post by Roland_Yonaba »

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.

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

Re: Help with "require" needed

Post by Ser_Mitch »

Thank you so much, it all works now :D But even better, I understand why :)
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Help with "require" needed

Post by Nixola »

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
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Ser_Mitch
Prole
Posts: 12
Joined: Wed Oct 17, 2012 12:06 am

Re: Help with "require" needed

Post by Ser_Mitch »

All good nixola.. Thanks guys, I wasn't actually expecting to get an answer back so quick :P
User avatar
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

Post by Roland_Yonaba »

Obey...Khal Drogo...
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Help with "require" needed

Post by kikito »

Roland_Yonaba wrote:Obey...Khal Drogo...
It is known.
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests