generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Metatables Thing:Stuff()

Previous Thread :: Next Thread 
Kapitanovas is not online. Kapitanovas
Joined: 09 Sep 2012
Total Posts: 4216
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
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
02 Feb 2015 05:14 AM
Give me an example code of how you would use it, because I didn't understand.
Report Abuse
Kapitanovas is not online. Kapitanovas
Joined: 09 Sep 2012
Total Posts: 4216
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
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
02 Feb 2015 05:56 AM
I don't know. I am pretty sure you can, but I don't know.
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
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
Kapitanovas is not online. Kapitanovas
Joined: 09 Sep 2012
Total Posts: 4216
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 is not online. 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 is not online. 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
Kapitanovas is not online. Kapitanovas
Joined: 09 Sep 2012
Total Posts: 4216
02 Feb 2015 10:41 AM
hmmm lol complicating
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
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
Kapitanovas is not online. Kapitanovas
Joined: 09 Sep 2012
Total Posts: 4216
02 Feb 2015 12:59 PM
lol still kinda complicated but I'll see if I can learn it off
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
02 Feb 2015 01:04 PM
Would you like me to comment on what things do?
Report Abuse
Kapitanovas is not online. Kapitanovas
Joined: 09 Sep 2012
Total Posts: 4216
02 Feb 2015 01:07 PM
that'd be nice :D
Report Abuse
eLunate is not online. 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
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
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
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
03 Feb 2015 06:55 AM
Did the commenting help?
Report Abuse
Kapitanovas is not online. Kapitanovas
Joined: 09 Sep 2012
Total Posts: 4216
03 Feb 2015 10:51 AM
yes, thnx
Report Abuse
earthyreuben is not online. earthyreuben
Joined: 17 Jan 2010
Total Posts: 669
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 is not online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image