How to use "self" properly?

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
sololove2d
Prole
Posts: 7
Joined: Fri May 22, 2020 6:13 pm

How to use "self" properly?

Post by sololove2d »

When I create a table for a character, I can do it this way:

Code: Select all

h01 = {}
h01.body = love.physics.newBody(runWorld, 350, 100, "dynamic")
h01.shape = love.physics.newRectangleShape(66, 92)
h01.fixture = love.physics.newFixture(h01.body, h01.shape, density)
Note h01.fixture are using previously defined properties as parameters for newFixture.

However, if I want to use metatable, it does not like "self":

Code: Select all

h01 = {}

function h01:Create()
    local this =
    {

        body = love.physics.newBody(runWorld, 350, 100, "dynamic"),
        shape = love.physics.newRectangleShape(66, 92),
        fixture = love.physics.newFixture(self.body, self.shape, density)


    }
    setmetatable(this, h01)
    return this
end
I got error:

Code: Select all


bad argument #1 to 'newFixture' (Body expected, got nil)
How should I refer to these 2 previously defined properties in fixture in this case?
duaner
Prole
Posts: 41
Joined: Thu May 07, 2020 6:43 pm
Contact:

Re: How to use "self" properly?

Post by duaner »

At the point you're referencing body in the table, self doesn't yet have the meaning you want. You're probably going to have to do it the way you did in the first example. At least, that's what I've been doing.

Code: Select all

function h01:Create()
    local this =
    {

        body = love.physics.newBody(runWorld, 350, 100, "dynamic"),
        shape = love.physics.newRectangleShape(66, 92),

    }

    this.fixture = love.physics.newFixture(this.body, this.shape, density)

    setmetatable(this, h01)
    return this
end
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: How to use "self" properly?

Post by 4vZEROv »

Code: Select all

function h01:Create()
    local tbl = {}
        tbl.body = love.physics.newBody(runWorld, 350, 100, "dynamic"),
        tbl.shape = love.physics.newRectangleShape(66, 92),
        tbl.fixture = love.physics.newFixture(tbl.body, tbl.shape, density)
    return setmetatable(tbl , h01)
end
"self" is just a hidden parameter passed to the function corresponding to the table calling the function.
sololove2d
Prole
Posts: 7
Joined: Fri May 22, 2020 6:13 pm

Re: How to use "self" properly?

Post by sololove2d »

Thanks. I thought I could use "self" within the function when defining it.
MrFariator
Party member
Posts: 511
Joined: Wed Oct 05, 2016 11:53 am

Re: How to use "self" properly?

Post by MrFariator »

Consider the following for illustration purposes:

Code: Select all

local myTable = {}
function myTable.myFunc ( self ) -- note the use of ".", note that in a situation like this 'self' is just a convention
  print ( self == myTable )      -- prints true
end
-- is equivalent to
local myTable = {}
function myTable:myFunc ( ) -- note the use of ":", "self" is received implicitly
  print ( self == myTable ) -- prints true
end

-- these two are equivalent
myTable.myFunc(myTable)
myTable:myFunc()
When you use a colon (":") when calling a function contained within a table, the table itself is automatically passed to the function as its first parameter. And when you define a function inside a table using a colon, the table ("self") is received implicitly under the hood. It's all syntax sugar.

In your use case specifically, you were trying to use h01 itself as the "self", even though what you wanted to use was the newly created object (this, or tbl in zero's fix).
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: How to use "self" properly?

Post by 4vZEROv »

Also in your code you tried to do some calculation on a field inside the same table declaration :

Code: Select all

-- error
local myTable = {
  a = 5,
  b = a * 2
}
You can't do that because a is not yet declared, if you want to do that:

Code: Select all

local myTable = {
  a = 5
}
myTable.b = myTable.a * 2
-- or
local myTable = {}
myTable.a = 5
myTable.b = myTable.a * 2
sololove2d
Prole
Posts: 7
Joined: Fri May 22, 2020 6:13 pm

Re: How to use "self" properly?

Post by sololove2d »

Thanks a lot. After played around for a while, I found out the way duaner mentioned works best for me.
Post Reply

Who is online

Users browsing this forum: sbr2729 and 70 guests