Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

To make classes, why do you set __index = Class?

Asked by
stepatron 103
4 years ago
Edited 4 years ago

I feel like every other explanation is so close to actually answering it, except it never does.

First of all, what the heck does __index really do? I don't want an analogy, just give it to me straight. The most I understand is that it's used to reference a key to another table if the original key doesn't have that. But why would you ever want that? When would that ever happen, where you have an empty key, but need a value to it? You have a missing user ID in the User datastore, so you check the Premium User datastore? And I doubt that's very likely to happen.

Secondly, why do stuff like:

local class = {}
class.__index = class

function class.new()
    ...
end

when it works just fine without "class.__index = class"? The function's already logged into the table, there's literally no way (besides hardware glitches) you'd not get the correct function when you call for it.

I'm just so confused on why you want to do this. Everyone keeps telling to do it, but never explaining why.

Figured It Out After hours of research, I've finally figured out what the crap this does. It's to bring in the class functions to an object. Sorry if this is a bit a rushed, but I'm more than a bit angry that so many people couldn't have said this simple thing instead.

So when you first create an object from a class in this example and initialize it (giving it it's own individual properties, like how a cat has 4 legs.):

local Entity = {}

function Entity.New(name)
    local new_entity = {}

    new_entity.Name = name

    return new_entity
end

function Entity:Walk(speed)
    self.Speed = speed
end

local dog = Entity.New("dog")
dog:Walk(5)

This'll throw an error message telling you that dog;Walk() is nil, meaning that it doesn't exist. Confusing, right? Because since this is prototypal inheritance (don't worry too much about this, but it's basically normal class inheritance such as in python but the object doesn't inherit everything in the original class), it know's nothing about the Walk function.

To do this, we gotta set its metatable to the class (it basically makes the object become a subclass of the metatable). Now we could do it inside the new function by setmetatable(new_entity, Entity), but it appears that doing Entity.__index = Entity appears to be just syntax sugar (just a style of code). Now if we add that code, the code will execute completely.

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

This seems to be out of the "Roblox Lua" API but taking some information from here and here it is used for some useful aspects. You might have been misinformed to what it does is what brings up your question.

When calling the "__index" it actually refers to when adding information to a new metatable of the same class it automatically adds-in that extra piece of required data for you. Finding a way to use this is up to you. :)

Final Edit: For anyone looking for a quick definition this is just a function that runs when you add something to the table.

If you have any questions or issues please contact me.

0
I don't quite get why it's useful in the context where you set the table's __index metamethod back to the table. According to this answer: https://scriptinghelpers.org/questions/76948/what-does-table__index-table-do-when-doing-oop , it appears that it avoids redefining functions, which I'm struggling to understand. stepatron 103 — 4y
0
To put it into perspective and taking some more information from the sources it just means that it runs every time you add something to the table. One of the examples you can do with this is that once you add-in something like an array with the same data (player = {position, size, etc}) that function checks for missing data if you didn't apply anything and gives it a value. lazycoolboy500 597 — 4y
0
In more simpler terms it's literally just a function that runs once you have added something to the table. lazycoolboy500 597 — 4y
Ad

Answer this question