jCode
|
  |
| Joined: 12 Feb 2012 |
| Total Posts: 33 |
|
|
| 14 Feb 2012 04:28 PM |
If I wanted to make a method that acts "in" an object, would that be possible? An example would be a pseudo getPlayersInTeam method where if used on a Team object would return a table full of player names whose TeamColor is the same as that team.
{ Proficient in, but not limited to - RBX.Lua } |
|
|
| Report Abuse |
|
|
|
| 14 Feb 2012 04:50 PM |
I'm pretty sure it is possible, using some metatable magic. I'm unable to help with this, but I'm curious, what would be the use? Why not just make a function that does it, like this? function getPlayersInTeam(team) local t = {} for _, v in pairs(game.Players:GetChildren()) do if v.TeamColor == team.TeamColor then t[_] = v end end return t end
|
|
|
| Report Abuse |
|
|
|
| 14 Feb 2012 04:52 PM |
| Methods are cooler because you don't have to have a function argument. |
|
|
| Report Abuse |
|
|
|
| 14 Feb 2012 04:53 PM |
| lol, but i don't see why you would go through the trouble when there's an easy solution |
|
|
| Report Abuse |
|
|
jCode
|
  |
| Joined: 12 Feb 2012 |
| Total Posts: 33 |
|
|
| 14 Feb 2012 04:56 PM |
I want to create an easy to use function library in which one can simply call a method instead of calling a function.
Here's my question for you - why is there a GetPlayerFromCharacter method if it can be achieved otherwise?
{ Proficient in, but not limited to - RBX.Lua } |
|
|
| Report Abuse |
|
|
|
| 14 Feb 2012 04:58 PM |
| I see your point, however it should be noted that that is ran C-side, allowing it to be much faster. |
|
|
| Report Abuse |
|
|
|
| 14 Feb 2012 05:12 PM |
http://www.roblox.com/Forum/ShowPost.aspx?PostID=50134580
Maybe that? |
|
|
| Report Abuse |
|
|
|
| 14 Feb 2012 06:16 PM |
Ew. Old thread is old. I've a new script.
By the way, these 'methods' I have, they are only usable in ONE script. And, to get the physical object itself, you have to CALL the returned value (for example, p() to get the part). Other than that, it will work exactly the same.
function SetCustomMethods(object) local myMethods = { GetAngleFromPoint = function(anObject, Point, Radians) if (anObject.IsA and anObject:IsA("BasePart")) then local v1 = Point - anObject.Position local v2 = Vector3.new(0,0,-1) local angle = math.acos(v1:Dot(v2)/v1.magnitude) if Radians then return angle else return math.deg(angle) end end end, } --end myMethods local newObject = newproxy(true) local mt = getmetatable(newObject) mt.__index = function(Table, Key) if myMethods[Key] then return function(a, ...) if a~=nil then return myMethods[Key](object, ...) else return myMethods[Key] end end else return function(a, ...) if a~=nil then return object[Key](object, ...) else return object[Key] end end end end mt.__newindex = function(Table, Key, Value) pcall(function() object[Key] = Value end) end mt.__metatable = {} mt.__call = function() return object end return newObject end
-- Example local part = SetCustomMethods(Instance.new("Part")) part.Parent = workspace part.Anchored = true part.BrickColor = BrickColor.new("Really red") part.Position = Vector3.new(1,0,0) --Use a point with a 90 degree angle to (0,0,0)
local angleRad = part:GetAngleFromPoint(Vector3.new(0,0,0), true) local angleDeg = part:GetAngleFromPoint(Vector3.new(0,0,0))
print( angleRad, angleDeg ) --> 1.57... 90
I can add a SetMethod function if you'd like, that way you can dynamically set a method to an object instead of having to define it within the function itself. |
|
|
| Report Abuse |
|
|
| |
|
|
| 14 Feb 2012 06:59 PM |
Pseudo: The worst word that was ever created. It makes me want to throw up. |
|
|
| Report Abuse |
|
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
|
| 14 Feb 2012 07:01 PM |
http://www.roblox.com/Improved-Instance-item?id=72385126
It has no documentation yet and is still under development but its pretty epik. Allows for pseudo properties of objects (includes methods) and has a nice little error handling system with a few other perks :) I'll release the actual thing when I finish but it works well so far |
|
|
| Report Abuse |
|
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
|
| 14 Feb 2012 07:02 PM |
"Pseudo: The worst word that was ever created. It makes me want to throw up."
Hater |
|
|
| Report Abuse |
|
|
|
| 14 Feb 2012 07:05 PM |
@CM32
Animation. The best word that was ever created. It makes me want to throw up, they are so epic. |
|
|
| Report Abuse |
|
|
| |
|
jCode
|
  |
