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: how to make a psuedo property?

Previous Thread :: Next Thread 
darkstriken is not online. darkstriken
Joined: 23 Oct 2010
Total Posts: 376
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 is not online. rayk999
Joined: 18 Feb 2011
Total Posts: 4705
05 Oct 2015 06:48 PM
wrapping


Red Blossoms
Report Abuse
darkstriken is not online. darkstriken
Joined: 23 Oct 2010
Total Posts: 376
05 Oct 2015 06:49 PM
Rayk, that isnt helpful.
Report Abuse
lostend is not online. lostend
Joined: 21 Aug 2011
Total Posts: 8265
05 Oct 2015 06:54 PM
var obj=new Object();
Report Abuse
EgoMoose is not online. 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
TickerOfTime is not online. TickerOfTime
Joined: 02 Sep 2012
Total Posts: 2030
05 Oct 2015 07:44 PM
"no code"
asd

You've got to use wrappers and the __index metatamethod.

-TickerOfTime1
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
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
darkstriken is not online. darkstriken
Joined: 23 Oct 2010
Total Posts: 376
06 Oct 2015 06:18 AM
omg i think i get it
ill reply with my code after school
Report Abuse
eLunate is not online. 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
darkstriken is not online. darkstriken
Joined: 23 Oct 2010
Total Posts: 376
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
TickerOfTime is not online. TickerOfTime
Joined: 02 Sep 2012
Total Posts: 2030
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
darkstriken is not online. darkstriken
Joined: 23 Oct 2010
Total Posts: 376
06 Oct 2015 03:11 PM
:(
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
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
darkstriken is not online. darkstriken
Joined: 23 Oct 2010
Total Posts: 376
06 Oct 2015 03:26 PM
thanks
Report Abuse
MiniNob is not online. 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
darkstriken is not online. darkstriken
Joined: 23 Oct 2010
Total Posts: 376
06 Oct 2015 03:30 PM
omg mininob got it perfectc
Report Abuse
darkstriken is not online. darkstriken
Joined: 23 Oct 2010
Total Posts: 376
06 Oct 2015 03:31 PM
does it work tho?
Report Abuse
MiniNob is not online. 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
earthyreuben is not online. earthyreuben
Joined: 17 Jan 2010
Total Posts: 669
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 is not online. 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
earthyreuben is not online. earthyreuben
Joined: 17 Jan 2010
Total Posts: 669
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 is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
07 Oct 2015 01:09 PM
What praise.
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
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 is not online. MiniNob
Joined: 14 May 2013
Total Posts: 822
07 Oct 2015 01:57 PM
0.0
Report Abuse
eLunate is not online. 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
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