New "datatype"

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]

Thanks in advance :stuck_out_tongue:

metatables

Thanks

Edit: Whats this business about __index?? Do i need it?

__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.

Very sorry… im probably a retard or something… but what the hell did you just say?

Haven’t messed with metatables much myself, but it’s something along the lines of this.

[lua]local tbla = {}
tbla.wat = true

local tblb = {}

setmetatable(tblb , {__index = tbla})

print(tblb.wat)[/lua]

Read this: http://www.lua.org/pil/16.html

It can’t get any clearer, once you’ve read the whole chapter several times, manipulating metatables will be easy as fuck.

Which one can i make methods on? Like… im confused. What is tbla and tblb? What IS a metatable anyway?

A metatable is quite literately, data about data. It’s information about how a table should be handled.

Alright maby i don’t mean a metatable. I want to have an object that has properties and methods i can call on it, and i can return one etc.

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…

Can i see the source for them?

I think they are at least. I’m not sure. I’m too busy to check if they are in the gcf or not.

Where can i find the source for them?

Probably somewhere in the GCF.

There’s an example there.

Okay, that was very helpful. Thank you.

If it is only going to be called on entities then you can use…


local ENT = _R.Entity

which is equivalent to


local ENT = getmetatable("Entity")

then you can create the functions like


function ENT:DoSomething()
   self:DoSomeStuffToIt()
end

It just helps speed things up for us lazy people :wink:

Do you even know what metatables really is? thats very unrelated

[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: