|
| 02 Feb 2015 04:31 AM |
| is there a possibility for me doing Thing:Stuff() with Metatables wen calling... and then with 2 Args inside the function itself (self, stuff) and so then use Thing as 'self' and Stuff as 'stuff'? |
|
|
| Report Abuse |
|
|
|
| 02 Feb 2015 05:14 AM |
| Give me an example code of how you would use it, because I didn't understand. |
|
|
| Report Abuse |
|
|
|
| 02 Feb 2015 05:33 AM |
something like
local metatable = { __index = { --Start of Function 1-- __Print = function(Self, Text) print(Self, 'says ', Text) end; --End of Function 1-- } } |
|
|
| Report Abuse |
|
|
|
| 02 Feb 2015 05:56 AM |
| I don't know. I am pretty sure you can, but I don't know. |
|
|
| Report Abuse |
|
|
|
| 02 Feb 2015 10:13 AM |
You'd have to wrap the object. A simple example would be:
function Wrap(obj) return setmetatable({}, { __index = function(t, k) if k == "print" then return print(obj) else return t[k] end, __newindex = function(t,k,v) t[k] = v end, __metatable = "Attempt to modify protected metatable" }) end
local Part = Wrap(workspace.Part)
Part:print() |
|
|
| Report Abuse |
|
|
|
| 02 Feb 2015 10:18 AM |
hmmm... by the looks of it, I'm thinking I can easily Print Name of Instance and Value... (self, text) --self(Instance), text(random String Value) I'll need to check it out thnx |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 02 Feb 2015 10:25 AM |
Of course you can do that.
It's the basis of all Lua classes, and it's all fine.
local class = {}; local function class:new(i) local nC = { msg = i or "NAnanananNANSANASJDNBDN" } return setmetatable(nC, {__index = self}); end
local function class:say(msg) print(msg or self.msg) end
local thing = class:new("Monkeys") thing:say() --> Monkeys |
|
|
| Report Abuse |
|
|
Fedorakid
|
  |
| Joined: 17 Jul 2010 |
| Total Posts: 7079 |
|
|
| 02 Feb 2015 10:30 AM |
@war
17:30:22.824 - Workspace.MethodTesting:19: attempt to call method 'print' (a nil value) 17:30:22.825 - Stack Begin 17:30:22.825 - Script 'Workspace.MethodTesting', Line 19 17:30:22.826 - Stack End |
|
|
| Report Abuse |
|
|
| |
|
|
| 02 Feb 2015 12:57 PM |
Lol sorry about that:
function Wrap(obj, props) local properties = {} for i,v in pairs(props) do properties[i] = v end
local wrapped = newproxy(true) getmetatable(wrapped).__index = function(t, k) if properties[k] then return properties[k] elseif not properties[k] then return obj[k] end end
getmetatable(wrapped).__newindex = function(t,k,v) if obj[k] then obj[k] = v else t[k] = v end end
getmetatable(wrapped).__metatable = "Attempt to modify protected metatable"
return wrapped end
local Part = Wrap(workspace.Part, {print = function() print(workspace.Part) end})
Part:print()
Fixed, and improved :D |
|
|
| Report Abuse |
|
|
|
| 02 Feb 2015 12:59 PM |
| lol still kinda complicated but I'll see if I can learn it off |
|
|
| Report Abuse |
|
|
|
| 02 Feb 2015 01:04 PM |
| Would you like me to comment on what things do? |
|
|
| Report Abuse |
|
|
| |
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 02 Feb 2015 01:15 PM |
I was going to ask what the requirement for a wrapper was, but then I realised I'd never read the question properly.
__index = function(t,k) return function(...) return t._Real[k](t, t._Real[k], ...) end end
Something like that might suffice for casual use. |
|
|
| Report Abuse |
|
|
|
| 02 Feb 2015 01:28 PM |
function Wrap(obj, props) --Define a function for wrapping local properties = {} --Create a properties table full of propprties for i,v in pairs(props) do --Cycle through the properties given properties[i] = v --Transfer the value over from their table to mine end --End the loop
--[[Why did I copy the table?
I copied the table because if I never, if anyone changed the props variable, they would change the custom instance! We don't want that! So, instead I transfered. Be warned however, if one of the properties is a table, they can still change that! (See table.copy of my ExtendedTable ModuleScript model.) --]]
local wrapped = newproxy(true) --Create a userdata.
--[[Why did I use newproxy?
I used newproxy so it would return a userdata, and we wouldn't have to worry about players messing with it using rawset/rawget. If they did this, they could mess things up. Not to mention roblox objects are userdatas, so this makes it more realistic. --]]
getmetatable(wrapped).__index = function(t, k) --If somebody tries to index (What I mean by index is like tab.hello (We just indexed hello of tab)) if properties[k] then --Check if a custom property exists, if it does return properties[k] --Return it elseif not properties[k] then --If it does not exist return obj[k] --Return the property of the actual object end --End the if statement end --End the index function
getmetatable(wrapped).__newindex = function(t,k,v) --If somebody tries to change/add a property this function will fire. if obj[k] then --If the object we're wrapping has the property obj[k] = v --Set the object's property else --If not t[k] = v --Set the custom property end --End the if statement end --End the newindex function
getmetatable(wrapped).__metatable = "Attempt to modify protected metatable" --Stop people from modifying the wrapped object's metatable
return wrapped --Return the wrapped object end --End the wrap function
local Part = Wrap(workspace.Part, {print = function() print(workspace.Part) end}) --Set Part to a wrapped version of workspace.Part, giving it the property print.
Part:print() --Use it's print property |
|
|
| Report Abuse |
|
|
| |
|
| |
|
|
| 03 Feb 2015 11:48 AM |
| I am confused as to this purpose of this. If so one would be so kind to shed some light my way, I will be grateful. |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 03 Feb 2015 11:55 AM |
http://www.roblox.com/Things-item?id=211146719
That would be the amazing purpose. |
|
|
| Report Abuse |
|
|