Question regarding "require".

Hi,

I was about to find a way to have a singleton object accessible across all my classes, which each is in it’s on lua file, that I stumbled upon this article:

http://www.coronalabs.com/blog/2012/08/07/managing-state-between-scenes/

And it was fine and all but this sentenced puzzled me:

“Since all scenes must require( “storyboard” ), then they all share a common object.” What does it mean?

Does this mean that “require” is making a singleton out of a lua file? So if I load several audio files in c’tor of a lua class and “require” it several times, will it just load those audio files just once?

Thanks.

I think there might be optimisation in the audio system to avoid loading an actual audio file more than once, but I’m really not sure - check the docs properly.

By “c’tor” I’m guessing you mean ‘constructor’?

‘require’ loads a code library but once it is loaded it will not be loaded again, unless it is unloaded. You can call the functions within multiple times, but it won’t take up more space in memory.

What developers tend to do is “local storyboard = require(“storyboard”)” and so every .lua file needs to include that line. If the main file contains “storyboard = require(“storyboard”)” you do not need to have it in each .lua because it is globally loaded.

That’s how I do it and it works just fine. There may be subtleties that I’m missing and may well be good reasons for not doing that, but it works for me.

Thanks horacebury,

So if I load a module with “require” keyword at main, instantiate an audio manager and put it’s reference into the storyboard instance (?) then if I require that module in another lua file, I can access reference to my audio manager class, right?

I’m also interested to hear other people’s input on this as well.

No, require’ing a module does not cause code to be shared, it only loads the code in that module - similar to loading a DLL in Windows. It becomes available to use, that’s all.

I’ve not read the audio in storyboard tutorial, but I would think that you’d need to keep a reference to the loaded audio and pass it in scene parameters for another scene to “know” about the audio. That’s how it works with all other data.

The only exception is if you use a truly global variable, which I don’t advise (though, of course, I do it a lot, tbh.)

Code isn’t shared, but opening a portal works perfectly fine.

ie: if I set storyboard.clue = 99, any other lua file that requires in storyboard should also have that value. (required in files aren’t instantiated twice)

This approach is specifically used by CL’s recommended method for global variables (using a variable store lua file instead of _G). The first time it’s required, the variables are created. Any other time just calls the reference.

I think there might be optimisation in the audio system to avoid loading an actual audio file more than once, but I’m really not sure - check the docs properly.

By “c’tor” I’m guessing you mean ‘constructor’?

‘require’ loads a code library but once it is loaded it will not be loaded again, unless it is unloaded. You can call the functions within multiple times, but it won’t take up more space in memory.

What developers tend to do is “local storyboard = require(“storyboard”)” and so every .lua file needs to include that line. If the main file contains “storyboard = require(“storyboard”)” you do not need to have it in each .lua because it is globally loaded.

That’s how I do it and it works just fine. There may be subtleties that I’m missing and may well be good reasons for not doing that, but it works for me.

Thanks horacebury,

So if I load a module with “require” keyword at main, instantiate an audio manager and put it’s reference into the storyboard instance (?) then if I require that module in another lua file, I can access reference to my audio manager class, right?

I’m also interested to hear other people’s input on this as well.

No, require’ing a module does not cause code to be shared, it only loads the code in that module - similar to loading a DLL in Windows. It becomes available to use, that’s all.

I’ve not read the audio in storyboard tutorial, but I would think that you’d need to keep a reference to the loaded audio and pass it in scene parameters for another scene to “know” about the audio. That’s how it works with all other data.

The only exception is if you use a truly global variable, which I don’t advise (though, of course, I do it a lot, tbh.)

Code isn’t shared, but opening a portal works perfectly fine.

ie: if I set storyboard.clue = 99, any other lua file that requires in storyboard should also have that value. (required in files aren’t instantiated twice)

This approach is specifically used by CL’s recommended method for global variables (using a variable store lua file instead of _G). The first time it’s required, the variables are created. Any other time just calls the reference.