So basically in this i am confused about the return 1 part, I am using code from and AlvinBlox vid. But my question is i though you used returning to be able to access things later or in module scripts, when you return the functions or like when you return a function and turn it into a variable so you can call it and change it’s parameters. However this is doing none of this, could someone please explain why we are returning 1 and how it affects the code???
local myTable = {30,20,10}
local metatable = {
__ index = function(table,index)
print("No data at this index")
return 1
end
}
setmetatable(mytable,mymetatable)
local mynumber = mytable[5]
local anotherNumber = mynumber *5
You are getting confused with returning, nothing to do with metatables, first learn how returning works then you can learn metatables (knowing what return does is vital)
local function add (a,b)
local result = a + b -- if a is 1 and b is 2, result will = 3
return result --> "returns" the result back to were the function was called
end
local mathresult = add(1,2)
print(mathresult) --> mathresult = 3, if you didn't do return, mathresult would = nil
__index is a meta method that is ran if you attempt to index a table and it contains nil
So
local myTable = {30,20,10}
print(myTable[5]) --Prints nil cause there's no 5th index in the table
Basically what the code is doing is
an index has been given to myTable, check if there’s already something in the table in that index and if there is, return that
Nothing in that index has been found, so run the __index meta method
The method prints out "No data at this index" and returns 1
So this basically means that because of the method, if you print to print out an index with no data, such as myTable[5], it’s going to return 1 instead of nil
thats not the problem.Please re read and you will understand, i am not confused baout metatables i m confused about the use of returning in this metable. I know what is happening in this code i just dont understand the return 1 line
return does exactly the same thing in metatables as it does not in metatables
local metatable = {
__index = function(table,index)
return banana -- returns banana back to where the index was called
end)
print(table_thats_set_to_metatable[5] -- index 5 doesn't exist
--> prints banana
Ohhh wait i think i get it. So it sends 1 back to the error to stop it from erroring, instead giving it a default value, that will now be tiemsed by five???
We are trying to explain it works just like returns work
print(nice_table[5]) -- index 5 in table doesn't exist
-- checks if there is a metatable
-- there is a metatable
-- checks if there is an index metamethod
-- fires __index method
-- returns whatever you "return" to where it was called in this case our print statement
-- prints out what got returned ("1" in your case)
local myTable = {30,20,10} -- some numbers that were already defined
local metatable = {
__index = function(table,index)
print("No data at this index") -- notifies that the index does not exist
return 10 -- instead of returning nil it will return 10
end
}
setmetatable(myTable,metatable)-- sets the metatable to myTable
local mynumber = myTable[5] -- myTable[5] does not exist so it will return 10 instead
local anotherNumber = mynumber *3 -- this will return 30
print(mynumber, anotherNumber) -- 10 30