Object Oriented Problem

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
jimbobert94
Prole
Posts: 16
Joined: Mon Jun 25, 2012 7:17 am

Object Oriented Problem

Post 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()

Code: Select all

width = image.body:getWidth()

Code: Select all

width = self.body:getWidth()
So which one is it?
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Object Oriented Problem

Post 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()
jimbobert94
Prole
Posts: 16
Joined: Mon Jun 25, 2012 7:17 am

Re: Object Oriented Problem

Post 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.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Object Oriented Problem

Post 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.
jimbobert94
Prole
Posts: 16
Joined: Mon Jun 25, 2012 7:17 am

Re: Object Oriented Problem

Post 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. :awesome:
jimbobert94
Prole
Posts: 16
Joined: Mon Jun 25, 2012 7:17 am

Re: Object Oriented Problem

Post by jimbobert94 »

There should totally be a upvote/downvote system here by the way.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Object Oriented Problem

Post by Nixola »

There was the Karma mod, but a phpBB update broke it
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 3 guests