in an platform game "TyranoBuilder" i can use some javascript ! So i can use like
var bb = (ORANGE)
and the game can take that variable by
f.Test = (bb);
My question is how i can import that bb variable from outside of game, from an url ! ? EXAMPLE
var bb = src"http....a.js"
so i can edit it everytime i want ! :| Sorry my English, but i really need ! i have read some things like in html firs.js second .js ! but the second js is in the game !
No: that's what JSON is for.
Don't make a text file, make a .json file and put your variable in that as an object property, like a lot of programs already do. E.g.
{
bb: "orange",
fg: "green"
}
Then you can either directly import it if you're using something like Node:
const config = require('./config.json');
...
let thing = config.bb;
Or you can separately load the file from url (e.g. using the Fetch API if you're working in the browser) and then use the .json() function on the fetched result to get the data as plain JS that you can access like any other part of your program:
function startGame(config) {
// entry point that starts your actual application
}
fetch("/config.json").then(data => data.json()).then(config => startGame(config));
Related
i'm currently working on an extends script which is the technology that we can use to create extension for adobe softwares, its a javascript similar script language
Im working on a script to automate some boring and repetitive tasks
Here is my problem i've to wait the creation of a bin before use a variable which call this bin but i can't, i tried
.then( Promise => { //Some Code }) ; setTimeout(Function(), time); nothing is working
Can someone help me please ?
Here is my code
root = app.project.rootItem
newFilesPath = "Path"
newFilesBin = root.children[0]
app.project.importFiles(newFilesPath, true, newFilesBin, false)
app.project.save()
for(i=0; i<newFilesBin.children.numItems; i++){ //here is the problem it tells me that
newFile = newFilesBin.children[i] //newFilesBin is null, i think he has not the
//time to create the file and then to put it
//in the variable
name = newFile.name
newTiktokBin = root.createBin(name)
root.children[i].children[0].moveBin(newTiktokBin)
}
You can simply send your program to sleep for any amount of milliseconds by using the $.sleep() method.
$.sleep(1000); // sends the program to sleep for a second
There is no such thing as .then() or promises in ExtendScript, as it is basically an ancient version of JavaScript.
I have been having issues with module.exports.
I have 2 Scripts that use Discord.js, and i need to have a SetTimeout() variable that is common to both scripts to be able to use ClearTimeout() from each of them.
I tried to use:
//first script
var foo = 20;
module.exports.foo = foo;
//Second Script
var exp = require('./firstscript');
console.log(exp.foo);
This was a test, to see if i was doing it the wrong way, with a simple variable instead of my SetTimeout().
The test worked fine when i ran new scripts with the node command but with npm start on my 2 original scripts it returned 'undefined' while having the same syntax.
In every script is already a module.exports for the Discord.js event class like 'Ready' for exemple.
I'm running this bit of code outside the main module.exports at the top where the const are declared.
I wonder if this is causing my issue.
example:
//the code i'm talking about is here.
module.exports = class ReadyEvent extends BaseEvent {
constructor() {
super('ready');
}
async run(client) {
Thanks for your help. Ask me for clarification if needed.
EDIT:
I looked up on the internet to see if it was an issue with the module.export that was already present on the script. And it was.
Apparently you cannot have several module.export in a script if each one of them doesn't specify a variable:
//this doesn't work
module.exports.value = value;
module.exports = value2;
//this works
module.exports.value = value;
module.exportd.value2 = value2;
My problem was that the one already present was used by the 'discord.js compiler' to register every command so i couldn't modify it without breaking the bot.
I decided to put my timer in a new script named GlobalVars and it worked Perfectly fine.
I'm Satisfied that it works now. For me the issue is fixed but i would love to know if it is possible to export a variable WITH the discord.js module.exports syntax included.
From your Second Script variable, its seems to be requiring a folder and not a file, and I don't think folders are allowed.
Assuming you are requiring a script and not a folder:
First Mistake: When requiring, inside the brackets you must have quotation marks ("") or ('').
If it is a folder, add a / next to ./firstscript and assign it to the file you want to refer to.
var exp = require('./firstscript/THE_INTENDED_FILE');
...If firstscript is a json file:
var exp = require('./firstscript.json');
Extracting Variables from file
// Option 1:
console.log(exp.foo);
// Option 2:
const extract = exp.foo
console.log(extract);
I tried to write an extension for vscode, the extension needs to read pasteImage.parth variable from ./vscode/settings.json file
{
"pasteImage.path": "${workspaceRoot}/assets/images"
}
I tried to use vscode.workspace.getConfiguration() function to get the variable like this:
let folderPathFromConfig = vscode.workspace.getConfiguration('pasteImage')['path'];
But it got ${workspaceRoot}/assets/images, I want ${workspaceRoot} to expand to the real path of the workspace root, what should I do?
Thanks for any hint!
There doesn't seem to be a service for it, you'd have to extend the AbstractVariableResolverService similarly to the debug host to do this.
Unfortunately you cannot import these things from within your extension.
The actual implementation is quite straightforward but I do wonder what they thought when they decided against this.
IProcessEnvironment is just a stupid interface for the built-in process.env prop.
According to the vscode extensibility documentation you can use
let workspaceRoot = vscode.workspace.workspaceFolders[0];
to get the root WorkspaceFolder. You can get the absolute path of a WorkspaceFolder by using its Uri:
let workspaceRootPath = workspaceRoot.uri.path;
Then you just replace the "${workspaceRoot}" sub string of the folderPathFromConfig.
To conclude:
let folderPathFromConfig = vscode.workspace.getConfiguration('pasteImage')['path'];
if (vscode.workspace.workspaceFolders !== undefined) { // is undefined if no folder is opened
let workspaceRoot = vscode.workspace.workspaceFolders[0];
folderPathFromConfig = folderPathFromConfig.replace('${workspaceRoot}', workspaceRoot.uri.path);
}
So, I'm a big fan of creating global namespaces in javascript. For example, if my app is named Xyz I normally have an object XYZ which I fill with properties and nested objects, for an example:
XYZ.Resources.ErrorMessage // = "An error while making request, please try again"
XYZ.DAL.City // = { getAll: function() { ... }, getById: function(id) { .. } }
XYZ.ViewModels.City // = { .... }
XYZ.Models.City // = { .... }
I sort of picked this up while working on a project with Knockout, and I really like it because there are no wild references to some objects declare in god-knows-where. Everything is in one place.
Now. This is ok for front-end, however, I'm currently developing a basic skeleton for a project which will start in a month, and it uses Node.
What I wanted was, instead of all the requires in .js files, I'd have a single object ('XYZ') which would hold all requires in one place. For example:
Instead of:
// route.js file
var cityModel = require('./models/city');
var cityService = require('./services/city');
app.get('/city', function() { ...........});
I would make an object:
XYZ.Models.City = require('./models/city');
XYZ.DAL.City = require('./services/city');
And use it like:
// route.js file
var cityModel = XYZ.Models.City;
var cityService = XYZ.DAL.City;
app.get('/city', function() { ...........});
I don't really have in-depth knowledge but all of the requires get cached and are served, if cached, from memory so re-requiring in multiple files isn't a problem.
Is this an ok workflow, or should I just stick to the standard procedure of referencing dependencies?
edit: I forgot to say, would this sort-of-factory pattern block the main thread, or delay the starting of the server? I just need to know what are the downsides... I don't mind the requires in code, but I just renamed a single folder and had to go through five files to change the paths... Which is really inconvenient.
I think that's a bad idea, because you are going to serve a ton of modules every single time, and you may not need them always. Your namespaced object will get quite monstrous. require will check the module cache first, so I'd use standard requires for each request / script that you need on the server.
I have some inline-javascript containing large datasets which are hard-coded into my PHP site:
var statsData = {
"times" : [1369008000,1369094400,1369180800,],
"counts" : [49,479,516,]
};
I'd like to refactor my code so that my variables are served with this structure:
[
[1369008000, 49],
[1369094400, 479],
[1369180800, 516],
]
However I have many files to update - are there any tools that would help automate this process?
Just create a new array then loop through the original one, and place the values according to the indexes:
var statsData = {"times":[1369008000,1369094400,1369180800,],"counts":[49,479,516,]};
var result = [];//Create a new array for results.
for (var i = 0; i < statsData.times.length; ++i){//Loop the original object's times property from 0 to it's length.
result.push([statsData.times[i], statsData.counts[i]]);//Push a new array to the result array in the new order that will contain the original values you acces throught the index in the loop variable.
}
console.log(result);
Also in your code you have two start [ in your object's counts attribute but only one ] closing it.
Carrying on from the comments; Trying to parse JS from a mix of PHP/HTML is horrible so if you are prepared to do some copying and pasting then - if it were me - I'd opt for a simple command-line tool. As your Javascript won't validate as JSON it doesn't make much sense to try and parse it in any other language.
I've knocked up a quick script to work with your current example (I'll leave it up to you to extend it further as needed). To run it you will need to install Node.js
Next, save the following where ever you like to organise files - lets call it statsData.js:
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data){
try {
eval(data+';global.data=statsData');
processData();
} catch(e) {
process.stdout.write('Error: Invalid Javascript\n');
}
});
function processData(){
try {
var i, out = [];
while(i = data.times.shift())
out.push([i, data.counts.shift()||0]);
process.stdout.write('var statsData='+JSON.stringify(out)+';\n');
} catch(e) {
process.stdout.write('Error: Unexpected Javascript\n');
}
}
Now you have a CLI tool that works with standard I/O, to use it open a terminal window and run:
$ node path/to/statsData.js
It will then sit and wait for you to copy and paste valid javascript statements into the terminal, otherwise you could always pipe the input stream from a file where you have copied and pasted your JS to:
$ cat inputFile.js | node path/to/statsData.js > outputFile.js
cat is a unix command - if you are working on a windows machine I think the equivalent is type - but I'm unable to test that right now.