WayScript's JS editor lets you reference Variables that exist in your program. To do this, you can reference them through the variables dictionary.
As an example, let's create a variable called "Location" and set a value of "Brooklyn, NY"
location = variables[ "Location" ];// Or, as a JS objectlocation = variables.Location;
More abstractly, you reference any variable with the format:
const my_var = variables[ "<var_name>" ];
You can also output data from your JS code and turn these into variables that can be used by other Modules in your program.
You can do this with the variables dictionary in the following format:
variables[ "<Var Name>" ] = variable;// exampleconst currentTime = new Date();variables[ "Date" ] = currentTime.toISOString();
If you would like to use an npm module in your code, you can declare those dependencies in the "Requirements.txt" file.
Declare these dependencies on separate lines, using the format you would for an npm-install command.
You can then consume these packages in your code:
const wayscript = require( 'wayscript' );wayscript.apiKey = 'YOUR_API_KEY';let programId = 1234;wayscript.runProgram( programId );
foo.js, which has an exported function
You can import and use the
bar method in
scratch.js by using a relative import:
const foo = require('./foo');