Sign In Register

How can we help you today?

Start a new topic
Answered

Modules

First of all I want to give my appreciation to what you've done. Having written a backend in node.js myself, I can definitely appreciate gamesparks! This truly is awesome! On the bad side, the documentation lacks. A lot of what I understand I had to get through with help of deduction. I found a tab in cloud code called "modules", which I "feel" can be very helpful to organizing code (similarly to what I used to do in node.js). But I can't find any documentation, I don't really know where to start and how to use. I would assume that it should be different from node.js modules, but I wish there was a documentation there. 


I also can't find how can I execute functions during creation of a new player (registration process), I need to get info for the newly registered player and update world data and then send it back. 


Best Answer

Hi Dennis


Thanks for the feedback!


We are just in the final stages of our new documentation site, we plan to release this on June 1. This has been driven by customer feedback and we feel the new dos site will go a long way to address what can be done with the platform.


Here is the coy from the new docs site about modules:


Modules allow you to create your own libraries of JavaScript that can be included within other scripts.


This allows you to separate common functionality that needs to be shared between scripts into a single module that can be included.


To include a module within your script use the global require function as follow :

 

 

require("MODULE_SHORT_CODE");

 

 

Using require, you can load a module into the current execution context. This is not common.js require, but a more basic inclusion pattern, where the loaded module is inlined within the current script.


If you have circular reference between modules, the require method will only load a single module once per require invocation. This is to protect from infinite loops and stack overflows.


You can conditionally require modules based on input parameters etc to have a single script that can perform multiple tasks without having a large script

 

if(something) {
    require("MODULE_SHORT_CODE_1");
} else {
    require("MODULE_SHORT_CODE_2");
}

 

 As for getting scripts to execute on player registration, you can bind your script to either RegistrationRequest or RegistrationResponse. 


Gabriel



Answer

Hi Dennis


Thanks for the feedback!


We are just in the final stages of our new documentation site, we plan to release this on June 1. This has been driven by customer feedback and we feel the new dos site will go a long way to address what can be done with the platform.


Here is the coy from the new docs site about modules:


Modules allow you to create your own libraries of JavaScript that can be included within other scripts.


This allows you to separate common functionality that needs to be shared between scripts into a single module that can be included.


To include a module within your script use the global require function as follow :

 

 

require("MODULE_SHORT_CODE");

 

 

Using require, you can load a module into the current execution context. This is not common.js require, but a more basic inclusion pattern, where the loaded module is inlined within the current script.


If you have circular reference between modules, the require method will only load a single module once per require invocation. This is to protect from infinite loops and stack overflows.


You can conditionally require modules based on input parameters etc to have a single script that can perform multiple tasks without having a large script

 

if(something) {
    require("MODULE_SHORT_CODE_1");
} else {
    require("MODULE_SHORT_CODE_2");
}

 

 As for getting scripts to execute on player registration, you can bind your script to either RegistrationRequest or RegistrationResponse. 


Gabriel


I tried using exports.functionName = function().. for when there multiple functions in a module, but it keeps saying exports is undefined.

I also tried module.exports, same result.. is there a way we can achieve this ?

Hi some 001,


I take in the this scenario you're using a module (exports), and wanting to setup and use functions within this module? You can create a function within your module using:


var myFunctionName = function(){}


or


function myFunctionName(){}


You can then invoke these functions in a cloud code script by first requiring the module:


require("exports");


then calling the calling the function as you would if it were local:


myFunctionname();


I hope this helps clarify things for you. If you have any further questions please let us know.


Regards,

Vinnie


1 person likes this

While this's another way to do it, it is not what I expected from modules.

With this, modules in GS are simply a special type of folded code/code region and cannot be used for exporting functions.

I'm not sure whether require() is pure JavaScript or if it's just a NodeJS thing.

 

Anyway, thanks Vinnie.

Hi Vinnie!


I have some troubles with modules. I've created a module "CC_UsersHelpers" and used require("CC_UsersHelpers") is registration request. After that I call a function from module. Though debugger in test harness keep saying that the function is not defined

Login to post a comment