How do i create my own “datatype”? I know in a language like java its called a class, but how do i do it in lua? I never see it done… I am trying to recreate and optomize source’s entity networking, but to do so i need a datatype to return. How can i make one that i can call a function on? Like… example:
[lua]
local NewEnt = NewNet.Create(“prop_physics”)
NewEnt:CallSomeFunction()
[/lua]
__index redirects to a table or function when the table you setmetatable is referenced/indexed/accessed/whatever, basically passing what you want to pass instead of the table itself.
Yeah, you do use metatables for that. For example, Angles and Vectors in GMod are made with metatables, unless I’ve cocked up and mixed them up with something else…
[lua]
a = {}
print(a.b)
[/lua]
This will print nil, since b is undefined.
[lua]
c = {}
c.b = “hurr”
a = {}
a.__index = c
print(a.b)
[/lua]
This will print “hurr” because when it can’t find a value for index b in table a it looks in table c since that’s what __index is set to, there it find that b is equal to “hurr” and thus return “hurr” to the print function.
__index can also be a function.
[lua]
a = {}
a.__index = function(self,key) --self is always equal to a
return "You tried to index "..key.." but it was nil."
end
print(a.b)
[/lua]
Prints “You tried to index b but it was nil.”
If you don’t understand this just read it again and again 'til you do:smile: