Need help with returning in my metatable

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

thanks SO MUCH for your help

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)

This video explains it well


In summary returning looks like this:

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

no im not im confused about the use of returning in this metable

I understand that completely, its only this i have struggled with as im confused about why he is returning 1

__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
1 Like

No im onyl confused about it in this table I DONT NEED TO KNOW WHAT IT DOES IN OTHER TABLES *SORRY FOR CAPS MY KEYBORAD IS SPASSING OUT

I can’t help you then I just told you what it is doing, in your particular case it returns “1” in my example it returned “banana”

i think that it is impossible to explain maybe, as obviously no one knows waht is happeing in my hed lol

This is a table of integers so returning another integer 1 would be necessary? You can’t do mathematics with nil.

Note: I’m not too experienced with metatables for expect me to be wrong.

I’m pretty sure that it will return 1 if the index is nil.
The anotherNumber variable will return 5 since its multiplying 5 * 1.

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)
1 Like

i know that is why it’s using __index

Here’s an example of how a metatable is used.

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
1 Like