Updated Introduction to Editor Scripting.md (markdown)
commited
commit
682acee51da3244359599e1e613c40c62b414731
... | ... | @@ -59,6 +59,17 @@ setBlock(0, 0, 1, myBlocks[0]); |
59 | 59 | setBlock(1, 0, 1, myBlocks[0]); |
60 | 60 | ``` |
61 | 61 | |
62 | ## Loops |
|
63 | Now if we want to place multiple blocks at once of course we will need a loop! JS uses 3 types of loop; "while" loop, "do while" loop and "for" loop. In this scenario we will use the "for" loop. |
|
64 | ``` |
|
65 | const myBlocks = [findBlock("Bricks"), findBlock("Dirt")]; |
|
66 | ||
67 | let x = 0; |
|
68 | for (let element of myBlocks) { |
|
69 | setBlock(x++, 0, 0, element); |
|
70 | } |
|
71 | ``` |
|
72 | ||
62 | 73 | ## Objects |
63 | 74 | |
64 | 75 | Objects `{}` works almost similarly to arrays but unlike it is not considered a collection of value but instead a single value composed of other values called "properties" and functions called "methods". Lets take for example a ball, you will ask, how bouncy is this ball? what is its radius? just like this ball an object has its own properties to determine its characteristics. Using it lets try to make a simple structure. |
... | ... | @@ -66,7 +77,9 @@ Objects `{}` works almost similarly to arrays but unlike it is not considered a |
66 | 77 | ### Properties |
67 | 78 | ``` |
68 | 79 | let justATree = { |
69 | position: [0, 0, 0], |
|
80 | x: 0, |
|
81 | y: 0, |
|
82 | z: 0, |
|
70 | 83 | components: { |
71 | 84 | // component: material, size, position |
72 | 85 | trunk: ["Wood Y", [1, 6, 1] [0, 0, 0]], |
... | ... | @@ -76,26 +89,24 @@ let justATree = { |
76 | 89 | |
77 | 90 | //If you want to you can also add or override properties later |
78 | 91 | ``` |
79 | properties are values that represent chracteristics of an object it can be any value type or a function. As you can see in our example we made the object `justATree`which contains properties named `position` and `component`. Using this properties lets try to make a method that will build the tree! |
|
92 | properties are values that represent chracteristics of an object it can be any value type or a function. As you can see in our example we made the object `justATree`which contains properties named `x`, `y`, `z` and `component`. Using this properties lets try to make a method that will build the tree! |
|
80 | 93 | |
81 | 94 | ### Methods |
82 | 95 | methods are special properties of an object that can be used as instruction for its behaviors. Now lets try to add a new method named `build` inside `justATree`. |
83 | 96 | |
84 | 97 | ``` |
85 | 98 | let justATree = { |
86 | position: [0, 0, 0], |
|
99 | x: 0, |
|
100 | y: 0, |
|
101 | z: 0, |
|
87 | 102 | components: { |
88 | 103 | // component: material, size, position |
89 | 104 | trunk: ["Wood Y", [1, 6, 1] [0, 0, 0]], |
90 | 105 | leaves: [Foliage, [3, 3, 3], [-1, 4, -1]] |
91 | 106 | }, |
92 | 107 | build: () => { |
93 | // the "this" keyword refers to the object it is inside to, in this case the "justATree" |
|
94 | let x = this.position[0]; |
|
95 | let y = this.position[1]; |
|
96 | let z = this.position[2]; |
|
97 | ||
98 | setBlock(x, y, z, 0); |
|
108 | // the "this" keyword refers to the object it is inside to, in this case the "justATree" but since its not yet defined as that we use it |
|
109 | setBlock(this.x, this.y, this.z, 0); |
|
99 | 110 | } |
100 | 111 | } |
101 | 112 | ``` |