Page 1 of 1
How to use "self" properly?
Posted: Tue May 26, 2020 11:34 pm
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?
Re: How to use "self" properly?
Posted: Tue May 26, 2020 11:52 pm
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
Re: How to use "self" properly?
Posted: Wed May 27, 2020 12:18 am
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.
Re: How to use "self" properly?
Posted: Wed May 27, 2020 12:47 am
by sololove2d
Thanks. I thought I could use "self" within the function when defining it.
Re: How to use "self" properly?
Posted: Wed May 27, 2020 1:11 am
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).
Re: How to use "self" properly?
Posted: Wed May 27, 2020 5:52 am
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
Re: How to use "self" properly?
Posted: Wed May 27, 2020 3:56 pm
by sololove2d
Thanks a lot. After played around for a while, I found out the way duaner mentioned works best for me.