What does self mean?

The question is simple. I have seen the keyword self used in scripts. But what Is self? Is it the workspace ? Is it the game ? Can someone tell me what it is?

And don’t bother me with “use the searchbar next time” because I have already done that

2 Likes
7 Likes

I am assuming self is the table called “hmm” in the following example?

local hmm = {"hhwigwoihquoig"}

function hmm:Method()
  print(self[1]) -- self is automatically defined
end
hmm:Method()
4 Likes

@DragRacer31, i have understand it with the help of the others and by asking on the forum, so first i don‘t now how you have searched, but i already ask this question, second i will try to explain it you.

Let say you have a function in a table:

local Table = {number = 0}
function Table:TestFunction() 
end

And now we say that we want to get the number from the Table, we can simply use the self keyword, as it was the same as the table self:

local Table = {number = 0}
function Table:TestFunction() 
    self.number = 1
end

If you are using a dot function, then you need to define you self the self keyword:

local Table = {number = 0}
function Table.TestFunction(customtable) 
    customtable.number = 1
end

This was the difference when you are using a : or a . function.

3 Likes

I would like to add to @Eternalove_fan32 post that I think there is no use for self if you are not creating an object from it for example

-- adding to his post @Eternalove_fan32 post
 function Table:new (o)
      o = o or {}   -- create object if user does not provide one
      setmetatable(o, self)
      self.__index = self
      return o
end
--Now I can create 2 individual table from the variable Table that Eternalove_fan32 created
local newTable1 = Table:new()
local newTable2 = Table:new()

--here we are printing the value of the variable number in the table
print(newTable1.number)  --output 0
print(newTable2.number)  --output 0

--This is where the self comes in 
newTable1:TestFunction()  --I am using Eternalove_fan32 TestFunction function

--Now lets print it out again
print(newTable1.number)  --output 1
print(newTable2.number)  --output 0
2 Likes

Note that you can call a colon function with a dot, however you’ll need to provide self as the first argument.
Sometimes you may need to pass a colon function with a dot since passing it with a colon is always calling it.

local Object = {}
Object.__index = Object

function Object.new()
    return setmetatable({
        Number = 1
    }, Object)
end

function Object:Method()
    return self.Number
end

--// Create a new Object
local MyObject = Object.new()
--// Call it
local DotCall = Object.Method(Object)
local ColonCall = Object:Method()
print(DotCall) --// 1
print(ColonCall) --// 1
4 Likes

@ReturnedTrue, i still not understand why we use the __index methamethod, can you say it me pls?

1 Like

The index metamethod is required so when the Object is used as a metatable it’s contents are returned.

local Object = { Property = "Hello World!" }
Object.__index = true --// Set any indexes to return true

local New = setmetatable({}, Object)
print(New.Property) --// True

Object.__index = Object --// Set any indexes to return the indexed through the Object table

local New2 = setmetatable({}, Object)
print(New2.Property) --// Hello, World!
3 Likes

Nope, don‘t understand. Your example help me, and i understand (i think), but can you say it, but in a more… how to say, beginner form? I not usually use metatables so i still am a beginner with it. So, when you set to true you say that that that was in the table was true, as you say that we set any index to return true, but if you are doing this Object.__index = Object, you can make the dot operator possible (New2.Property). If i right understand, this is cool! Hope it will help others peoples that what you have say.

1 Like

In layman’s terms, if you assign __index to a table then it’ll search through that table if it isn’t in the actual table.

local Object = { Cash = 100 } --// Cash is defined in the Object class
Object.__index = Object

function Object.new(Amount)
    local ActualTable = {
        Gems = Amount; --// Define the Gems in the actual table, not the metatable
    }

    return setmetatable(ActualTable, Object)
end

local New = Object.new(10) --// Give 10 for the Gems amount
print(New.Gems) --// Found in the ActualTable, 10
print(New.Cash) --// Found in the Object class, 100
5 Likes