Renamed Introduction to Editor Scripting to /Script/Introduction to Editor Scripting
commited
commit
001789037548dfa950bac7c52985f2c41fdf5407
... | ... | @@ -174,3 +174,4 @@ Execution Order.md: Script/Execution Order.md |
174 | 174 | Sync between devices.md: Questions/Sync between devices.md |
175 | 175 | Arcade search information.md: Questions/Arcade search information.md |
176 | 176 | Fancade Beta.md: Fancade Beta I.md |
177 | EditorScript/Introduction to Editor Scripting.md: Script/Introduction to Editor Scripting.md |
... | ... | @@ -1,132 +0,0 @@ |
1 | Editor Scripting is an experimental feature that can be used to build levels by programmatically placing blocks. The editor is currently available in the beta version of [[ Fancade Web ]] by entering the `EditorScript` command in the [command line](/wiki/fancade-web#command-line). |
|
2 | ||
3 | ## Getting Started |
|
4 | Let's create a new level and enter the `EditorScript` command. You will be greeted by a simple code editor with some examples in place. The code might seem unfamiliar at first, because it is written in [JavaScript](https://developer.mozilla.org/en-US/docs/Learn/JavaScript). But don't be scared, because we will learn the basics in this tutorial! |
|
5 | ||
6 | ## [๐](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function)Functions |
|
7 | So let's look at our first example. |
|
8 | ```js |
|
9 | // Set block at position (x, y, z) |
|
10 | setBlock(x, y, z, prefabIndex); |
|
11 | ``` |
|
12 | We can ignore the first line, since it begins with `//`. This makes the editor ignore any code in the current line, but it can give us some hints on what the following line does.\\ |
|
13 | The second line starts with the function `setBlock`. You can imagine this as placing a script block with the aforementioned name. The following text represents the inputs given to this function, separated by `,` and enclosed by brackets. So to put this in [[visual scripting]] terms, we are placing a block called `setBlock` and give it the inputs `x`, `y`, `z` and `prefabIndex`. |
|
14 | ||
15 | But those inputs were actually just placeholders, so let's replace them with some actual values! |
|
16 | ```js |
|
17 | // Set block at position (0, 0, 0) in level or open block |
|
18 | setBlock(0, 0, 0, 3); |
|
19 | ``` |
|
20 | Copy this code and paste it in the editor, then hit `Run` and `Save and Close`. As you might have guessed, this puts a block on the coordinates `(0, 0, 0)`. The fourth value is the unique identifier for the Grass block to be placed. There is a list of unique ids for each built-in block, but what if we want to place a custom block instead? |
|
21 | ||
22 | ## Creating Functions |
|
23 | We have already seen some built-in functions, but what if we wanted to create our own ones? |
|
24 | ```js |
|
25 | function moveBlock(fromX, fromY, fromZ, toX, toY, toZ) { |
|
26 | // Get block at position (fromX, fromY, fromZ) |
|
27 | let block = getBlock(fromX, fromY, fromZ, block); |
|
28 | ||
29 | // Remove block at position (fromX, fromY, fromZ) |
|
30 | setBlock(fromX, fromY, fromZ, 0); |
|
31 | // Set block at position (toX, toY, toZ) |
|
32 | setBlock(toX, toY, toZ, block); |
|
33 | } |
|
34 | ``` |
|
35 | This time, the line starts with a `function` expression, followed by the name of the function we want to create and the name of the inputs the function takes. The code inside the curly brackets is run when we run the function: |
|
36 | ```js |
|
37 | moveBlock(0, 0, 4, 1, 0, 4); |
|
38 | ``` |
|
39 | You will see that this function we have just created moves the block at `(0, 0, 4)` to `(1, 0, 4)`. This way, we can simplify our code by putting common tasks in a block of code that we can use later. |
|
40 | ||
41 | ## Strings |
|
42 | [Create a custom block](/wiki/build/how-do-i-make-my-own-custom-blocks), for instance `My Block`. Then replace your previous code with this one: |
|
43 | ```js |
|
44 | // Find the block called "My Block" |
|
45 | findBlock("My Block"); |
|
46 | ``` |
|
47 | You can once again see a function, but this one is called `findBlock`. In contrast to the previous function, this one doesn't take numbers as input. Instead it takes a block of text, called a string, which is encapsulated by a pair of quotes. We can now run this code, but it will do nothing at first, because we haven't used the output of the function anywhere. We can change this by storing the output in a variable! |
|
48 | ||
49 | ## [๐](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types/#Declarations)Variables |
|
50 | ```js |
|
51 | // Find block by name and store it in a variable |
|
52 | let myBlock = findBlock("My Block"); |
|
53 | setBlock(0, 0, 0, myBlock); |
|
54 | ``` |
|
55 | This line is different, because it doesn't start with the name of a function. Instead, we can see the `let` keyword, which creates a new local variable for us. The name of the variable is determined by the next word, `myBlock`. To store a value in the new variable, we follow the variable name with a `=` and the value to store, similar to a [[Set Variable]] block. In this case, this value is the output of the function we have used before.\\ |
|
56 | The next line looks familiar and it once again places a block at the coordinates `0, 0, 0`. But this time we don't directly input the id of our block, but instead we use the value stored in the variable which we have created before. This allows us to not only store any kind of value, but also use it in multiple other places.\\ |
|
57 | After we have created the variable, we can change its value by adding following code in order to set a new custom block next to our previous block: |
|
58 | ```js |
|
59 | // Change the value of the variable |
|
60 | myBlock = findBlock("My New Block"); |
|
61 | setBlock(1, 0, 0, myBlock); |
|
62 | ``` |
|
63 | But what if we want to store multiple blocks in our variable? |
|
64 | ||
65 | ## [๐](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)Arrays |
|
66 | ```js |
|
67 | // Create an empty array |
|
68 | let myBlocks = []; |
|
69 | // Store block ids in the array |
|
70 | myBlocks[0] = findBlock("My Block"); |
|
71 | myBlocks[1] = findBlock("My New Block"); |
|
72 | ``` |
|
73 | Like previously, we are creating a new variable. But this time, we instead store an empty list `[]` in it. We then add values to the array by assigning them to the indexes in curly braces. This works similar to a [[List Element]] block and is also used to access the values afterwards: |
|
74 | ```js |
|
75 | // Set blocks from the list |
|
76 | setBlock(0, 0, 1, myBlocks[0]); |
|
77 | setBlock(1, 0, 1, myBlocks[0]); |
|
78 | ``` |
|
79 | Additionally, we can create entire arrays in just one line, similary to how we pass our inputs: |
|
80 | ```js |
|
81 | // Create a filled array |
|
82 | let myBlocks = [ |
|
83 | findBlock("My Block"), |
|
84 | findBlock("My New Block") |
|
85 | ]; |
|
86 | ``` |
|
87 | This way, we can store multiple values of any type, including arrays themselves! This can be used to create simple multidimensional lists.\\ |
|
88 | Now what if we wanted to place all our blocks at once? |
|
89 | ||
90 | ## [๐](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration)Loops |
|
91 | ```js |
|
92 | // Set block for each block in the array |
|
93 | for (let index of myBlocks) { |
|
94 | setBlock(index, 0, 2, myBlocks[index]) |
|
95 | } |
|
96 | ``` |
|
97 | This time, the line starts with a `for` loop expression. Inside this expression, we create a variable `index` and assign it to each index in the array. The code inside the curly braces will then be repeated for each new index. This is similar to using a [[Loop]] block with the array length as input. |
|
98 | ||
99 | Take note that there are also other kinds of loop iterators. In our example, we have used a [for...of](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration) statement. |
|
100 | ||
101 | ## [๐](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures/#Objects)Objects |
|
102 | ```js |
|
103 | // Create an empty object |
|
104 | let myBlocks = {}; |
|
105 | // Store block ids in the object |
|
106 | myBlocks["My Block"] = findBlock("My Block"); |
|
107 | myBlocks["My New Block"] = findBlock("My New Block"); |
|
108 | ``` |
|
109 | Objects `{}` are quite similar to lists in that they can hold values for different indexes. However, they access their values using a string as index. For instance, this allows you to store blocks based on their name instead of their id. |
|
110 | ``` |
|
111 | // Set blocks from the object |
|
112 | setBlock(0, 0, 3, myBlocks["My Block"]); |
|
113 | setBlock(1, 0, 3, myBlocks["My New Block"]); |
|
114 | ``` |
|
115 | However, this also allows us to create objects with custom properties like this: |
|
116 | ``` |
|
117 | // Create object with custom properties |
|
118 | let myBlock = { |
|
119 | x: 0, |
|
120 | y: 0, |
|
121 | z: 4, |
|
122 | id: findBlock("My Block") |
|
123 | }; |
|
124 | ``` |
|
125 | We can then access the properties by following the variable name by `.` and the name of the property: |
|
126 | ```js |
|
127 | // Set block by accessing custom properties |
|
128 | setBlock(myBlock.x, myBlock.y, myBlock.z, myBlock.id); |
|
129 | ``` |
|
130 | ||
131 | ||
132 | <!-- ToDo: Conditions --> |
... | ... | @@ -0,0 +1,132 @@ |
1 | Editor Scripting is an experimental feature that can be used to build levels by programmatically placing blocks. The editor is currently available in the beta version of [[ Fancade Web ]] by entering the `EditorScript` command in the [command line](/wiki/fancade-web#command-line). |
|
2 | ||
3 | ## Getting Started |
|
4 | Let's create a new level and enter the `EditorScript` command. You will be greeted by a simple code editor with some examples in place. The code might seem unfamiliar at first, because it is written in [JavaScript](https://developer.mozilla.org/en-US/docs/Learn/JavaScript). But don't be scared, because we will learn the basics in this tutorial! |
|
5 | ||
6 | ## [๐](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function)Functions |
|
7 | So let's look at our first example. |
|
8 | ```js |
|
9 | // Set block at position (x, y, z) |
|
10 | setBlock(x, y, z, prefabIndex); |
|
11 | ``` |
|
12 | We can ignore the first line, since it begins with `//`. This makes the editor ignore any code in the current line, but it can give us some hints on what the following line does.\\ |
|
13 | The second line starts with the function `setBlock`. You can imagine this as placing a script block with the aforementioned name. The following text represents the inputs given to this function, separated by `,` and enclosed by brackets. So to put this in [[visual scripting]] terms, we are placing a block called `setBlock` and give it the inputs `x`, `y`, `z` and `prefabIndex`. |
|
14 | ||
15 | But those inputs were actually just placeholders, so let's replace them with some actual values! |
|
16 | ```js |
|
17 | // Set block at position (0, 0, 0) in level or open block |
|
18 | setBlock(0, 0, 0, 3); |
|
19 | ``` |
|
20 | Copy this code and paste it in the editor, then hit `Run` and `Save and Close`. As you might have guessed, this puts a block on the coordinates `(0, 0, 0)`. The fourth value is the unique identifier for the Grass block to be placed. There is a list of unique ids for each built-in block, but what if we want to place a custom block instead? |
|
21 | ||
22 | ## Creating Functions |
|
23 | We have already seen some built-in functions, but what if we wanted to create our own ones? |
|
24 | ```js |
|
25 | function moveBlock(fromX, fromY, fromZ, toX, toY, toZ) { |
|
26 | // Get block at position (fromX, fromY, fromZ) |
|
27 | let block = getBlock(fromX, fromY, fromZ, block); |
|
28 | ||
29 | // Remove block at position (fromX, fromY, fromZ) |
|
30 | setBlock(fromX, fromY, fromZ, 0); |
|
31 | // Set block at position (toX, toY, toZ) |
|
32 | setBlock(toX, toY, toZ, block); |
|
33 | } |
|
34 | ``` |
|
35 | This time, the line starts with a `function` expression, followed by the name of the function we want to create and the name of the inputs the function takes. The code inside the curly brackets is run when we run the function: |
|
36 | ```js |
|
37 | moveBlock(0, 0, 4, 1, 0, 4); |
|
38 | ``` |
|
39 | You will see that this function we have just created moves the block at `(0, 0, 4)` to `(1, 0, 4)`. This way, we can simplify our code by putting common tasks in a block of code that we can use later. |
|
40 | ||
41 | ## Strings |
|
42 | [Create a custom block](/wiki/build/how-do-i-make-my-own-custom-blocks), for instance `My Block`. Then replace your previous code with this one: |
|
43 | ```js |
|
44 | // Find the block called "My Block" |
|
45 | findBlock("My Block"); |
|
46 | ``` |
|
47 | You can once again see a function, but this one is called `findBlock`. In contrast to the previous function, this one doesn't take numbers as input. Instead it takes a block of text, called a string, which is encapsulated by a pair of quotes. We can now run this code, but it will do nothing at first, because we haven't used the output of the function anywhere. We can change this by storing the output in a variable! |
|
48 | ||
49 | ## [๐](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types/#Declarations)Variables |
|
50 | ```js |
|
51 | // Find block by name and store it in a variable |
|
52 | let myBlock = findBlock("My Block"); |
|
53 | setBlock(0, 0, 0, myBlock); |
|
54 | ``` |
|
55 | This line is different, because it doesn't start with the name of a function. Instead, we can see the `let` keyword, which creates a new local variable for us. The name of the variable is determined by the next word, `myBlock`. To store a value in the new variable, we follow the variable name with a `=` and the value to store, similar to a [[Set Variable]] block. In this case, this value is the output of the function we have used before.\\ |
|
56 | The next line looks familiar and it once again places a block at the coordinates `0, 0, 0`. But this time we don't directly input the id of our block, but instead we use the value stored in the variable which we have created before. This allows us to not only store any kind of value, but also use it in multiple other places.\\ |
|
57 | After we have created the variable, we can change its value by adding following code in order to set a new custom block next to our previous block: |
|
58 | ```js |
|
59 | // Change the value of the variable |
|
60 | myBlock = findBlock("My New Block"); |
|
61 | setBlock(1, 0, 0, myBlock); |
|
62 | ``` |
|
63 | But what if we want to store multiple blocks in our variable? |
|
64 | ||
65 | ## [๐](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)Arrays |
|
66 | ```js |
|
67 | // Create an empty array |
|
68 | let myBlocks = []; |
|
69 | // Store block ids in the array |
|
70 | myBlocks[0] = findBlock("My Block"); |
|
71 | myBlocks[1] = findBlock("My New Block"); |
|
72 | ``` |
|
73 | Like previously, we are creating a new variable. But this time, we instead store an empty list `[]` in it. We then add values to the array by assigning them to the indexes in curly braces. This works similar to a [[List Element]] block and is also used to access the values afterwards: |
|
74 | ```js |
|
75 | // Set blocks from the list |
|
76 | setBlock(0, 0, 1, myBlocks[0]); |
|
77 | setBlock(1, 0, 1, myBlocks[0]); |
|
78 | ``` |
|
79 | Additionally, we can create entire arrays in just one line, similary to how we pass our inputs: |
|
80 | ```js |
|
81 | // Create a filled array |
|
82 | let myBlocks = [ |
|
83 | findBlock("My Block"), |
|
84 | findBlock("My New Block") |
|
85 | ]; |
|
86 | ``` |
|
87 | This way, we can store multiple values of any type, including arrays themselves! This can be used to create simple multidimensional lists.\\ |
|
88 | Now what if we wanted to place all our blocks at once? |
|
89 | ||
90 | ## [๐](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration)Loops |
|
91 | ```js |
|
92 | // Set block for each block in the array |
|
93 | for (let index of myBlocks) { |
|
94 | setBlock(index, 0, 2, myBlocks[index]) |
|
95 | } |
|
96 | ``` |
|
97 | This time, the line starts with a `for` loop expression. Inside this expression, we create a variable `index` and assign it to each index in the array. The code inside the curly braces will then be repeated for each new index. This is similar to using a [[Loop]] block with the array length as input. |
|
98 | ||
99 | Take note that there are also other kinds of loop iterators. In our example, we have used a [for...of](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration) statement. |
|
100 | ||
101 | ## [๐](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures/#Objects)Objects |
|
102 | ```js |
|
103 | // Create an empty object |
|
104 | let myBlocks = {}; |
|
105 | // Store block ids in the object |
|
106 | myBlocks["My Block"] = findBlock("My Block"); |
|
107 | myBlocks["My New Block"] = findBlock("My New Block"); |
|
108 | ``` |
|
109 | Objects `{}` are quite similar to lists in that they can hold values for different indexes. However, they access their values using a string as index. For instance, this allows you to store blocks based on their name instead of their id. |
|
110 | ``` |
|
111 | // Set blocks from the object |
|
112 | setBlock(0, 0, 3, myBlocks["My Block"]); |
|
113 | setBlock(1, 0, 3, myBlocks["My New Block"]); |
|
114 | ``` |
|
115 | However, this also allows us to create objects with custom properties like this: |
|
116 | ``` |
|
117 | // Create object with custom properties |
|
118 | let myBlock = { |
|
119 | x: 0, |
|
120 | y: 0, |
|
121 | z: 4, |
|
122 | id: findBlock("My Block") |
|
123 | }; |
|
124 | ``` |
|
125 | We can then access the properties by following the variable name by `.` and the name of the property: |
|
126 | ```js |
|
127 | // Set block by accessing custom properties |
|
128 | setBlock(myBlock.x, myBlock.y, myBlock.z, myBlock.id); |
|
129 | ``` |
|
130 | ||
131 | ||
132 | <!-- ToDo: Conditions --> |