Userdata: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Blocco
No edit summary
>JulienDethurens
No edit summary
 
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
A userdata is just another way to represent data. It is very similar to a [[Metatables|metatable]]. You can create a new userdata by passing true to the newproxy function.
==Userdata==
===What is a userdata?===
A {{type|userdata}} is the type Lua uses for data structures in the underlying C program. There aren't native Lua functions for handling those types, they must be handled via [[metatables]]. All Roblox objects and events are {{type|userdata}} Lua type.


<big><span style="color:#ff0000;">WARNING: newproxy is temporarily disabled due to maintenance on _G</span></big>
===Using userdatas===
<pre>
You can create a new {{type|userdata}} with an empty metatable by passing true to the undocumented [[newproxy]] function. This {{type|userdata}} cannot be used for anything other than invoking functions via its metatable.  
local NewUserData = newproxy(true) -- New userdata; Returns an empty metatable
getmetatable(NewUserData).__index = {Name = "Foo", FooValue = "EPIC"} -- index MetaMethod
print(type(NewUserData), NewUserData.Name, NewUserData.FooValue)
</pre>


In the output window, you should see
{{Example|<pre>
<pre>
local ud = newproxy(true) -- new userdata with an empty metatable
userdata Foo EPIC
getmetatable(ud).__index = {Foo = "Bar"} -- index metamethod
</pre>
print(type(ud), ud.Foo)
[[Category:Data Types]]
 
Output:
userdata: 0x631fc8 Bar
</pre>}}
 
[[Category:Data types]]

Latest revision as of 17:59, 7 April 2012

Userdata

What is a userdata?

A userdata is the type Lua uses for data structures in the underlying C program. There aren't native Lua functions for handling those types, they must be handled via metatables. All Roblox objects and events are userdata Lua type.

Using userdatas

You can create a new userdata with an empty metatable by passing true to the undocumented newproxy function. This userdata cannot be used for anything other than invoking functions via its metatable.

Example
local ud = newproxy(true) -- new userdata with an empty metatable
getmetatable(ud).__index = {Foo = "Bar"} -- index metamethod
print(type(ud), ud.Foo)

Output:
userdata: 0x631fc8	Bar