Global VS Local Require()

I’m wondering which of these ways is best:

Way #1
[lua]–main.lua

thisModule=require(“module1”)
thatModule=require(“module2”)

–Change scenes[/lua]

[lua]–scene1.lua
local thingy=thisModule.createSomething()
local something=thatModule.createSomethingElse()[/lua]

Way #2
[lua]–main.lua
–Change Scenes[/lua]

[lua]–scene1.lua
local thisModule=require(“module1”)
local thatModule=require(“module2”)

local thingy=thisModule.createSomething()
local something=thatModule.createSomethingElse()[/lua]

Is there an advantage to either way? I mean as in globally requiring things with main.lua (or something else) or locally requiring things inside each scene.

Is there a difference?

Caleb [import]uid: 147322 topic_id: 35110 reply_id: 335110[/import]

Common thought is local is always better than global. But if you load module1.lua in scene-a.lua, then local module1.lua in scene-b.lua both with “local”, you are accessing a singular copy of module1.lua. If scene-a makes changes to data in module1, then scene-b sees the changes.

You can think of the module as being globally available, you just have a local reference to it.

I would say if you’re doing things that would affect frame-rate, like things inside tight loops where you want to be as efficient as possible then by all means localize it. If you have a soundfx.lua file that loads in your common UI sounds that you play in multiple places but not very often, no reason to not use that globally, unless you’re a purist who is in the never use globals camp.

[import]uid: 199310 topic_id: 35110 reply_id: 139595[/import]

So Rob, is it safe to say that those two methods OP posted are the same? [import]uid: 206803 topic_id: 35110 reply_id: 139602[/import]

No, they are different, but they are not radically different. [import]uid: 199310 topic_id: 35110 reply_id: 139606[/import]

So, in essence, they really have about the same effect? [import]uid: 147322 topic_id: 35110 reply_id: 139625[/import]

It depends. If the module you are requiring is something like the math library, it can be a serious impact on performance to use global modules. The keyword is “can be”. For instance, if you’re running some complex math calculations in a tight loop, accessing global variables and modules in global space is more expensive. Imagine you’re doing a tower defense game and you have 200 enemies on the screen and each can have dozens of bullets going on. In OOP, you’re likely going to have a module for the enemy and a module for the bullets. In a 60 fps game that’s a lot of calculations in 1/60th of a second. Doing things like:

local random = math.random  

can save a lot of time. This is the “Yes always localize” rule.

You’re making a whack-a-mole game where a mole pops up every second, you generally can spare a few CPU cycles to look things up in global space.

But for younger, less experienced programmers its always best practice to use best practices. As you get more experience and understand what impacts performance, then you can get sloppy. Sadly for new programmers sloppier is easier.
[import]uid: 199310 topic_id: 35110 reply_id: 139656[/import]

Hmm… So I think I’m getting it some:

When you require something globally, all of the things inside of it also become global. So when you use a function from the globally required something-or-other, you’re using a global function.

When you require something locally, all of the things inside of it also become local. So when you use a function from the locally required something-or-other, you’re using a local function.

And local is faster than global.

So if all of that is true, does it have the same effect to do this:
[lua]–First do method #1 in main.lua
–Scene1.lua

local thisModule=thisModule
local thatModule=thatModule

…[/lua]

Caleb [import]uid: 147322 topic_id: 35110 reply_id: 139722[/import]

If right hand side “thisModule” is global, then it would in theory be faster. You see a lot of examples of people doing:

local random = math.random

Which localizes the random function. The math library lives in global space, so doing that will improve performance if you’re calling that function a lot. [import]uid: 199310 topic_id: 35110 reply_id: 139804[/import]

Common thought is local is always better than global. But if you load module1.lua in scene-a.lua, then local module1.lua in scene-b.lua both with “local”, you are accessing a singular copy of module1.lua. If scene-a makes changes to data in module1, then scene-b sees the changes.

You can think of the module as being globally available, you just have a local reference to it.

I would say if you’re doing things that would affect frame-rate, like things inside tight loops where you want to be as efficient as possible then by all means localize it. If you have a soundfx.lua file that loads in your common UI sounds that you play in multiple places but not very often, no reason to not use that globally, unless you’re a purist who is in the never use globals camp.

[import]uid: 199310 topic_id: 35110 reply_id: 139595[/import]

So Rob, is it safe to say that those two methods OP posted are the same? [import]uid: 206803 topic_id: 35110 reply_id: 139602[/import]

No, they are different, but they are not radically different. [import]uid: 199310 topic_id: 35110 reply_id: 139606[/import]

So, in essence, they really have about the same effect? [import]uid: 147322 topic_id: 35110 reply_id: 139625[/import]

It depends. If the module you are requiring is something like the math library, it can be a serious impact on performance to use global modules. The keyword is “can be”. For instance, if you’re running some complex math calculations in a tight loop, accessing global variables and modules in global space is more expensive. Imagine you’re doing a tower defense game and you have 200 enemies on the screen and each can have dozens of bullets going on. In OOP, you’re likely going to have a module for the enemy and a module for the bullets. In a 60 fps game that’s a lot of calculations in 1/60th of a second. Doing things like:

local random = math.random  

can save a lot of time. This is the “Yes always localize” rule.

You’re making a whack-a-mole game where a mole pops up every second, you generally can spare a few CPU cycles to look things up in global space.

But for younger, less experienced programmers its always best practice to use best practices. As you get more experience and understand what impacts performance, then you can get sloppy. Sadly for new programmers sloppier is easier.
[import]uid: 199310 topic_id: 35110 reply_id: 139656[/import]

Hmm… So I think I’m getting it some:

When you require something globally, all of the things inside of it also become global. So when you use a function from the globally required something-or-other, you’re using a global function.

When you require something locally, all of the things inside of it also become local. So when you use a function from the locally required something-or-other, you’re using a local function.

And local is faster than global.

So if all of that is true, does it have the same effect to do this:
[lua]–First do method #1 in main.lua
–Scene1.lua

local thisModule=thisModule
local thatModule=thatModule

…[/lua]

Caleb [import]uid: 147322 topic_id: 35110 reply_id: 139722[/import]

If right hand side “thisModule” is global, then it would in theory be faster. You see a lot of examples of people doing:

local random = math.random

Which localizes the random function. The math library lives in global space, so doing that will improve performance if you’re calling that function a lot. [import]uid: 199310 topic_id: 35110 reply_id: 139804[/import]