mtr_addon:nte:js:start
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
mtr_addon:nte:js:start [2023/10/08 14:36] – weryskok | mtr_addon:nte:js:start [2025/05/10 18:51] (current) – Improve scripting explanation based on JCM wiki lx862 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== JavaScript Support ====== | ====== JavaScript Support ====== | ||
- | NTE supports fully customizable control of rendering and other features using JavaScript | + | |
+ | NTE supports fully customizable control of rendering and other features using JavaScript. This page lists the various functions, objects, | ||
===== Why Overcomplicate Everything? ===== | ===== Why Overcomplicate Everything? ===== | ||
- | The main purpose of this feature is to implement dynamic displays on trains, but you have to keep in mind that the interface of the displays, the logic of operation, and so on, varies greatly from city to city and from train to train around the world. | ||
- | If a simple format was chosen, it would not be possible to flexibly reproduce the actual interface, and there would be a lot of requests and complaints; if a more complex format was developed | + | The main purpose |
- | ===== How To Edit .js Files? ===== | + | By simplifying this process, it would be too hard to accurately reproduce how the system looks and works in real life and a lot of people would complain. This more complicated format is designed |
- | Any text editor will do, such as Notepad3 or Notepad++. You may want to use something like Visual Studio Code. Using IDEA/Visual Studio | + | |
- | This article assumes that you have some basic understanding of JavaScript, so it won't delve into the basic syntax and other aspects of JavaScript here, you can learn JavaScript from resources on the web, such as [[https:// | + | ===== What is JavaScript? ===== |
- | ===== Type Annotation | + | JavaScript is a programming language that... in very simple terms, instructs computer to do stuff :D |
- | As you know, values in JS have different types. When calling a function, you must pass parameters of the appropriate type, and the result it returns will also have a type. In this article, the parameter and return types of the provided functions are denoted in a form similar to TypeScript. For example: | + | |
- | * <code javascript> | + | It can describe logic, an example would be: //__If__ there' |
+ | |||
+ | This rest of this article assumes that you have a basic understanding of JavaScript and JavaScript types, so it won't delve into the basic syntax and other aspects of it here. You can learn JavaScript from resources on the web, such as [[https:// | ||
+ | |||
+ | ===== The Nature of Scripting in NTE ===== | ||
+ | |||
+ | While JavaScript is commonly associated with webpages or even server applications (via Node.js), NTE's implementation of JavaScript only utilize the language itself. | ||
+ | |||
+ | As such, this means that you only really need to care about the language itself (e.g. Variable & Function Declaration, | ||
+ | |||
+ | Keep that in mind, as IDE (e.g. Visual Studio Code) may assume you are developing for a webpage and provides suggestions that are not applicable to our method of scripting! | ||
+ | |||
+ | ===== Script Flow ===== | ||
+ | |||
+ | ==== Initial Parsing ==== | ||
+ | |||
+ | Instead of each train having it's own script instance, your JS scripts are parsed (Or rather, executed) **once** during the resource pack loading. Your script are expected to have functions with specific name (e.g. **create()**, | ||
+ | |||
+ | These functions will be invoked by NTE with the parameter corresponding to a specific train/ | ||
+ | |||
+ | Consider the following example of scripting applied to a train: | ||
+ | |||
+ | <code javascript> | ||
+ | let displaySpeed = 1; | ||
+ | |||
+ | function create(ctx, state, train) { | ||
+ | | ||
+ | } | ||
+ | |||
+ | function render(ctx, state, train) { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | function dispose(ctx, | ||
+ | } | ||
+ | |||
+ | console.log(displaySpeed); | ||
+ | </ | ||
+ | |||
+ | An example output would be: | ||
+ | |||
+ | <code -> | ||
+ | 1 // The console.log at the bottom of the script, as the entire script is executed once during resource reload | ||
+ | |||
+ | dp: 1.75 // Train A rendering | ||
+ | dp: 2.75 // Train A rendering | ||
+ | |||
+ | // Assume Train B now enters the view | ||
+ | |||
+ | dp: 3.75 // Train A rendering | ||
+ | dp: 1.75 // Train B rendering | ||
+ | |||
+ | dp: 4.75 // Train A rendering | ||
+ | dp: 2.75 // Train B rendering | ||
+ | </ | ||
+ | |||
+ | ==== Execution ==== | ||
+ | |||
+ | An example flow is available below. The following diagram assumes the player is running Minecraft at **13fps** (For simplicity sake), which means 13 frames in 1 second. | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Immediately you may have noticed the following thing: | ||
+ | |||
+ | === Scripts are executed asynchronously / In a different thread === | ||
+ | |||
+ | This means that the script runs in the background and does not prevent the game from continue rendering (Therefore, less fps lag). __Note that this does NOT mean you can freely block script execution or run some **Thread.Sleep**, | ||
+ | |||
+ | === Functions are invoked every frame === | ||
+ | |||
+ | More precisely, the '' | ||
+ | |||
+ | If there' | ||
+ | |||
+ | As such, you should not assume that your function will always be called "x times per second", | ||
+ | |||
+ | This also means that if you increment a variable by a fixed amount for each frame, that increment speed won't be the same if the fps is higher/ | ||
+ | |||
+ | Delta timing is used to solve this by obtaining the time since last frame, which can then be used to balance out the value. | ||
+ | |||
+ | === Except they aren't always invoked every frame! === | ||
+ | |||
+ | While NTE //tries// to call the '' | ||
+ | |||
+ | ===== Do I have to learn Java to write JavaScript? ===== | ||
+ | |||
+ | JavaScript does not have anything, or not much to do with Java at all, even though they share " | ||
+ | |||
+ | ===== But can I use Java classes in JavaScript? ===== | ||
+ | |||
+ | Under normal circumstances, | ||
+ | |||
+ | //However// the JavaScript Engine that NTE uses - **Rhino**, //do// allow using classes from the standard Java library as '' | ||
+ | |||
+ | ===== Documentation Format ===== | ||
+ | |||
+ | As you know, values in JS have different types. When calling a function, you must pass parameters of the appropriate type, and the result it returns will also have a type. In this article, | ||
+ | |||
+ | * <code javascript> | ||
+ | static Resources.id(idStr: | ||
+ | </ | ||
* '' | * '' | ||
* '' | * '' | ||
* '': | * '': | ||
- | * <code javascript> | + | * <code javascript> |
+ | Matrices.rotateX(radian: | ||
+ | </ | ||
* The lack of '' | * The lack of '' | ||
* '' | * '' | ||
* '': | * '': | ||
* '': | * '': | ||
+ | * | ||
- | ===== Using Built-In Java Classes | + | ===== Errors |
- | The Rhino JS engine allows using classes from the standard Java library as '' | + | |
- | ===== Declaring variables using let or var ===== | + | If the script is executed incorrectly, |
- | NTE uses JavaScript' | + | |
+ | In addition, NTE pauses the script for 4 seconds and then tries to run it again. | ||
+ | |||
+ | ===== Tips & Notes ===== | ||
+ | |||
+ | ==== Declaring variables using let or var ==== | ||
+ | |||
+ | NTE uses JavaScript' | ||
+ | |||
+ | This means you can't do '' | ||
+ | |||
+ | Instead, you have use syntax like '' | ||
[Translator' | [Translator' | ||
- | ===== Don't Block Or Infinitely Loop ===== | + | ==== Don't Block Or Infinitely Loop ==== |
- | NTE calls the function you wrote once per frame and expects | + | |
+ | The function you wrote are called | ||
+ | |||
+ | What you likely | ||
+ | |||
+ | If you are trying to execute a long-running operation (e.g. Fetching data over the internet), you should submit it to another thread/ | ||
+ | |||
+ | If blocks or infinite loops did occur, then the entire script execution will stall as scripts are executed one at a time [//in the same thread//]. In such situation, you can reset it with F3+T. | ||
- | If locks or infinite loops occur in the code, the entire script execution will stall because NTE scripts are executed one at a time [in the same thread]. This can be reset with F3+T. | + | ==== Interoperability between Java Classes/ |
- | ===== Differences In Using Java Classes ===== | ||
For common function types such as strings, Java and JavaScript have different class implementations, | For common function types such as strings, Java and JavaScript have different class implementations, | ||
For example, here's an example of a problem caused by using '' | For example, here's an example of a problem caused by using '' | ||
+ | |||
<code javascript> | <code javascript> | ||
var stationName = train.getThisRoutePlatforms().get(0).station.name; | var stationName = train.getThisRoutePlatforms().get(0).station.name; | ||
Line 47: | Line 167: | ||
print(("" | print(("" | ||
</ | </ | ||
+ | |||
Similarly, there is a '' | Similarly, there is a '' | ||
- | ===== Supported Parts Of The JavaScript Standard ===== | + | ==== Supported Parts Of The JavaScript Standard ==== |
The Rhino engine does not support all of the latest JavaScript features. See [[https:// | The Rhino engine does not support all of the latest JavaScript features. See [[https:// | ||
- | ===== Errors | + | ==== Running Other Scripts |
- | If the script is executed incorrectly, | + | |
- | In addition, NTE pauses the script for 4 seconds and then tries to run it again. | + | |
- | + | static include(relPath: | |
- | ===== Running Other Scripts ===== | + | </ |
- | | + | |
* Loads and runs another JS file relative to the current JS file. | * Loads and runs another JS file relative to the current JS file. | ||
- | * <code javascript> | + | * <code javascript> |
+ | static include(path: | ||
+ | </ | ||
* Loads and runs the JS file by location in the resource pack. For example, '' | * Loads and runs the JS file by location in the resource pack. For example, '' | ||
+ | |||
+ | ===== Source ===== | ||
+ | |||
+ | * [[https:// | ||
+ | |||
+ | * [[https:// | ||
+ | |||
+ |
mtr_addon/nte/js/start.1696775806.txt.gz · Last modified: 2023/10/08 14:36 by weryskok