128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 01 Nov 2014 06:20 PM |
So if I wanna make a function in the 'string' table
function string.bob() end -->error, Attempt to modify a readonly table
I get an error
If I do this
local string = {} function string.bob() end -->no error
print(string.sub("ok bye", 1, 2)) -->error, attempt to call field 'sub' (a nil value) --Because I made string a blank table
If I do this local string = (function() local tab = {} for x, f in pairs (string) do tab[x] = f end return tab end)() function string.bob() print("lol") end -->no error
string.bob() -->lol print(string.sub("ok bye", 1, 2)) -->ok
It completely works, but is there a better way?
I tried local string = {unpack(string)} But it didn't work because string is a dictionary table |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 01 Nov 2014 06:24 PM |
Try using rawset. Personally, I have a helper function for creating readonly tables (I'd just sent a message to Jarod about it, because I'm helping him with OOP) and it works like this
function readonlytable(table) return setmetatable({}, { __index = table, __newindex = function(table, key, value) error("Attempt to modify read-only table") end, __metatable = false }); end
Which makes a table that can only be set to with internal methods or rawset (Which bypasses the metatable) This of course assumes that string was implemented in pure Lua hehe |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 01 Nov 2014 06:28 PM |
For the purpose of a module script, forgot to add Idk if it matters
It would be like
local string = require(blah) |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 01 Nov 2014 06:35 PM |
| Can't you cheat by using require(module)() to override the scope's string table? |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 01 Nov 2014 08:06 PM |
| I'm not sure what you mean (Because when I tried that it didn't work) |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 01 Nov 2014 08:07 PM |
| If you're not quite sure what I mean then honestly don't worry about it ^^ |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 01 Nov 2014 08:10 PM |
I did this
workspace.module says
local module = {} function module.bob() print("bob") end return module
In the script
require(workspace.module()) module.bob() -->error, attempt to call a table value |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 01 Nov 2014 08:12 PM |
require(workspace.module()) That's a new one to me... |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 01 Nov 2014 08:14 PM |
That was a typo sorry I did require(workspace.module)()
Anyway I guess the way I did it isn't that bad anyway right? |
|
|
| Report Abuse |
|
|
RoflBread
|
  |
| Joined: 18 Jun 2009 |
| Total Posts: 3803 |
|
|
| 01 Nov 2014 08:16 PM |
Is it not
xxx = require(workspace.module)
xxx.bob() |
|
|
| Report Abuse |
|
|
RoflBread
|
  |
| Joined: 18 Jun 2009 |
| Total Posts: 3803 |
|
| |
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 01 Nov 2014 08:17 PM |
Nah, it's not bad. I'd just have used rawset to import it though hehe The deal is, when you go require(module)(), you're executing the function in a bit of a weird space. It inherits the function environment from the module's script, but it's within the environment of the scope that executed it, and with that you can (ab)use some things hehe |
|
|
| Report Abuse |
|
|
|
| 01 Nov 2014 08:17 PM |
| replace local strings with manipulated module values |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 01 Nov 2014 08:17 PM |
Yes it is but eLunate said 'Can't you cheat by using require(module)() to override the scope's string table?'
And they are right, there is something like that I've seen before Anyway this is getting offtopic
Is there a more efficient or shorter (Or more efficient and slightly longer) way to do this
local string = (function() local tab = {} for x, f in pairs (string) do tab[x] = f end return tab end)()
|
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 01 Nov 2014 08:19 PM |
| Have you tried rawset, like I said? |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
| |
|
| |
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 01 Nov 2014 08:22 PM |
http://www.lua.org/pil/14.2.html
Will also give you a nice insight into changing the behaviour of the _G table |
|
|
| Report Abuse |
|
|
| |
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
| |
|
|
| 01 Nov 2014 08:24 PM |
| wow noob so rude be nice wow |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 01 Nov 2014 08:25 PM |
| This is the nicest you'll ever get to know me. Get used to it. |
|
|
| Report Abuse |
|
|
|
| 01 Nov 2014 08:26 PM |
| WHAT THE FUCK IS WRONG WITH YOU!?!?! |
|
|
| Report Abuse |
|
|
RoflBread
|
  |
| Joined: 18 Jun 2009 |
| Total Posts: 3803 |
|
|
| 01 Nov 2014 08:27 PM |
| F bomb'd. Roblox has you now. Cya nerd. |
|
|
| Report Abuse |
|
|
|
| 01 Nov 2014 08:28 PM |
| DUMB PRICK GET THE FUCK OUT!! |
|
|
| Report Abuse |
|
|