Skip to content

Commit ee3ec95

Browse files
committed
editor scripts
1 parent 2f07b3b commit ee3ec95

File tree

1 file changed

+140
-1
lines changed

1 file changed

+140
-1
lines changed

docs/en/manuals/editor-scripts.md

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,17 @@ You can interact with the editor using `editor` package that defines this API:
5656
- for atlas animations: `images` (same as `images` in atlas)
5757
- for tilemaps: `layers` (list of editor nodes for layers in the tilemap)
5858
- for tilemap layers: `tiles` (an unbounded 2d grid of tiles), see `tilemap.tiles.*` for more info
59+
- for particlefx: `emitters` (list of emitter editor nodes) and `modifiers` (list of modifier editor nodes)
60+
- for particlefx emitters: `modifiers` (list of modifier editor nodes)
61+
- for collision objects: `shapes` (list of collision shape editor nodes)
62+
- for GUI files: `layers` (list of layer editor nodes)
5963
- some properties that are shown in the Properties view when you have selected something in the Outline view. These types of outline properties supported:
6064
- `strings`
6165
- `booleans`
6266
- `numbers`
6367
- `vec2`/`vec3`/`vec4`
6468
- `resources`
65-
69+
- `curves`
6670
Please note that some of these properties might be read-only, and some might be unavailable in different contexts, so you should use `editor.can_get` before reading them and `editor.can_set` before making editor set them. Hover over property name in Properties view to see a tooltip with information about how this property is named in editor scripts. You can set resource properties to `nil` by supplying `""` value.
6771
- `editor.can_get(node_id, property)` — check if you can get this property so `editor.get()` won't throw an error.
6872
- `editor.can_set(node_id, property)` — check if `editor.tx.set()` transaction step with this property won't throw an error.
@@ -269,6 +273,141 @@ editor.transact({
269273
})
270274
```
271275

276+
#### Editing particlefx
277+
278+
You can edit particlefx using `modifiers` and `emitters` properties. For example, adding a circle emitter with acceleration modifier is done like this:
279+
```lua
280+
editor.transact({
281+
editor.tx.add("/fire.particlefx", "emitters", {
282+
type = "emitter-type-circle",
283+
modifiers = {
284+
{type = "modifier-type-acceleration"}
285+
}
286+
})
287+
})
288+
```
289+
Many particlefx properties are curves or curve spreads (i.e. curve + some randomizer value). Curves are represented as a table with a non-empty list of `points`, where each point is a table with the following properties:
290+
- `x` - the x coordinate of the point, should start at 0 and end at 1
291+
- `y` - the value of the point
292+
- `tx` (0 to 1) and `ty` (-1 to 1) - tangents of the point. E.g., for an 80-degree angle, `tx` should be `math.cos(math.rad(80))` and `ty` should be `math.sin(math.rad(80))`.
293+
Curve spreads additionally have a `spread` number property.
294+
295+
For example, setting a particle lifetime alpha curve for an already existing emitter might look like this:
296+
```lua
297+
local emitter = editor.get("/fire.particlefx", "emitters")[1]
298+
editor.transact({
299+
editor.tx.set(emitter, "particle_key_alpha", { points = {
300+
{x = 0, y = 0, tx = 0.1, ty = 1}, -- start at 0, go up quickly
301+
{x = 0.2, y = 1, tx = 1, ty = 0}, -- reach 1 at 20% of a lifetime
302+
{x = 1, y = 0, tx = 1, ty = 0} -- slowly go down to 0
303+
}})
304+
})
305+
```
306+
Of course, it's also possible to use the `particle_key_alpha` key in a table when creating an emitter. Additionally, you can use a single number instead to represent a "static" curve.
307+
308+
#### Editing collision objects
309+
310+
In addition to default outline properties, collision objects define `shapes` node list property. Adding new collision shapes is done like this:
311+
```lua
312+
editor.transact({
313+
editor.tx.add("/hero.collisionobject", "shapes", {
314+
type = "shape-type-box" -- or "shape-type-sphere", "shape-type-capsule"
315+
})
316+
})
317+
```
318+
Shape's `type` property is required during creation and cannot be changed after the shape is added. There are 3 shape types:
319+
- `shape-type-box` - box shape with `dimensions` property
320+
- `shape-type-sphere` - sphere shape with `diameter` property
321+
- `shape-type-capsule` - capsule shape with `diameter` and `height` properties
322+
323+
#### Editing GUI files
324+
325+
In addition to outline properties, GUI nodes defines the following properties:
326+
- `layers` — list of layer editor nodes (reorderable)
327+
- `materials` — list of material editor nodes
328+
329+
It's possible to edit GUI layers using editor `layers` property, e.g.:
330+
```lua
331+
editor.transact({
332+
editor.tx.add("/main.gui", "layers", {name = "foreground"}),
333+
editor.tx.add("/main.gui", "layers", {name = "background"})
334+
})
335+
```
336+
Additionally, it's possible to reorder layers:
337+
```lua
338+
local fg, bg = table.unpack(editor.get("/main.gui", "layers"))
339+
editor.transact({
340+
editor.tx.reorder("/main.gui", "layers", {bg, fg})
341+
})
342+
```
343+
Similarly, fonts, materials, textures, and particlefxs are edited using `fonts`, `materials`, `textures`, and `particlefxs` properties:
344+
```lua
345+
editor.transact({
346+
editor.tx.add("/main.gui", "fonts", {font = "/main.font"}),
347+
editor.tx.add("/main.gui", "materials", {name = "shine", material = "/shine.material"}),
348+
editor.tx.add("/main.gui", "particlefxs", {particlefx = "/confetti.material"}),
349+
editor.tx.add("/main.gui", "textures", {texture = "/ui.atlas"})
350+
})
351+
```
352+
These properties don't support reordering.
353+
354+
Finally, you can edit GUI nodes using `nodes` list property, e.g.:
355+
```lua
356+
editor.transact({
357+
editor.tx.add("/main.gui", "nodes", {
358+
type = "gui-node-type-box",
359+
position = {20, 20, 20}
360+
}),
361+
editor.tx.add("/main.gui", "nodes", {
362+
type = "gui-node-type-template",
363+
template = "/button.gui"
364+
}),
365+
})
366+
```
367+
Built-in node types are:
368+
- `gui-node-type-box`
369+
- `gui-node-type-particlefx`
370+
- `gui-node-type-pie`
371+
- `gui-node-type-template`
372+
- `gui-node-type-text`
373+
374+
If you are using spine extension, you can also use `gui-node-type-spine` node type.
375+
376+
If the GUI file defines layouts, you can get and set the values from layouts using `layout:property` syntax, e.g.:
377+
```lua
378+
local node = editor.get("/main.gui", "nodes")[1]
379+
380+
-- GET:
381+
local position = editor.get(node, "position")
382+
pprint(position) -- {20, 20, 20}
383+
local landscape_position = editor.get(node, "Landscape:position")
384+
pprint(landscape_position) -- {20, 20, 20}
385+
386+
-- SET:
387+
editor.transact({
388+
editor.tx.set(node, "Landscape:position", {30, 30, 30})
389+
})
390+
pprint(editor.get(node, "Landscape:position")) -- {30, 30, 30}
391+
```
392+
393+
Layout properties that were set can be reset to their default values using `editor.tx.reset`:
394+
```lua
395+
print(editor.can_reset(node, "Landscape:position")) -- true
396+
editor.transact({
397+
editor.tx.reset(node, "Landscape:position")
398+
})
399+
```
400+
Template node trees can be read, but not edited — you can only set node properties of the template node tree:
401+
```lua
402+
local template = editor.get("/main.gui", "nodes")[2]
403+
print(editor.can_add(template, "nodes")) -- false
404+
local node_in_template = editor.get(template, "nodes")[1]
405+
editor.transact({
406+
editor.tx.set(node_in_template, "text", "Button text")
407+
})
408+
print(editor.can_reset(node_in_template, "text")) -- true (overrides a value in the template)
409+
```
410+
272411
### Use shell commands
273412

274413
Inside the `run` handler, you can write to files (using `io` module) and execute shell commands (using `editor.execute()` command). When executing shell commands, it's possible to capture the output of a shell command as a string and then use it in code. For example, if you want to make a command for formatting JSON that shells out to globally installed [`jq`](https://jqlang.github.io/jq/), you can write the following command:

0 commit comments

Comments
 (0)