|
| 05 Feb 2014 06:17 PM |
This is kind of hard to explain so.
Inside the connect method How could I access the variable Bob that indirectly called the method?
------------------------------------------------------------------------- local Player = { Events = { Spawned = { connect = function (Func) --Player.CharacterAdded:connect(Func) end ; }; Died = {}; }; }
setmetatable(Player,{ __index = function(s,k) if s.Events[k] then return s.Events[k] end end })
setmetatable(Player.Spawned,{ })
function Player:New(Playery) local Object = {} setmetatable(Object,{ __index = function(s,k) return Player[k] or error("Cannot invoke "..k.." on this object") end }) Object.Player = Playery return Object end
local Bob = Player:New("I am the player object") Bob.Spawned:connect(function() print("Player has spawned") end) |
|
|
| Report Abuse |
|
|
| |
|
| |
|
| |
|
| |
|
|
| 11 Apr 2014 07:04 PM |
| Where exactly do you want to use 'Bob' in your script? |
|
|
| Report Abuse |
|
|
|
| 11 Apr 2014 07:08 PM |
Inside this function
connect = function (Func) --Player.CharacterAdded:connect(Func) end
I'm just trying to create a custom player object with custom events.....I guess i could just leave out the connect method making this a lot simpler... |
|
|
| Report Abuse |
|
|
|
| 11 Apr 2014 07:17 PM |
This is Roblox, metatables aren't extremely important to be honest.
Can you put your script on pastebin and Lua format it? I can't look at a long script when it isn't formatted correctly :P |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 11 Apr 2014 07:18 PM |
| Why would you want to return "Bob" if you already have it though? |
|
|
| Report Abuse |
|
|
|
| 11 Apr 2014 07:19 PM |
I know they are not really that useful(Here). I just want to mess around with them
pastebin.com/HPMPSnz2 |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 11 Apr 2014 07:23 PM |
I see future errors but none that are always going to occur (as far as I can see)
Fix 1: 'connect = function (x, Func)' Fix 2: 'return rawget(Player, k) or error("Cannot invoke "..k.." on this object")' |
|
|
| Report Abuse |
|
|
|
| 11 Apr 2014 07:23 PM |
| I don't have "bob" inside of the connect function.... |
|
|
| Report Abuse |
|
|
| |
|
|
| 11 Apr 2014 07:28 PM |
| Using rawget actually breaks the current script............ |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 11 Apr 2014 07:31 PM |
It should work fine, since your way is going to cause a stack overflow if the key doesn't exist. And same with 'if s.Events[k] then' |
|
|
| Report Abuse |
|
|
|
| 11 Apr 2014 07:31 PM |
Yes i understand that but...
18:28:22.349 - local Player = { Events = { Spawned = { connect = fun:28: Cannot invoke Spawned on this object 18:28:22.351 - Script 'local Player = { Events = { Spawned = { connect = fun', Line 28 - index Spawned 18:28:22.352 - Script 'local Player = { Events = { Spawned = { connect = fun', Line 36 18:28:22.353 - stack end
What? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 11 Apr 2014 07:34 PM |
Oh pssht, no wonder. For a second I thought it was setting the metatable to Player, not Object. You don't need that part but you should fix the second thing I mentioned (So: setmetatable(Player,{ __index = function(s,k) return rawget(s.Events, k) end })) |
|
|
| Report Abuse |
|
|
|
| 11 Apr 2014 07:34 PM |
I'd reformat the script to put everything in your new method, that way your 'Player' table has access to your Object table.
Also, the way your current format works, it completely defeats the purpose of a metatable on Object, even as a wrapper.
Basically, you want to make a single metatable if possible, and have any tables that would be duplicates (as in, unique to each player) to be contained inside the new method.
Would you like to see what I mean? |
|
|
| Report Abuse |
|
|
|
| 11 Apr 2014 07:39 PM |
| I mostly understand what you mean...I would like to see what you mean though. |
|
|
| Report Abuse |
|
|
|
| 11 Apr 2014 07:42 PM |
| I will address the security issues with your script as well. Give me a bit of time to type up the thing; I haven't worked with metatables for a few weeks. |
|
|
| Report Abuse |
|
|
|
| 11 Apr 2014 07:47 PM |
I guess I could just do something like this that does not use metatables...
local Player = { New = function (_,Player) local Object = { Spawned = { Connect = function(Func) Player.CharacterAdded:connect(Func) end } } return Object end }
local Player = Player:New(Game.Players.Bob) Player.Spawned:Connect(function() ---LALALALA end) |
|
|
| Report Abuse |
|
|
|
| 11 Apr 2014 08:10 PM |
| http://pastebin.com/drddDjQw |
|
|
| Report Abuse |
|
|
|
| 11 Apr 2014 08:12 PM |
You can ignore the Connection and Callbacks tables. They aren't used. I was going to implement custom events, but I realized you don't need them for what you wanted to do, and I forgot to remove those two lines from the script.
I hope you can understand what I did. :P |
|
|
| Report Abuse |
|
|
|
| 11 Apr 2014 08:22 PM |
| I understand most of it...Some of it took me a while to understand. But thanks. I never really thought of doing stuff this way. |
|
|
| Report Abuse |
|
|