Page 1 of 1
Object Oriented Problem
Posted: Wed Dec 12, 2012 2:21 am
by jimbobert94
So I want to use an attribute in the same object/table that the caller is in. i.e.:
Code: Select all
function create_player(self,filename,x,y)
self = {
placement = {
x = x,
y = y
},
image = {
body = love.graphics.newImage(filename),
width = body:getWidth(),
height = body:getHeight()
},
The rest of the function is irrelevant. Anyway, when I run the code, an error pops up saying that body is a nil value. So my problem lies there. I've tried
Code: Select all
width = self.image.body:getWidth()
So which one is it?
Re: Object Oriented Problem
Posted: Wed Dec 12, 2012 2:58 am
by Santos
The reason why
width = body:getWidth() isn't working is because it's looking for a variable named
body, which isn't actually there.
width = self.image.body:getWidth() seems like it should work, however if you're trying to access this
within the definition of the
self table,
self doesn't exist yet.
After self has been created though, this will work!
Code: Select all
function create_player(self,filename,x,y)
self = {
placement = {
x = x,
y = y
},
image = {
body = love.graphics.newImage(filename)
}
}
self.image.width = self.image.body:getWidth()
self.image.height = self.image.body:getHeight()
Re: Object Oriented Problem
Posted: Wed Dec 12, 2012 3:09 am
by jimbobert94
Santos wrote:The reason why
width = body:getWidth() isn't working is because it's looking for a variable named
body, which isn't actually there.
width = self.image.body:getWidth() seems like it should work, however if you're trying to access this
within the definition of the
self table,
self doesn't exist yet.
After self has been created though, this will work!
Code: Select all
function create_player(self,filename,x,y)
self = {
placement = {
x = x,
y = y
},
image = {
body = love.graphics.newImage(filename)
}
}
self.image.width = self.image.body:getWidth()
self.image.height = self.image.body:getHeight()
I thought that might be it. Nice alternative, Thanks a lot. If I can upvote you, I will. Let me check.
Re: Object Oriented Problem
Posted: Wed Dec 12, 2012 4:02 am
by Inny
You probably don't want to abuse the variable "self" like that, since you'll end up heavily shadowing it with other object-oriented code.
Instead, define your player class like so:
Code: Select all
Player = {}
Player.__index = Player
function Player.new(filename,x,y)
local self = {
placement = {
x = x,
y = y
},
image = {
body = love.graphics.newImage(filename)
}
}
self.image.width = self.image.body:getWidth()
self.image.height = self.image.body:getHeight()
return setmetatable(self, Player)
end
function Player:doSomething()
-- example of defining a class function for the objects to use
end
player = Player.new("player.png", 0, 0)
player:doSomething()
The important parts of this are: self is local to the function, not global. Player, with a capital P, is that table where you can define functions for the player object. player, with a lowercase p, is the global object that you'll interact with.
This is the bare minimum basic class system for lua, which doesn't require a framework, and should cover most of your needs.
Also, I recommend this to everyone, please read the free ebook
Programming Lua.
Re: Object Oriented Problem
Posted: Thu Dec 13, 2012 7:48 pm
by jimbobert94
Inny wrote:You probably don't want to abuse the variable "self" like that, since you'll end up heavily shadowing it with other object-oriented code.
Instead, define your player class like so:
Code: Select all
Player = {}
Player.__index = Player
function Player.new(filename,x,y)
local self = {
placement = {
x = x,
y = y
},
image = {
body = love.graphics.newImage(filename)
}
}
self.image.width = self.image.body:getWidth()
self.image.height = self.image.body:getHeight()
return setmetatable(self, Player)
end
function Player:doSomething()
-- example of defining a class function for the objects to use
end
player = Player.new("player.png", 0, 0)
player:doSomething()
The important parts of this are: self is local to the function, not global. Player, with a capital P, is that table where you can define functions for the player object. player, with a lowercase p, is the global object that you'll interact with.
This is the bare minimum basic class system for lua, which doesn't require a framework, and should cover most of your needs.
Also, I recommend this to everyone, please read the free ebook
Programming Lua.
Yeah, I got that after running the code, but thanks for the additional help.
Re: Object Oriented Problem
Posted: Thu Dec 13, 2012 7:49 pm
by jimbobert94
There should totally be a upvote/downvote system here by the way.
Re: Object Oriented Problem
Posted: Thu Dec 13, 2012 8:22 pm
by Nixola
There was the Karma mod, but a phpBB update broke it