Rhakar wrote:Sorry if I'm slightly off topic , but is there a way to reload the script.js without restarting the client?
of course you can
but let me show how to do this with few simple steps.
first, you should change onUserInput function to:
- Code: Select all
function onUserInput(input) {
print("On user input " + input);
eval(input);
}
or something similar(key here is eval(input);)
Next, you need way to load files. I propose to do it with this function:
- Code: Select all
var loadFileToString = function(fileName) {
return('' + new java.lang.String(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(fileName))));
}
Then you can execute script from file in current scope. Few more steps to do it:
- Code: Select all
var script = null;
var loadScript = function() {
eval(loadFileToString("scripts/test.js"));
}
function onGameLoaded(game) {
print('Game loaded');
g = game;
loadScript();
}
scripts/test.js contains:
- Code: Select all
script = new Object();
script.print = function() {
print("some shit");
}
so now(if all other stuff in script.js setup correctly) you can run client and after it load you can type in console "script.print();" and it will print you:
On user input script.print();
some shit
then you can change scripts/test.js file to something like this:
- Code: Select all
script = new Object();
script.print = function() {
print("some other shit");
}
type in console "loadScript();" and then "script.print();" again. If you do all right you will get:
loadScript();
On user input loadScript();
script.print();
On user input script.print();
some other shit
you can do this even with script.js file itself, but you suppose to find way to call it within global context
P.S. Few more additions to understand it right.
In script.js you defines script variable in global scope, so you can use it everywhere in code.
After that, in onGameLoaded() function client execute loadScript() function which loads scripts/test.js file in which we construct our script variable as empty Object and assign one field to it(print function).
When you recall loadScript() function manually via console your client executes this file again, script variable reconstructs as new empty object and all fields reassigns to it according to current file content.
P.P.S. I'm not a Java or JS programmer or even CS major, just SO and Google, so i'm open to all critics