ShayF2 Posted February 15, 2019 Share Posted February 15, 2019 I need help figuring out how to convert a script files buffer to a table. Here's what I mean: local buffer = [[local sx, sy = 0, 0 globalVar = 'test string' function testFunction() end local function testFunction2() end]] -- to local buffer = {sx = 0, sy = 0, globalVar = 'test string', function testFunction() end, function testFunction2() end} Link to comment
Scripting Moderators thisdp Posted February 15, 2019 Scripting Moderators Share Posted February 15, 2019 (edited) local buffer = [[local sx, sy = 0, 0 globalVar = 'test string' function testFunction() end local function testFunction2() end]] buffer = buffer:gsub("local","") local fnc = loadstring(buffer) local gTable = {} setmetatable(gTable,{__index=_G}) setfenv(fnc,gTable) fnc() for k,v in pairs(gTable) do print(k,v) end Edited February 15, 2019 by thisdp Link to comment
ShayF2 Posted February 19, 2019 Author Share Posted February 19, 2019 (edited) @thisdp your solution is false You load the code, and dont unload it. I'm sorry but this solution will not work. However I do like that you made things global, but that is also false because after adding those functions to the table you need to change the name of those functions, gable.functionName() In the scripts string format before you load it I mean, you don't change their names, you just change the names where the functions are executed, if you know what I mean. Edited February 19, 2019 by ShayF Link to comment
Scripting Moderators thisdp Posted February 19, 2019 Scripting Moderators Share Posted February 19, 2019 (edited) No need to unload, because the variables won't affect the _G table, this means everything is "local" which will be marked by garbage collector after they are useless (If you know how lua garbage collector works you will understand what I have said). The code has passed my test. In terms of your request, the code works. For the "local" variables, there is no way to get their name unless they are "global". Edited February 19, 2019 by thisdp Link to comment
ShayF2 Posted February 19, 2019 Author Share Posted February 19, 2019 @thisdp Try your code again this time do this print(type(gTable.testFunction)) print(type(testFunction)) the reason i say this is because you dont unload the code, which means that the original functions and variables are still there even after you add them to the table. Not only do you have to unload the code but you need to reformat the string due to those functions and variables being inside the table so that events, commands, and timers can trigger them from gTable instead of the original code. If the second print test prints function then the solution is false. Link to comment
Scripting Moderators thisdp Posted February 19, 2019 Scripting Moderators Share Posted February 19, 2019 (edited) 3 hours ago, ShayF said: @thisdp Try your code again this time do this print(type(gTable.testFunction)) print(type(testFunction)) the reason i say this is because you dont unload the code, which means that the original functions and variables are still there even after you add them to the table. Not only do you have to unload the code but you need to reformat the string due to those functions and variables being inside the table so that events, commands, and timers can trigger them from gTable instead of the original code. If the second print test prints function then the solution is false. The first "print" prints "function" The second "print" prints "nil" I am afraid that you didn't test my code. If you think it is false without test, then it is false anyway. I don't care. Edited February 19, 2019 by thisdp Link to comment
ShayF2 Posted February 19, 2019 Author Share Posted February 19, 2019 @thisdp I hardly ever test code, even my own, I just make it n I already know what works n what doesn't. I honestly don't understand how you got the second print to be nil, because the string is loaded throughout the file and not unloaded but oh well, good job. Link to comment
Scripting Moderators thisdp Posted February 19, 2019 Scripting Moderators Share Posted February 19, 2019 (edited) 1 minute ago, ShayF said: @thisdp I hardly ever test code, even my own, I just make it n I already know what works n what doesn't. I honestly don't understand how you got the second print to be nil, because the string is loaded throughout the file and not unloaded but oh well, good job. because of "setfenv" And also, I recommend you to learn LUA weak table if you don't know. Edited February 19, 2019 by thisdp Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now