Hello guys, I'm kinda new to Lua and i have some previous experience with OOP in Java and C++. I've been playing with Love2d, i made a player class but i can't get it to work every time i load my game into love2d this error shows.
debug = true
player = require 'Player'
p1 = player.new()
function love.load(arg)
end
function love.update(dt)
p1.movement()
end
function love.draw(dt)
end
Player = {}
function Player.new(x,y)
local plyr setmetatable({}, Player)
plyr.x = x
plyr.y = y
plyr.xspeed = 0
plyr.yspeed = 0
return plyr
end
function Player:movement()
if love.keyboard.isDown('a') then
print ("love")
end
end
debug = true
require 'player'
player = Player.new(1,2)
print (player.x)
function love.load(arg)
end
function love.update(dt)
end
function love.draw(dt)
end
It works because you defined Player as a global in player.lua
You generally don't want to do that since it pollutes the scope.
You do local Player = {}
and then
return Player
at the end.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
local Player = {}
Player.__index = Player
function Player.new(x,y)
local self = setmetatable({}, Player)
self.x = x
self.y = y
return self
end
return Player
if someone give me an example of how should be done i would really appreciate it
You're getting that error because there's no longer a Player variable visible at the point where you reference it. When you call require, it returns whatever your module returned (more or less; please read the manual for the exact details). You need to store that value in a local variable.
Japple wrote:
I'm still gettin an error
if someone give me an example of how should be done i would really appreciate it
Hi Japple,
The way you have defined you code in Player.lua is quite fine. You defined it in a way it is self-contained within the scope of the file itself, which is a very good practive you should keep doing when writing Lua code.
But you are getting an error because of the way you are using it inside main.lua.
See, when you require the file Player, it returns something (because of the return statement at the end of thi very file). But obviously, if you wan to use what this file returns (the module Player), then you need to assign it to some variable to get a proper reference to it.
-- Catch what is returned by the file player.lua
-- and assign it to a local variable named Player withing the file main.lua
local Player = require 'player'
-- and then use it as you wish
player = Player.new(1,2)
-- etc etc
Also, as you can see, the name of the table inside your "class" (i.e. other file(s)), the name of that file, and the name of the variable you'll require it into can all differ, though it's a good thing to be consistent in these matters.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.