dugasz1 Posted August 13, 2017 Share Posted August 13, 2017 Hello! So i want to load external lua files in my resource and i do it with loadstring (Link). Like this: function loadLuaFile( path ) local file = fileOpen(path, true) if (not file) then outputChatBox("Failed") return else local string = "" local buffer while not fileIsEOF(file) do buffer = fileRead(file, 100) if (buffer) then string = string .. buffer end end loadstring(string)() end fileClose(file) end Loading a few script: loadLuaFile(":shared/oop.lua") loadLuaFile(":shared/test1.lua") loadLuaFile(":shared/test2.lua") And i found a wierd thing. It doesn't matter how many lua files i load in, the memory usage in performancebrowser doesn't change. Is it a bug? Or somehow its recognize this code was loaded in another resource? Link to comment
NeXuS™ Posted August 13, 2017 Share Posted August 13, 2017 I don't think it's a bug, as the performancebrowser works perfectly (or maybe works perfectly whenever I use it). Maybe it does recognized that it is already in memory and it doesn't need to be loaded again. Link to comment
NeXuS™ Posted August 14, 2017 Share Posted August 14, 2017 But let's wait for someone's answer. Link to comment
dugasz1 Posted August 16, 2017 Author Share Posted August 16, 2017 Yeah it might be an optimization. Link to comment
Hoffmann Posted August 16, 2017 Share Posted August 16, 2017 (edited) It works for me if I load script again and again, current Lua memory will change Spoiler http://imgur.com/a/XNVys . function loadScript(stringCode) local code = loadstring(stringCode) setfenv(0, newENV) local executeCode = pcall(code) if executeCode then outputChatBox("Code has been loaded.") end Edited August 16, 2017 by NeverUnbeatable Link to comment
dugasz1 Posted August 16, 2017 Author Share Posted August 16, 2017 I used the performancebrowser online interface. Maybe because of that. How can i open it ingame? I don't really get setfenv(0,newENV). Can you please explane it for me? Kindy get how Lua's environments work but what is this 0 means:"As a special case, when f is 0 setfenv changes the environment of the running thread. In this case, setfenv returns no values." Why is it good? What is it for? Link to comment
Hoffmann Posted August 16, 2017 Share Posted August 16, 2017 26 minutes ago, dugasz1 said: I used the performancebrowser online interface. Maybe because of that. How can i open it ingame? I don't really get setfenv(0,newENV). Can you please explane it for me? Kindy get how Lua's environments work but what is this 0 means:"As a special case, when f is 0 setfenv changes the environment of the running thread. In this case, setfenv returns no values." Why is it good? What is it for? I will try to explain how it works. Every function has it's own environment containing variables to which function has access. But as you're using loadstring it returns function that could be called by func() or pcall(func) and this function's environment would be set to global state(_G) by default. Because of that, if you're loading scripts from maps made by people, they will be able to call any function or trigger any event, finally, ruin your server. Function environment won't let do that, because you can manage what functions can script use. For instance, local code = "a = 6 outputChatBox(a)" local loadedCode = loadstring(code) -- returns function. loadedCode() will also work to call this code local newEnv = {outputChatBox = outputChatBox} -- we put function to use it in code setfenv(loadedCode, newEnv) -- now this code is safe and can't access _G local executedCode = pcall(loadedCode) if executedCode then outputDebugString("Code loaded!", 3) end About perfomancebrowser, run ipb resource and use /ipb to open ingame browser. Also useful posts by Arezu: Hope, I helped a bit. 1 Link to comment
dugasz1 Posted August 17, 2017 Author Share Posted August 17, 2017 Thank you (and Arezu ) it clear things up. So an envirement is kindy a scope. (I don't need to worry about the security the loaded script will be my scripts but at least i know more) 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