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

What is __index and what happens when you set it to a table?

Asked by
birds3345 177
4 years ago

What is __index how do I use it, and what happens when you set it to a table?

1
They're part of a metatable. https://www.youtube.com/watch?v=aKe6YncHVAo youtubemasterWOW 2741 — 4y

1 answer

Log in to vote
19
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago
Edited 3 years ago

First, we need to understand what Metables are before we get into the functions:

What are metatables:

In Lua, metatables are tables that allow tables to manipulate another table. This means they make a table more powerful or have a greater advantage over something. Metatables allow us to accumulate data and change the behaviour of such property to be added. We can alter keys with a value.

What is __index?

__Index is a function in metatables, which is a hierarchy of Metatables that fires when a table should be indexed. __Index can also return a table.

Why are metatables useful?

Metatables are useful if you want to hide a table or return something that is not a descendant of the workspace. (Meaning it doesn't exist) you can easily use a metatable.

Here's a simple guide on metatables

Metatables usually introduce OOP. That's another topic yet to be introduced!

Fixed grammatical errors. Date: February 19, 2021

0
wow really good answer im bookmarking it :) 123nabilben123 499 — 4y
1
No problem JesseSong 3916 — 4y
0
Fixed minor errors. Date: Feb,20,21 JesseSong 3916 — 3y
Ad

Answer this question