Sign In Register

How can we help you today?

Start a new topic
Answered

Modules

Most of the forum post about modules are from atleast 2 years ago. Do modules still function as the existing posts suggest?

I am assuming that after the require, everything inside the module should be immediately available inline


I am simply trying to do:


require("testModule");

var x = testFunction();


inside the module named testModule is a function


function testFunction() {

    return 1;

}


I keep getting the reference error testFunction is not defined



Best Answer

Hi Jaayden,


You can't call a function inside a module from the main script because by the time the module finished processing it doesn't exist anymore. When the require function is called the module runs its script and will return any variables made/changed during its run. The module isn't a header/dependency included into the script but rather a piece of code agnostic to the main script.


Hope this sheds more light on the matter,

Omar


Could I add for example three.js or another physics engine to GameSparks as a module?

 

To be clear, the module shares the same data with the main script. So global variables made in the main script are identifiable by the module:


main script:

var example = "String"


module:


var foo = example;


Foo will have the same value as example because the main script and module share the data together. Think of it as a continuation. 



Answer

Hi Jaayden,


You can't call a function inside a module from the main script because by the time the module finished processing it doesn't exist anymore. When the require function is called the module runs its script and will return any variables made/changed during its run. The module isn't a header/dependency included into the script but rather a piece of code agnostic to the main script.


Hope this sheds more light on the matter,

Omar

Login to post a comment