Updated Introduction to Editor Scripting.md (markdown)
commited
commit
85ccabbeba65337c81c0f5bcdf750542df732e5d
... | ... | @@ -19,6 +19,25 @@ setBlock(0, 0, 0, 3); |
19 | 19 | ``` |
20 | 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 | 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 | ||
22 | 41 | ## Strings |
23 | 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: |
24 | 43 | ```js |
... | ... | @@ -107,24 +126,5 @@ We can then access the properties by following the variable name by `.` and the |
107 | 126 | setBlock(myBlock.x, myBlock.y, myBlock.z, myBlock.id); |
108 | 127 | ``` |
109 | 128 | |
110 | ## Creating Functions |
|
111 | We have already seen some built-in functions, but what if we wanted to create our own ones? |
|
112 | ```js |
|
113 | function moveBlock(fromX, fromY, fromZ, toX, toY, toZ) { |
|
114 | // Get block at position (fromX, fromY, fromZ) |
|
115 | let block = getBlock(fromX, fromY, fromZ, block); |
|
116 | ||
117 | // Remove block at position (fromX, fromY, fromZ) |
|
118 | setBlock(fromX, fromY, fromZ, 0); |
|
119 | // Set block at position (toX, toY, toZ) |
|
120 | setBlock(toX, toY, toZ, block); |
|
121 | } |
|
122 | ``` |
|
123 | 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: |
|
124 | ```js |
|
125 | moveBlock(0, 0, 4, 1, 0, 4); |
|
126 | ``` |
|
127 | 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. |
|
128 | ||
129 | 129 | |
130 | 130 | <!-- ToDo: Conditions --> |