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(); }
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
V
Vsevolod Miroshnikov
said
almost 6 years ago
Steve, Please let us know when this feature will be implemented
Customer Support
said
almost 6 years ago
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
C
Cem Gencer
said
over 5 years ago
any advance on modular module patterns?
Customer Support
said
over 5 years ago
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(); }
Hope that makes sense. This is very new, so apologies that we arent currently up to date with documentation on this.
Sean
C
Cem Gencer
said
over 5 years ago
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.
A
Alexey Pozdnyakov
said
about 5 years ago
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:
What the best way to do it? Can you please provide me some code example?
Thanks you
Customer Support
said
about 5 years ago
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.
Customer Support
said
about 5 years ago
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
A
Alexey Pozdnyakov
said
about 5 years ago
Thanks you!
I'll try it soon, and answer you!
A
Alexey Pozdnyakov
said
about 5 years ago
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.
"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
Customer Support
said
about 5 years ago
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
A
Alexey Pozdnyakov
said
about 5 years ago
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 }
}
Vsevolod Miroshnikov
For code reuse in different scripts.
require("script1") - not working
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
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstCustomer Support
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
Vsevolod Miroshnikov
Steve, Please let us know when this feature will be implemented
Customer Support
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
Cem Gencer
any advance on modular module patterns?
Customer Support
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
Cem Gencer
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.
Alexey Pozdnyakov
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:
And I want use it in modules like this:

What the best way to do it? Can you please provide me some code example?Thanks you
Customer Support
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.
Customer Support
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
Alexey Pozdnyakov
Thanks you!
I'll try it soon, and answer you!
Alexey Pozdnyakov
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
Customer Support
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
Alexey Pozdnyakov
Hi Vinnie!
It's not exactly what I mean. Strict equals doesn't work only in module.
There are example below:
Module Enum.js Code:
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2487 topics