What’s a hybrid table? Also, why did you use [] for some indexes?
And what’s the difference from specifying index in a dictionary and array, because can’t you just list them in order for them to be basically the same thing?
.__index is a metamethod used for metatables that are connected to other tables. It gets fired whenever the code tries to search for something in the table connected to the metatable that actually isn’t in the table. It won’t error but instead then is able to search in the metatable for the same value and return it that way. assert() on the other hand just checks if something is false or nil and then just runs an error message. ipairs is index pairs and is used to keep the order of an array from 1 to your array length. pairs doesn’t keep track of order and is used for dictionaries. the keyword “in” doesn’t get used for anything else. getfenv() gets the current environment. I can’t really explain how environments works but a function is a new environment for example. If you were to assign a local variable inside an environment it would only be saved inside that environment and can only be accessed from that environment and not outside of it. Garbage collection gets rid of data that doesn’t get used in the script anymore at all. Luckily lua automatically does this else it would be 10x harder to code without the memory filling up. And garbage are just values that don’t get used anymore in anything.
Making a dictionary and indexing them numerically will allow you to iterate through it in order.
local dictionary = {
[1] = 5,
[2] = 10,
[3] = 12
}
for _,v in ipairs(dictionary) do
warn(v) -- 5, 10, 12
end
But if you replace the indexes with something that isn’t a number, order will be lost and you will have to use pairs which is used for dictionaries (while ipairs is used for arrays)
local dictionary = {
["test1"] = 5,
["test2"] = 10,
["test3"] = 12
}
for _,v in pairs(dictionary) do
warn(v) -- 12, 5, 10 (there is no order)
end
The value that you put in your array has a position called “index”, if it’s the third value in your array then it will have index 3. If you want to access this data then just do Table[3] and it will return the value on that position in your array.
I think a better way to explain this is that 5 is the value at index 1, 7 is the value at index 2, etc. Might confuse him to think that the array starts at index 5