Jsconsole.js
From Spheriki
The custom script jsconsole.js by Sulphur21 enables you to include a JavaScript console into your project.
Contents |
What is a JavaScript console?
A console is a command line, where you can type in JavaScript statements and expressions and have them executed immediately. It can simplify debugging, because it lets you watch and change variables during the runtime of your game. It can also be used to call or even declare functions. Generally, it simply runs any JavaScript code you type in.
Sphere has a console included (see the 'tools' menu), but it runs independently from your game's scripts and therefore does not help you checking and setting your game's vars and functions. This is why jsonsole.js has been created.
Download and usage
Getting started
Download jsconsole.js.txt <link is broken>, rename the file to jsconsole.js and copy it into the scripts folder of your project. The license of the script has been placed on top of the script code. Read it before using the script. To integrate the JSConsole() object type into your game do as follows:
RequireScript("jsconsole.js");
MyConsole = new JSConsole();
To execute the object you just created, you must place the following function call somewhere in your code (for example as an entry of your main menu):
MyConsole.run();
Alternately, it can be very useful, to have its execution bound to a key. This way you can have it pop up (almost) whenever you need it. For example:
BindKey(KEY_F1, "MyConsole.run()", "");
Running the console
At the console, the I> symbol stands for input (enter something here). O> represents output. Simply type in a var name and press enter to have the value of the var displayed. Or type in code that you want executed. Like:
myVar = 32;
or:
DoSomething();
or even more complex stuff.
Hotkeys
These are the console hotkeys:
- ENTER = execute.
- UP, DOWN = scroll up/down.
- PAGE UP, PAGE DOWN = turn pages.
- SHIFT+HOME, SHIFT+END = move to beginning/end of session
- F8 = clear session.
- ESC = exit.
Pressing the left direction key at the beginning of an empty command line will bring up the previously entered line again.
Keyboard layouts
If you encounter problems typing in special characters, because they aren't associated with the keys they should be, you most likely have a non-US keyboard layout. To make the console use another layout, go get the script keyboard_layout.js, then copy it into the scripts folder of your project (next to jsconsole.js) and use this code to include and run the console:
RequireScript("keyboard_layout.js");
RequireScript("jsconsole.js");
MyConsole = new JSConsole();
MyConsole.keyboardLayout = "Germany";
This will activate German keyboard layout. Replace "Germany" with another name depending on your preferred layout. For a list of supported keyboard layouts and how to make your own ones, see keyboard_layout.js.
As usual, use
MyConsole.run();
to run the console.
Notes
- To view the content of a given var that is an array or an object, it can be really helpful to use JavaScript's
toSource()method. For example entername.toSource()
where 'name' should be the var name. Use up and down to scroll the screen, if too much code appears at once. - Don't use the
varkeyword to declare global variables at the console. Just writename = value
instead. Plus, to declare a function, don't tryfunction myFunc() { ... }otherwise you'll get an error. Declare it like this:myFunc = function() { ... }At the moment, the mentioned workarounds will hopefully suffice. - Using the keyword
thisat the console will refer to the console object itself (unlessthisis placed inside of a method declaration).
Revision history
- Version 1.01
- Renamed script file to
jsconsole.js. The object type name turned fromConsoletoJSConsole, too. - Added support for international keyboard layouts.
- Created online documentation.
- Renamed script file to
- Version 1.00
- First public release.
See also
- RequireScript()
- BindKey()
