Skip to content

Commit 43bea76

Browse files
committed
Migrate flecs script tutorial to v4
1 parent b6181db commit 43bea76

13 files changed

+69
-39
lines changed

docs/FlecsScriptTutorial.md

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
This tutorial hasn't been updated for v4 yet! Use the [Script Manual](FlecsScript.md) for now.
2-
31
# Flecs Script Tutorial
42
This tutorial shows you how to use Flecs script, which is a declarative language for creating entities without having to write and compile code. Flecs script can be used for different things, such as building scenes, assets, or as a language for configuration files.
53

@@ -20,28 +18,28 @@ The page should look similar to this:
2018

2119
The panel on the left is the entity treeview, which shows all of the entities in our scene. The center view is the canvas, which shows us the renderable entities of our scene (more on that later). Finally, the pane on the right is the editor, where we can write flecs scripts.
2220

23-
At any point in time you can disable panels by clicking on the "x" in the top-right corner. Panels can be brought back by pressing on their button in the menu bar on the left.
21+
At any point in time you can disable panels by clicking on the button in the top-right corner. Panels can be brought back by pressing on their button in the menu bar on the left.
2422

2523
One other important thing is the link button in the top-right of the screen. You can use that button to obtain a link to your content, which is a good way to save progress, or to share what you've created with other people.
2624

2725
## The Basics
2826
Currently the explorer is showing the default scene. Let's clear it by removing all code from the editor. You should now see an empty canvas:
2927
![explorer with empty canvas](img/script_tutorial/tut_playground_empty.png)
3028

31-
Lets create an entity by typing its name into the editor:
29+
Lets create an entity by typing its name into the editor followed by an empty scope:
3230

3331
```js
34-
my_entity
32+
my_entity {}
3533
```
3634

