Sign In Register

How can we help you today?

Start a new topic
Answered

How to use the modules in real time script?

For code reuse in different scripts.

require("script1") - not working


Best Answer
So because RTscript work a lot differently to the event scripts, modules for RT need to be given an object reference before being called.
Function in RT modules are also setup differently.

As an example i'll create a RT module, that just prints 'Hello World' to the log, when a player connects (just a quick example)

So, the RT script might look something like this....

RTSession.onPlayerConnect(function(player){
    var helloWorld_Mod = require('helloWorld');
    helloWorld_Mod.log();
}


Now we need a module called 'helloWorld' .....

module.exports.log = function(){
    RTSession.getLogger().debug('Hello World!');
}

Hope that makes sense.
This is very new, so apologies that we arent currently up to date with documentation on this.

Sean

 


2 people have this question

Hi Vsevolod,


The real time scripts don't currently support the use of modules. I've submitted this as a feature request to our development team.


Kind regards,

 - Steve



Steve, Please let us know when this feature will be implemented

Hi Vsevolod,


There isn't currently an ETA on this feature, but I'll be sure to notify you once it has been implemented..


Kind regards,

 - Steve

any advance on modular module patterns?

Answer
So because RTscript work a lot differently to the event scripts, modules for RT need to be given an object reference before being called.
Function in RT modules are also setup differently.

As an example i'll create a RT module, that just prints 'Hello World' to the log, when a player connects (just a quick example)

So, the RT script might look something like this....

RTSession.onPlayerConnect(function(player){
    var helloWorld_Mod = require('helloWorld');
    helloWorld_Mod.log();
}


Now we need a module called 'helloWorld' .....

module.exports.log = function(){
    RTSession.getLogger().debug('Hello World!');
}

Hope that makes sense.
This is very new, so apologies that we arent currently up to date with documentation on this.

Sean

 

thats fine, actually I wanted to ask you about the module.exports option, seems you just answered that as well. thanks!

btw. any plans on AMD'fying? It seems, it already goes to such a direction.

Hello!


I'm not so good in JS, can you please clarify is it:

var helloWorld_Mod = require('helloWorld');

instance of object? 


And second question, I need a bunch of static functions. For example:

   

function ToDegrees (angle) {
  return angle * (180 / Math.PI);
}

function GetRandomArbitrary(min, max) {
    return Math.random() * (max - min) + min;
}

function Lerp(a, b, t) {
    var len = a.length;
    if (b.length != len) return;

    var x = [];
    for (var i = 0; i < len; i++)
        x.push(a[i] + t * (b[i] - a[i]));
    return x;
}

function CalculateDistance(from, to) {
    return  Math.sqrt(Math.pow((to.x - from.x), 2) + Math.pow((to.y - from.y), 2));;
}

   And I want use it in modules like this:

image

What the best way to do it? Can you please provide me some code example?


Thanks you


Hi Alexey, 


Our module 'require' essentially in-lines the code within your module. Allowing you to reference and execute any functions declared within. 


In relation to module best practice, it is best to maintain succinct modules i.e. to avoid having modules of massive scope. Each module should cater to a particular behavior and you should avoid the nesting of modules as this exponentially increases the time needed to inline. 


Could you provide a specific use case so that we can see how it is you intend to send and receive the data that is being operated upon by your modules? 


Happy to help - Patrick.  


Hi Alexey


You can reference modules by using a variable like your example or by just using require("moduleShortCode");

If you want to call a function in a module from an event that module must be required in that event or in a module that is required in that event.

For example 

If I set up four modules module_1 to module_4 each with a function func1, func2 etc

And set up an event that requires module_1 you can call func4 from that event if each module requires the ext module in the sequence.


But this does require all modules to be loaded so if you only needed func4 requiring just module_4 would be best.


Does that help?


Regards

Katie 

Thanks you!


I'll try it soon, and answer you!

Hi!


I did a lot experiments and separated a big scripts to many modules.

And I have few questions:

1. What context does "requare" put module?

 When I "require" module without equals any value I cant get access to it.

 But when I equal it like this var myModule = requare("MyModule") I can access to function myModule.LogSmth("test test test");

 It doesn't depends on place whether I put it in Event or in main Realtime Script body.

 Module code is:

 module.exports.LogSmth = function( message )

 {

  RTSession.getLogger().debug("LogSmth " + message );

 }

2. Why when I try to compare Object from module using operator "===" it always returns false.

 Code example below:

 var equals = Enums.ATTACKSTATE.Non == hero.TeamSrc.AttackSate;

 var tripleEquals = Enums.ATTACKSTATE.Non === hero.TeamSrc.AttackSate;

 var objectEquals = Enums.ATTACKSTATE.Non == Enums.ATTACKSTATE.Non;

 var objectEqualsTriple = Enums.ATTACKSTATE.Non === Enums.ATTACKSTATE.Non;

 RTSession.getLogger().debug("state is " + hero.TeamSrc.AttackSate.value + " Enums.ATTACKSTATE.Non " + Enums.ATTACKSTATE.Non.value + ", equals: " + equals + " tripleEquals " + tripleEquals + ", objectEquals " + objectEquals + ", objectEqualsTriple: " + objectEqualsTriple );

 The message from the realtime.log:

 "state is 0 Enums.ATTACKSTATE.Non 0, equals: true tripleEquals false, objectEquals true, objectEqualsTriple: false"

 Module code is:

 module.exports.ATTACKSTATE = {

  Non: { value: 0 },

  Attack: { value: 1 },

  Defend: { value: 2

 }

 And if I put this object in main realtime scrip all works correctly.

}


I can send you email with big explanation of all my experiments, does it best way?


Thank you!

Alexey Pozdnyakov

Hi Alexey,


You are correct; as per Sean's example above, it is necessary to assign the result of your 'require' call when using modules in RTScripts as they behave differently from those in other scripts.


Are you assigning your attack state value using your enum module, or hard-coding it as outlined above? Object comparison is done by reference as opposed to value, so even if both objects contain the same values for the same keys, the equality check will fail if the variables aren't referencing the same memory address. 


This bare-bones example illustrates this behaviour:


var a = {value:0}


var b = {value:0}


Spark.setScriptData("equal1", (a==b));

Spark.setScriptData("strict equal1", (a===b));



b = a;


Spark.setScriptData("equal2", (a==b));

Spark.setScriptData("strict equal2", (a===b));


As you can see, even though a and b are equivalent, the equality checks return false until I assign a to b.


I hope this helps clear things up a bit. If you have any further questions please let us know.


Regards,

Vinnie

Hi Vinnie!


It's not exactly what I mean. Strict equals doesn't work only in module.

There are example below:

  

var Enums   = require("Enums");
var totalConnection = 0;
RTSession.onPlayerConnect( function (player) 
{
    totalConnection++;
    if( totalConnection >=2 )
    {
        var val1 = Enums.TEST_OBJECTS.TEST_1;
        var val2 = Enums.TEST_OBJECTS.TEST_1;
        var moduleEquals = val1 === val2;
        RTSession.getLogger().debug("module equals " + moduleEquals ); // "module equals false"

        var localVal1 = localEnum.TEST_1;
        var localVal2 = localEnum.TEST_1;
        var localEquals = localVal1 === localVal2;
        RTSession.getLogger().debug("local equals " + localEquals ); //  "local equals true"
    }
});


var localEnum = 
{
    TEST_1 : {value: 1 }
}

  Module Enum.js Code:


 

module.exports.TEST_OBJECTS = 
{
    TEST_1 : {value: 1 }
}

 



Login to post a comment