|
| 05 Oct 2015 06:48 PM |
To where, for example, I could go part.ColorSet('blue') i know to use metatables, but im unsure how. Please dont just code it for me, actually explain the concept. (provide NO code) |
|
|
| Report Abuse |
|
|
rayk999
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 4705 |
|
| |
|
| |
|
lostend
|
  |
| Joined: 21 Aug 2011 |
| Total Posts: 8265 |
|
| |
|
EgoMoose
|
  |
| Joined: 04 Feb 2009 |
| Total Posts: 2896 |
|
|
| 05 Oct 2015 07:20 PM |
Really tough to give a response on this one with no code, but rayk is right. Wrapping is the way to go!
You'll need 2 tables and an object, obviously ;)
1. The first table, let's call it "table_a", will be the table that holds all the properties. 2. The second table, let's call it "table_b", will be "maestro" of the wrapped object. In other words, what you will interact with and what will do all the meta-table magic.
Once you have these two tables the basic idea is to have table_b update either table_a or the object's properties when the newindex metamethod is called, but never itself. In similar fashion table_b will also return either the dictionary value in table_a or the object's property when indexed.
A simple wrap is easy. What becomes difficult is when you want to make things seamless.
|
|
|
| Report Abuse |
|
|
|
| 05 Oct 2015 07:44 PM |
"no code" asd
You've got to use wrappers and the __index metatamethod.
-TickerOfTime1 |
|
|
| Report Abuse |
|
|
|
| 05 Oct 2015 07:57 PM |
| You're going to need a table, a userdata, and an object. Use the table to hold these pseudo properties. Use the userdata to handle indexing. When you fail to find something in the table, index the actual object. |
|
|
| Report Abuse |
|
|
|
| 06 Oct 2015 06:18 AM |
omg i think i get it ill reply with my code after school |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 06 Oct 2015 06:26 AM |
| If you're trying to add to Instances you do need a wrapper. Take a look at how my BaseLib and Libraries work in Valkyrie for an example. |
|
|
| Report Abuse |
|
|
|
| 06 Oct 2015 02:50 PM |
local CustomProps = {SetColor=function()end} local Part=workspace.Part setmetatable(Part,{__index=function()return CustomProps end})
is that right? (assuming i finished my custom propertys) |
|
|
| Report Abuse |
|
|
|
| 06 Oct 2015 03:05 PM |
Ah, not really. A part is not a table, therefor cannot have a metatable. Hint, you need a userdata that comes with a metatable. |
|
|
| Report Abuse |
|
|
| |
|
|
| 06 Oct 2015 03:24 PM |
local CustomProps = {SetColor=function()end} local Part=workspace.Part setmetatable(Part,{__index=function()return CustomProps end})
How about something more like
local RPart = workspace.Part local CustomProps = setmetatable({SetColor=function()end},{__index = RPart}) local Part = newproxy(true) getmetatable(Part).__index=function(t,k)return CustomProps[k] end
Of course that's poorly designed, I made it to look as much like your original as I could.
Wrappers get fairly complicated, simple ones (like the one above) provide little functionality. |
|
|
| Report Abuse |
|
|
| |
|
MiniNob
|
  |
| Joined: 14 May 2013 |
| Total Posts: 822 |
|
|
| 06 Oct 2015 03:29 PM |
local part=workspace.Part local properties={ Color=function(newpart,color) part.BrickColor=color end }
local newpart=newproxy(true) local mt=getmetatable(newpart) mt.__index=function(self,index) return properties[index] or part[index] end
newpart:Color(BrickColor.Red()) print(newpart.Name) |
|
|
| Report Abuse |
|
|
|
| 06 Oct 2015 03:30 PM |
| omg mininob got it perfectc |
|
|
| Report Abuse |
|
|
| |
|
MiniNob
|
  |
| Joined: 14 May 2013 |
| Total Posts: 822 |
|
|
| 06 Oct 2015 03:31 PM |
| make a new part in studio and paste in the command bar |
|
|
| Report Abuse |
|
|
|
| 06 Oct 2015 06:45 PM |
I would make a pseudo class:
Block = { new = function(name, parent) local copy = {} copy.part = Instance.new("Part") copy.part.Name = name copy.part.Parent = parent local metaMethods = { __index = Block.methods, __metatable = false } setmetatable(copy, metaMethods) return copy end, methods = { setColor = function(this, ...) color = {...} if(type(color[1]) == "string") then this.part.BrickColor = BrickColor.new(color[1]) else this.part.BrickColor = BrickColor.new((color[1] or 0)/255,(color[2] or 0)/255,(color[3] or 0)/255) end end } }
You would create you custom class like so:
local part = Block.new("Test", game.Workspace)
And to call the method you would:
part:setColor("Really blue")
or
part:setColor(0,0,255)
You can add class specific methods in methods and all class will share them. And you can add more metamethods in the creation.
If your wondering what __metatable is for, it is just to protect the table. Wouldn't make sense if it could be changed, otherwise it wouldn't be the same class anymore (though you don't need it). |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 07 Oct 2015 10:35 AM |
Ahem
Your class is stupid, and all of the wrapping attempts break methods. |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2015 11:48 AM |
@eLunate You never did like my idea from the get-go. What's with this resounding, positive praise now? |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
| |
|
|
| 07 Oct 2015 01:10 PM |
"What praise."
That should've been a question mark. And I believe he was being sarcastic. |
|
|
| Report Abuse |
|
|
MiniNob
|
  |
| Joined: 14 May 2013 |
| Total Posts: 822 |
|
| |
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 07 Oct 2015 03:58 PM |
| I know what it should'be been, and I put it as a full stop on purpose. |
|
|
| Report Abuse |
|
|