3735
Notice that as we are typing the entity shows up in the treeview:
38-
[![explorer with a single entity](img/script_tutorial/tut_playground_entity.png)](https://www.flecs.dev/explorer/?wasm=https://www.flecs.dev/explorer/playground.js&script=%0Amy_entity%0A)
36+
![explorer with a single entity](img/script_tutorial/tut_playground_entity.png)
3937

4038
Entities are automatically created if they did not exist yet. Try entering the same entity twice:
4139

4240
```js
43-
my_entity
44-
my_entity
41+
my_entity {}
42+
my_entity {}
4543
```
4644

4745
Only one entity shows up in the treeview. The second time `my_entity` was parsed it already existed, so nothing needed to be done.
@@ -50,38 +48,60 @@ Only one entity shows up in the treeview. The second time `my_entity` was parsed
5048
Now that we have an entity, let's add a few components and tags to it. Change the text in the editor to this, to create an entity with a tag called `SpaceShip`:
5149

5250
```js
53-
my_entity :- SpaceShip
51+
my_entity { SpaceShip }
5452
```
5553

56-
Note that we didn't have to explicitly declare `SpaceShip` in advance, and that it also shows up as entity in the treeview. We can add multiple things to an entity this way:
54+
Note that this alone doesn't work as we didn't explicitly declare `SpaceShip` in advance, to fix this we must first declare `SpaceShip` as an empty entity, which is equivalent to a tag:
55+
```js
56+
SpaceShip {}
5757

58+
my_entity { SpaceShip }
59+
```
60+
We can add multiple tags to an entity:
5861
```js
59-
my_entity :- SpaceShip
60-
my_entity :- FasterThanLight
62+
SpaceShip {}
63+
FasterThanLight {}
64+
65+
my_entity { SpaceShip }
66+
my_entity { FasterThanLight }
6167
```
6268

63-
To avoid repeating the entity name many times, we can use the `{}` operators to open a scope for the entity. Inside the scope we can list components for the entity by prefixing them with a dash (`-`):
69+
To avoid repeating the entity name many times, we can use the `{}` operators to open a scope for the entity. Inside the scope we can list components for the entity by placing them on a new line:
70+
71+
```js
72+
SpaceShip {}
73+
FasterThanLight {}
6474

75+
my_entity {
76+
SpaceShip
77+
FasterThanLight
78+
}
79+
```
80+
Or separating them with the `;` operator:
6581
```js
82+
SpaceShip {}
83+
FasterThanLight {}
84+
6685
my_entity {
67-
- SpaceShip
68-
- FasterThanLight
86+
SpaceShip; FasterThanLight
6987
}
7088
```
7189

7290
We can inspect the entity and its contents by opening it in the entity inspector. To do this, click on the entity in the treeview. You should now see this:
7391

74-
[![entity with two tags](img/script_tutorial/tut_playground_inspector.png)](https://www.flecs.dev/explorer/?local=true&wasm=https://www.flecs.dev/explorer/playground.js&script=%0Amy_entity%20%7B%0A%20%20-%20SpaceShip%0A%20%20-%20FasterThanLight%0A%7D%0A&entity=my_entity)
92+
![entity with two tags](img/script_tutorial/tut_playground_inspector.png)
7593

76-
Note how the `SpaceShip` and `FasterThanLight` tags show up in the editor. There is also a `Script: main` tag, which exists for Flecs to keep track of which entities got created by our script.
94+
Note how the `SpaceShip` and `FasterThanLight` tags show up in the editor. There is also a `Script -> main` tag, which exists for Flecs to keep track of which entities got created by our script.
95+
96+
We can return to the script editor by clicking on the drop-down above the entity treeview, selecting `scripts` and then clicking on our script, `scene.flecs` in the list.
7797

7898
Adding a component is similar to adding tag with a value. Let's add the `Position3` component from the `flecs.components.transform` module which comes preloaded with the playground. Note how it also shows up in the inspector when we add this code:
7999

80100
```js
81101
my_entity {
82-
- SpaceShip
83-
- FasterThanLight
84-
- flecs.components.transform.Position3{1, 2, 3}
102+
SpaceShip
103+
FasterThanLight
104+
flecs.components.transform.Position3: {1, 2, 3}
85105
}
86106
```
87107

@@ -95,35 +115,39 @@ We can now use the component without the module name, which looks much cleaner:
95115

96116
```js
97117
my_entity {
98-
- SpaceShip
99-
- FasterThanLight
100-
- Position3{1, 2, 3}
118+
SpaceShip
119+
FasterThanLight
120+
Position3{1, 2, 3}
101121
}
102122
```
103123

104124
If all went well, the playground should now look like this:
105125

106-
[![entity with two tags and a component](img/script_tutorial/tut_playground_component.png)](https://www.flecs.dev/explorer/?local=true&wasm=https://www.flecs.dev/explorer/playground.js&script=using%20flecs.components.*%0A%0Amy_entity%20%7B%0A%20%20-%20SpaceShip%0A%20%20-%20FasterThanLight%0A%20%20-%20Position3%7B1%2C%202%2C%203%7D%0A%7D%0A&entity=my_entity)
126+
![entity with two tags and a component](img/script_tutorial/tut_playground_component.png)
107127

108-
Note how after we added the `Position3` component, the inspector also shows the `Transform` and `WorldCell` components. This happens because the playground imports modules that implement world partitioning and transforms, which we get for free just by using `flecs.components.transform.Position3` component.
128+
Note how after we added the `Position3` component, the inspector also shows the `Transform` component. This happens because the playground imports modules that implement transforms, which we get for free just by using `flecs.components.transform.Position3` component.
109129

110130
In addition to components and tags we can also add relationship pairs to entities. To add a pair, add this line to the scope of the entity, and note how it shows up in the inspector:
111131

112132
```js
113-
- (OwnedBy, Player)
133+
(OwnedBy, Player)
114134
```
115135

116-
Entities can be created in hierarchies. A child entity is created in the scope of an entity just like the components and tags, but without the preceding `-`. Add this to the scope of the entity:
136+
Note: you will also need to declare entities for `OwnedBy` and `Player`.
137+
138+
Entities can be created in hierarchies. A child entity is created in the scope of an entity just like the components and tags, but followed by a scope. Add this to the scope of the entity:
117139

118140
```js
119141
cockpit {
120-
pilot :- (Faction, Earth)
142+
pilot { (Faction, Earth) }
121143
}
122144
```
123145

146+
Note: you will also need to declare entities for `Faction` and `Earth`.
147+
124148
You can see the hierarchy this created in the treeview by expanding `my_entity`:
125149

126-
[![entity with hierarchy](img/script_tutorial/tut_playground_hierarchy.png)](https://www.flecs.dev/explorer/?local=true&wasm=https://www.flecs.dev/explorer/playground.js&script=using%20flecs.components.*%0A%0Amy_entity%20%7B%0A%20%20-%20SpaceShip%0A%20%20-%20FasterThanLight%0A%20%20-%20Position3%7B1%2C%202%2C%203%7D%0A%20%20%0A%20%20cockpit%20%7B%0A%20%20%20%20pilot%20%3A-%20(Faction%2C%20Earth)%0A%20%20%7D%0A%7D%0A&entity=my_entity)
150+
![entity with hierarchy](img/script_tutorial/tut_playground_hierarchy.png)
127151

128152
Congratulations! You now know how to create entities, hierarchies, and how to add components and tags. None of the entities we created so far are visible in the canvas however, so lets do something about that.
129153

@@ -146,14 +170,20 @@ Now add these lines into the editor to create our ground `plane`:
146170

147171
```js
148172
plane {
149-
- Position3{}
173+
Position3: {}
150174
Rectangle: {100, 100}
151-
- Rgb{0.9, 0.9, 0.9}
175+
Rgb: {0.9, 0.9, 0.9}
152176
}
153177
```
154178

155179
Something happened! But it doesn't look quite right:
156180

181+
![an extra ground plane](img/script_tutorial/tut_playground_ground_plane_disable.png)
182+
183+
The playground comes with a ground plane already called `ground_plane`, find it in the entity treeview and disable it by clicking the arrow in the top right of the inspector and then the `Disable` button.
184+
185+
With that out of the way, our ground plane still doesn't look quite right:
186+
157187
[![a buggy ground plane](img/script_tutorial/tut_playground_plane_wrong.png)](https://www.flecs.dev/explorer/?local=true&wasm=https://www.flecs.dev/explorer/playground.js&script=using%20flecs.components.*%0A%0Aplane%20%7B%0A%20%20-%20Position3%7B%7D%0A%20%20-%20Rectangle%7B100%2C%20100%7D%0A%20%20-%20Rgb%7B0.9%2C%200.9%2C%200.9%7D%0A%7D%0A)
158188

159189
The rectangle is rotated the wrong way for our plane. To fix this we need to rotate it 90 degrees or `π/2` radians on the x axis. First lets define `π` as a constant value in our script:
@@ -165,25 +195,25 @@ const PI = 3.1415926
165195
Now add this line to the scope of `plane`:
166196

167197
```js
168-
- Rotation3{$PI / 2}
198+
Rotation3: {$PI / 2}
169199
```
170200

171201
That looks better:
172202

173-
[![a ground plane](img/script_tutorial/tut_playground_plane_rotated.png)](https://www.flecs.dev/explorer/?local=true&wasm=https://www.flecs.dev/explorer/playground.js&script=using%20flecs.components.*%0A%0Aconst%20PI%20%3D%203.1415926%0A%0Aplane%20%7B%0A%20%20-%20Position3%7B%7D%0A%20%20-%20Rotation3%7B%24PI%20%2F%202%7D%0A%20%20-%20Rectangle%7B100%2C%20100%7D%0A%20%20-%20Rgb%7B0.9%2C%200.9%2C%200.9%7D%0A%7D%0A)
203+
![a ground plane](img/script_tutorial/tut_playground_plane_rotated.png)
174204

175205
Let's increase the sides of the plane to `10000` so that the fog effect makes it blends in with the background, which gives the illusion of a horizon:
176206

177-
[![a ground plane](img/script_tutorial/tut_playground_plane.png)](https://www.flecs.dev/explorer/?local=true&wasm=https://www.flecs.dev/explorer/playground.js&script=using%20flecs.components.*%0A%0Aconst%20PI%20%3D%203.1415926%0A%0Aplane%20%7B%0A%20%20-%20Position3%7B%7D%0A%20%20-%20Rotation3%7B%24PI%20%2F%202%7D%0A%20%20-%20Rectangle%7B10000%2C%2010000%7D%0A%20%20-%20Rgb%7B0.9%2C%200.9%2C%200.9%7D%0A%7D%0A)
207+
![a ground plane](img/script_tutorial/tut_playground_plane.png)
178208

179209
Note that the `PI` variable does not show up in the treeview. Variables do not create entities, and only exist within the context of a script.
180210

181211
Let's now add a cube to the scene. The code for this looks similar to the plane:
182212

183213
```js
184214
box {
185-
- Position3{}
186-
Box: {10, 10, 10}
215+
Position3: {}
216+
Box: {3, 3, 3}
187217
Rgb: {1, 0, 0}
188218
}
189219
```
@@ -196,15 +226,15 @@ To fix this, we can move it up by setting the `y` member of `Position3` to half
196226

197227
```js
198228
box {
199-
- Position3{y: 5}
200-
Box: {10, 10, 10}
229+
Position3: {y: 1.5}
230+
Box: {3, 3, 3}
201231
Rgb: {1, 0, 0}
202232
}
203233
```
204234

205235
Now the entire cube is visible, and should look like this:
206236

207-
[![a cube](img/script_tutorial/tut_playground_box.png)](https://www.flecs.dev/explorer/?local=true&wasm=https://www.flecs.dev/explorer/playground.js&script=using%20flecs.components.*%0A%0Aconst%20PI%20%3D%203.1415926%0A%0Aplane%20%7B%0A%20%20-%20Position3%7B%7D%0A%20%20-%20Rotation3%7B%24PI%20%2F%202%7D%0A%20%20-%20Rectangle%7B10000%2C%2010000%7D%0A%20%20-%20Rgb%7B0.9%2C%200.9%2C%200.9%7D%0A%7D%0A%0Abox%20%7B%0A%20%20-%20Position3%7By%3A%205%7D%0A%20%20-%20Box%7B10%2C%2010%2C%2010%7D%0A%20%20-%20Rgb%7B1%2C%200%2C%200%7D%0A%7D%0A)
237+
![a cube](img/script_tutorial/tut_playground_box.png)
208238

209239
We now have all of the basic knowledge to start drawing a fence! Note that if you want to move the camera around, first click on the canvas to give it focus. You can now move the camera around. To release focus from the canvas, click on it again (the green border should disappear).
210240

-173 KB
Loading
-63.8 KB
Loading
-103 KB
Loading
-53.4 KB
Loading
-570 KB
Loading
104 KB
Loading
-74.9 KB
Loading
-103 KB
Loading
-76.8 KB
Loading

0 commit comments

Comments
 (0)