RoflBread
|
  |
| Joined: 18 Jun 2009 |
| Total Posts: 3803 |
|
| |
|
|
| 01 Nov 2014 08:28 PM |
What is this thread even
Anyways OP what you're looking for is the __index metamethod to access 'old' string functions as well:
local oldString = string local string = {} function string.bob() end
setmetatable(string, { __index = oldString })
Now, string.bob("Hello world") and string.sub("Hello world", 3) should both work.
If you want it in a module, check out this that I made with the same concept (actually might be better because I also expanded the string library): http://www.roblox.com/A-Better-string-Library-item?id=175473953
~Upload code to codepad-dot-org with Lua syntax highlighting to preserve indentation and make it easier to read!~ |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 01 Nov 2014 08:36 PM |
| That wasn't very Christian of you :3 |
|
|
| Report Abuse |
|
|
|
| 01 Nov 2014 08:39 PM |
He's giving us a bad name.
--ThatChristianGuy |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 01 Nov 2014 08:41 PM |
setmetatable(module, { __index = string })
I don't understand how that works so I think I'll still use my method Unless mine is very inefficient compared to that (Thank you though, I just don't like to use things I don't understand |
|
|
| Report Abuse |
|
|
|
| 01 Nov 2014 08:43 PM |
"That wasn't very Christian of you :3"
That's very ironic of you.
|
|
|
| Report Abuse |
|
|
|
| 01 Nov 2014 08:45 PM |
I'm giving Scripting Helpers a bad name.
My actions do not determine the actions of other Christians. Do not judge a whole community just because of one person.
|
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 01 Nov 2014 08:46 PM |
Apart from the fact that as an Atheist, I don't need to be christian.
Basically, it works by saying that anything not contained in your new string library should be looked up for in the old string library. |
|
|
| Report Abuse |
|
|
|
| 01 Nov 2014 08:48 PM |
Apart from the fact that no one is to go unpunished.
|
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
| |
|
|
| 01 Nov 2014 08:49 PM |
Let me explain then, because it is a better way.
So there's these things called "metatables", which are themselves tables. You can assign these metatables (which again are just tables) to be 'attached' to a normal table.
You assign the metatable to a table like this:
local normalTable = {} local metaTable = {} setmetatable(normalTable, metaTable)
When a metatable is attached to a table via setmetatable(), it can affect the tables (normalTable)'s behavior by using certain code-words as keys.
These code-wods all start with two underscores like this: __index (a list of them and descriptions can be found on the wiki)
The __index "metamethod" (that's what the codewords and their assigned values are called) has a nifty feature where you can make the value of it ANOTHER table. Then, if someone tries to access a nil value in 'normalTable' and we have metaTable = {__index = anotherTable} , then they'll actually get the index in 'anotherTable' instead!
So when I'm setting the metatable of our local 'string' table to have __index=oldString, that means that when someone does this:
string.sub
, the code is like "hey, string (a local table) doesn't have sub! Let's use oldString.sub instead"
Get it now?
~Upload code to codepad-dot-org with Lua syntax highlighting to preserve indentation and make it easier to read!~ |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 01 Nov 2014 08:50 PM |
the __index metatable method thingy is called when a value is looked up in the table. If a table is set to __index, then it means that when the key is not found in the original table, it checks the __index table. YOu can abuse these for OOP inheritance and tailcalls. |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 01 Nov 2014 08:51 PM |
Yes I think I get it now (Get what its doing, still don't clearly understand metatables very well)
Thanks |
|
|
| Report Abuse |
|
|