| Joined: 12 Feb 2012 |
| Total Posts: 33 |
|
|
| 14 Feb 2012 08:02 PM |
@AFF
I just need to know how the infrastructure of ROBLOX objects, events, methods etcetera is set up so I can make my own stuff (objects, events, methods, etcetera).
{ Proficient in, but not limited to - RBX.Lua } |
|
|
| Report Abuse |
|
|
22436106
|
  |
| Joined: 19 Dec 2011 |
| Total Posts: 90 |
|
|
| 14 Feb 2012 08:03 PM |
@agent what kind of animation? :p
also use metatables to make custom method functions |
|
|
| Report Abuse |
|
|
jCode
|
  |
| Joined: 12 Feb 2012 |
| Total Posts: 33 |
|
|
| 14 Feb 2012 08:05 PM |
@22436106
I know it involves tables and metatables, I just need to know how ROBLOX itself sets up these things using them so I can base mine off of that.
{ Proficient in, but not limited to - RBX.Lua } |
|
|
| Report Abuse |
|
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
|
| 14 Feb 2012 08:05 PM |
| I think I'll stop posting models of code for people to look at. . . |
|
|
| Report Abuse |
|
|
jCode
|
  |
| Joined: 12 Feb 2012 |
| Total Posts: 33 |
|
|
| 14 Feb 2012 08:08 PM |
@smurf
I'm not going to take a model and open it up in ROBLOX Studio just to view the code.
{ Proficient in, but not limited to - RBX.Lua } |
|
|
| Report Abuse |
|
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
|
| 14 Feb 2012 08:09 PM |
| Which is why I'm going to stop posting models. . . |
|
|
| Report Abuse |
|
|
22436106
|
  |
| Joined: 19 Dec 2011 |
| Total Posts: 90 |
|
|
| 14 Feb 2012 08:09 PM |
obj =workspace Part = newproxy(true) gm = getmetatable
gm(Part).__tostring = function() return obj.Name end
gm(Part).__newindex = function(self,elem,value) obj[elem] = val end
gm(Part).__index=function(self,elem) -- if obj:IsA("Lightning") and elem=="GetPlayersFromDerp" then return func end return obj[elem] end |
|
|
| Report Abuse |
|
|
jCode
|
  |
| Joined: 12 Feb 2012 |
| Total Posts: 33 |
|
|
| 14 Feb 2012 08:11 PM |
@22436106
You just confused me. Are properties stored as different metatables inside of the main table, which is the object itself?
What is newproxy?
{ Proficient in, but not limited to - RBX.Lua } |
|
|
| Report Abuse |
|
|
|
| 14 Feb 2012 08:20 PM |
| Oh you guys and your fancy metatable magic. I've never learned that stuff yet. And I refuse to until I find a situation for me where I need it :P |
|
|
| Report Abuse |
|
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
|
| 14 Feb 2012 08:21 PM |
| ^But metatables allow for kool OOP stoof |
|
|
| Report Abuse |
|
|
jCode
|
  |
| Joined: 12 Feb 2012 |
| Total Posts: 33 |
|
|
| 14 Feb 2012 08:22 PM |
@crazyman
inb4pseudoevents
{ Proficient in, but not limited to - RBX.Lua } |
|
|
| Report Abuse |
|
|