ethanwdp Posted September 1, 2015 Share Posted September 1, 2015 Now that an update fixed it, I've been wondering what it does. ps: sorry for the short post Link to comment Share on other sites More sharing options...
RoboMat Posted September 1, 2015 Share Posted September 1, 2015 Usually you can use it to load modules in Lua, but in earlier version of PZ it always returned nil.Quick and dirty example:local Module = {};function Module.sayHello() print('Hello World');endreturn Module;and then in a different file you do this:local Module = require("Module");Module.sayHello(); -- Greet the world.Check out my OOP tutorial in the Tutorials section to see a bit more elaborate use case. Link to comment Share on other sites More sharing options...
ethanwdp Posted September 1, 2015 Author Share Posted September 1, 2015 Usually you can use it to load modules in Lua, but in earlier version of PZ it always returned nil.Quick and dirty example:local Module = {};function Module.sayHello() print('Hello World');endreturn Module;and then in a different file you do this:local Module = require("Module");Module.sayHello(); -- Greet the world.Check out my OOP tutorial in the Tutorials section to see a bit more elaborate use case. If two mods use the same module, but the module in one of them is slightly different, do they still overlap? Because otherwise, wouldn't it be easier just to use global variables? Link to comment Share on other sites More sharing options...
RoboMat Posted September 1, 2015 Share Posted September 1, 2015 Well I guess you could say globals are easier to use. Generally people will tell you to avoid them and it is considered to be bad practice by many to use globals at all (although you shouldn't listen to that ... as always write something that works first and then worry about improving it). Personally I follow this, when I work on my own games - I like to keep things as separated as possible.Not sure if I understand the case you mention above, because you couldn't solve it easily with globals either. Basically wether you use the module thing I wrote above or use a global variable doesn't change much in terms of how you have to write your code.-- GlobalModule = {};function Module.sayHello() print('Hello World');endThe difference is that you have more control over the scopes in your program. For example:if true then local foo = 'visible'; print(foo);endprint(foo);The first print statement can "see" the foo variable as it is in the scope of the if block. The second print statement can't "see" it because the variable is discarded when the if statement ends.What does this mean in terms of modding? You can give your different modules, functions, and variables a much more confined and controlled space in which they live. So basically modules only need to know about the stuff that really concerns them.This also has some benefits for speed because usually accessing a local function / variable is faster than accessing the global scope.PZ is pretty extreme in that regard as all of their lua modules live in the global scope. I have no idea though if it would bring any (noticeable) performance improvements if they'd refactor their code to use locals. ethanwdp 1 Link to comment Share on other sites More sharing options...
Brybry Posted September 1, 2015 Share Posted September 1, 2015 Require is also very important for ensuring load order of scripts -- which is wasn't doing before for mods. For example, if script B.lua declares a global Foobar and script A.lua needs that global but script A loads before script B then script A can require script B to ensure that it has access to Foobar. Link to comment Share on other sites More sharing options...
RoboMat Posted September 1, 2015 Share Posted September 1, 2015 Require is also very important for ensuring load order of scripts -- which is wasn't doing before for mods. For example, if script B.lua declares a global Foobar and script A.lua needs that global but script A loads before script B then script A can require script B to ensure that it has access to Foobar. It didn't do that before? Or was that broken also? Ah ... I remember that bug was introduced a while ago I think? Have you tried if it returns the loaded module correctly yet, Brybry? Link to comment Share on other sites More sharing options...
Brybry Posted September 2, 2015 Share Posted September 2, 2015 Yeah, require() was just flat out broken for mods before, it only worked for base game scripts. Now it both correctly loads (so load order is correct) and returns the loaded module/value. I don't know if it's according to spec with regards to multiple/duplicate imports (aka A requires B and C and B requires C then it shouldn't load C twice) but either way it's leaps and bounds better now Link to comment Share on other sites More sharing options...
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