Troubles with Lua class and OOP

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
Japple
Prole
Posts: 4
Joined: Sun Jul 31, 2016 10:45 pm

Troubles with Lua class and OOP

Post by Japple »

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.

Image

Here's my main.lua

Code: Select all

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
and here's player.lua

Code: Select all

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
Please bear with me i'm retarded :? .
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Troubles with Lua class and OOP

Post by Nixola »

You're supposed to return Player at the end of player.lua.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Japple
Prole
Posts: 4
Joined: Sun Jul 31, 2016 10:45 pm

Re: Troubles with Lua class and OOP

Post by Japple »

Nixola wrote:You're supposed to return Player at the end of player.lua.
I think i found another way to do it

main

Code: Select all

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
player

Code: Select all

Player = {}
Player.__index = Player

function Player.new(x,y)
	local self = setmetatable({}, Player)
	self.x = x
	self.y = y
	return self
end
After hours of searching it works! I'm not quite sure what metatables are use for tho
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Troubles with Lua class and OOP

Post by zorg »

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 :3True 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.
Japple
Prole
Posts: 4
Joined: Sun Jul 31, 2016 10:45 pm

Re: Troubles with Lua class and OOP

Post by Japple »

zorg wrote: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.
I'm still gettin an error
Image

Code: Select all

debug = true 
require 'player'
player = Player.new(1,2)
print (player.x)


function love.load(arg)
   
end

function love.update(dt)

end

Code: Select all

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
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Troubles with Lua class and OOP

Post by airstruck »

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.

http://lua-users.org/wiki/ModulesTutorial
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Troubles with Lua class and OOP

Post by Roland_Yonaba »

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.

So something like:

Code: Select all

-- 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
 
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Troubles with Lua class and OOP

Post by zorg »

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. :3
Me and my stuff :3True 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.
Japple
Prole
Posts: 4
Joined: Sun Jul 31, 2016 10:45 pm

Re: Troubles with Lua class and OOP

Post by Japple »

Thanks you all guys it works :awesome:
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest