__index, i really dont get it

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Pliskin09
Citizen
Posts: 89
Joined: Fri Jul 24, 2009 8:30 am

__index, i really dont get it

Post by Pliskin09 »

sorry this is more of a pure LUA question but it is being used in my game so... :ultraglee:

anyway, i really dont understand what __index does. could someone explain in layman's terms - the lua website isnt helping. they've made it very confusing. the example they've given is found here: http://lua-users.org/wiki/MetamethodsTutorial (scroll down a little bit bellow String class example). i sort of understand what that does but i dont think it's quiet it. to me it sounds like its just adding the length function to the mt metatable. and since s has had its metatable set to mt earlier on, it can now use this function. am i correct? is this the only use for it..? :ultrashocked::
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: __index, i really dont get it

Post by bmelts »

Okay, here's the short version:

__index is a special field used in a metatable. It defines a function which allows you to define what happens when you attempt to access an index of a table that doesn't exist.

And here's an example (outside of the String one):

Say you wanted to create a NumberTable which acts like a regular Lua table but can only have numbers in it.

It starts out simply enough:

Code: Select all

NumberTable = {} -- create our table
mt = {} -- create our metatable
setmetatable(NumberTable, mt)
Now, normally when you look up a non-existent index of a table, it returns nil. But this is a number table, it can only have numbers in it. So instead of returning nil, let's have it return 0.

Code: Select all

mt.__index = function(t, key)
   return 0
end
And this __index field in action:

Code: Select all

NumberTable.foo = 42
print(NumberTable.foo)
> 42
print(NumberTable.bar)
> 0
So, the __index field is very flexible, and can be used for all sorts of things to make tables do things they normally wouldn't be able to do.

Does it make more sense now?

EDIT: And here's what they're doing in the String example:

Lua's string library has a built-in function called string:len() which returns the length of a string. What the wiki is doing with the String class is making it so if you access the "length" index of a String table (an index which doesn't actually exist!), instead of returning nil, it calculates the length of the string stored in String.value and returns that. So, it's like the "length" index actually exists, even though it doesn't. But it will automatically update if you change the string stored in String.value, since String.length is not actually stored in memory as a number, just calculated when you try to access it.
User avatar
Pliskin09
Citizen
Posts: 89
Joined: Fri Jul 24, 2009 8:30 am

Re: __index, i really dont get it

Post by Pliskin09 »

thanks that makes alot more sense :ultraglee: . also, ive looked up examples on metatables; to me they appear like they're simply a collection of functions that we can associate a table with, right?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: __index, i really dont get it

Post by Robin »

Pliskin09 wrote:to me they appear like they're simply a collection of functions that we can associate a table with, right?
That's the gist of it. Another powerful metamethod is __newindex, which is called if you set a nonexisting table entry.

For example, keeping the table and metatable anjo used:

Code: Select all

mt.__newindex = function(t, key, value)
    if type(value) ~= "number" then
        error("Only numbers are allowed in NumberTables!")
    else
        rawset(t, key, value)
    end
end
That way, using NumberTable.x = 5 works like expected, but NumberTable.x = "Hello" generates an error.
Help us help you: attach a .love.
User avatar
Pliskin09
Citizen
Posts: 89
Joined: Fri Jul 24, 2009 8:30 am

Re: __index, i really dont get it

Post by Pliskin09 »

thanks robin and anjo, you guys are awesome! ^^
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests