Referencing "self" inside of class functions outside of class creation

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
baconhawka7x
Party member
Posts: 484
Joined: Mon Nov 21, 2011 7:05 am
Contact:

Referencing "self" inside of class functions outside of class creation

Post by baconhawka7x »

So I have a "Player.lua" file that returns a function, that creates / returns a class:

player.lua

Code: Select all

function CreatePlayer()
  local player={
    load=load,
    update=update,
    draw=draw,
  }
  setmetatable({},player)
  player.__index=player
  return player
end
As you see the class has 3 variables, load update and draw. Each of those are defined in the same file as local functions, I thought defining them inside of the player table would get messy. However, inside of those, the only way to manipulate the player's values is to pass the player class table created by that function as an argument, for example -

Code: Select all

var newPlayer=CreatePlayer()
newPlayer.load(newPlayer)
I'm very new to Lua's "classes" and I was wondering if there is a way around this? I've seen people use "self.value" but I'm not sure exactly how that works. I'm betting I have my "Player.lua" set up all wrong. Any help would be very appreciated, I've been looking for an answer for this for a while, but it's kind of difficult to figure out what to look up!
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Referencing "self" inside of class functions outside of class creation

Post by bartbes »

Well, the call site change is simple:

Code: Select all

newPlayer.load(newPlayer)
-- is equivalent to
newPlayer:load()
As for the definition site, you can just name the first parameter self. A similar shorthand exists there:

Code: Select all

function sometable:somefunc(arg)
end
-- is equivalent to
function sometable.somefunc(self, arg)
end
User avatar
baconhawka7x
Party member
Posts: 484
Joined: Mon Nov 21, 2011 7:05 am
Contact:

Re: Referencing "self" inside of class functions outside of class creation

Post by baconhawka7x »

Oh perfect! Thank you for the quick answer!

This works well, I'm now defining those 3 functions inside of the CreateClass function with that shorthand.

Code: Select all

function CreatePlayer()
  local newPlayer={}
  setmetatable({},newPlayer)
  newPlayer.__index=newPlayer

  function newPlayer:load()
  end
  function newPlayer:update(dt)
  end
  function newPlayer:draw()
  end

  return newPlayer
end
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Referencing "self" inside of class functions outside of class creation

Post by bartbes »

I should probably note that the setmetatable call is useless, since it sets the metatable of a temporary table you immediately discard.

The usual way people do this is by creating these functions once, instead of for every object, and then making objects have a metatable with an __index metafield that points to the function, as follows:

Code: Select all

local player = {}
player.__index = player

function player:load()
end
function player:update(dt)
end
function player:draw()
end

function CreatePlayer()
  local newPlayer={}
  setmetatable(newPlayer, player)
  return newPlayer
end

-- or in one go:
function CreatePlayer()
  return setmetatable({}, player)
end
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests