|
1 | 1 | --- |
2 | 2 | sidebar_position: 2 |
| 3 | +title: Entities |
3 | 4 | --- |
4 | 5 |
|
5 | 6 | # Entities |
6 | 7 |
|
7 | | -Entities are the building blocks of games. They are similar to Unity's `GameObject` or Godot's `Node`. They implement |
8 | | -basic functionality, support nesting and hierarchy, and can be scripted using Behavior classes. |
| 8 | +Entities are Dreamlab's fundamental game objects—similar to Unity **GameObjects** or Godot **Nodes**. |
| 9 | +They |
9 | 10 |
|
10 | | -Entities can be spawned using the right-click menu in the Scene Graph: |
11 | | - |
| 11 | +* own a **Transform** (position / rotation / scale / z-layer) |
| 12 | +* can nest to form a hierarchy |
| 13 | +* gain behavior through attached **Behavior** scripts |
| 14 | +* sync automatically when placed under the shared **`world`** root |
12 | 15 |
|
13 | | -## Entity Catalog |
| 16 | +Entities are usually added from the editor's right-click menu: |
14 | 17 |
|
15 | | -:::note |
| 18 | + |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## 1. Quick start - spawning by script |
| 23 | + |
| 24 | +### Create a brand-new entity |
| 25 | + |
| 26 | +```ts |
| 27 | +// Spawn an Empty into the shared world root |
| 28 | +const pivot = game.world.spawn({ |
| 29 | + name: "Pivot", |
| 30 | + type: Empty, |
| 31 | + transform: { position: { x: 4, y: 1 } }, |
| 32 | +}); |
| 33 | +``` |
| 34 | + |
| 35 | +### Clone a prefab at runtime |
| 36 | + |
| 37 | +```ts |
| 38 | +// “Crate” lives under prefabs/ |
| 39 | +// cloneInto returns the new instance |
| 40 | +const crate = game.prefabs._.Crate.cloneInto(game.world, { |
| 41 | + name: "Crate." + crypto.randomUUID(), |
| 42 | + authority: game.network.self, // I control this crate |
| 43 | +}); |
| 44 | +crate.pos.set(2, 0); |
| 45 | +``` |
| 46 | + |
| 47 | +> `cloneInto()` duplicates the entity (plus all children & behaviors) and registers it in the target root. |
| 48 | +
|
| 49 | +--- |
| 50 | + |
| 51 | +## 2. Core concepts |
| 52 | + |
| 53 | +| Feature | What / Why | |
| 54 | +| ------------------ | --------------------------------------------------------------------------------------------------------------- | |
| 55 | +| **Roots** | Decide *where* an entity exists. See the **[Scenes & Roots](./scenes-and-roots.mdx)** page. | |
| 56 | +| **Behaviors** | Add logic to the entity. See the **[Behaviors](./behaviors.mdx)** page. | |
| 57 | +| **Authority** | Peer that owns simulation authority → `entity.authority` or `takeAuthority()`. | |
| 58 | +| **Events** | Pub-sub events such as `EntityDestroyed`, `EntityChildSpawned`. | |
| 59 | +| **Values** | Strong-typed, optionally replicated props. See the **[Synced Values and Adapters](./synced-values.mdx)** page. | |
| 60 | +| **Transform API** | `entity.transform` or shortcuts `entity.pos`, `entity.z`. | |
| 61 | + |
| 62 | +### Accessing entities by name |
| 63 | + |
| 64 | +`root._.<Name>` gives you a quick reference to any **direct child** under a root: |
| 65 | + |
| 66 | +```ts |
| 67 | +const cam = game.local._.Camera; // generic Entity |
| 68 | +``` |
16 | 69 |
|
17 | | -This list may not always be up-to-date. Refer to the Dreamlab Editor for a complete list of entities. |
| 70 | +The proxy returns an **untyped** `Entity`, so if you want type-specific methods |
| 71 | +(e.g. `Camera.zoom`), cast it: |
18 | 72 |
|
| 73 | +```ts |
| 74 | +// get the local camera with full IntelliSense |
| 75 | +const cam = game.local._.Camera.cast(Camera); |
| 76 | + |
| 77 | +cam.zoom = 2; // Camera-only API |
| 78 | +``` |
| 79 | + |
| 80 | +Dot-path chains work for grandchildren too: |
| 81 | + |
| 82 | +```ts |
| 83 | +// Prefab » Player » Health |
| 84 | +const health = game.prefabs._.Player._.Health.cast(RichText); |
| 85 | +health.text = "HP: 100"; |
| 86 | +``` |
| 87 | + |
| 88 | +> The lookup throws if the child name is wrong, so check your spelling against the scene graph panel. |
| 89 | +
|
| 90 | +--- |
| 91 | + |
| 92 | +## 3. Entity catalog (common types) |
| 93 | + |
| 94 | +:::note |
| 95 | +This table is a quick reference—open the editor for the full, up-to-date list. |
19 | 96 | ::: |
20 | 97 |
|
21 | 98 | ### General |
22 | 99 |
|
23 | | -| Entity | Usage | |
24 | | -| --------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | |
25 | | -| `AudioSource` | Play an audio clip. Optionally enable positional audio relative to the camera | |
26 | | -| `Camera` | Refer to the dedicated [Camera page](./camera.mdx) of the engine manual | |
27 | | -| `CharacterController` | Collider with dedicated character movement checks that prevent collision | |
28 | | -| `Clickable` | Entity that emits a Signal when clicked on | |
29 | | -| `Collider` | Solid collision for character controllers and other physics bodies | |
30 | | -| `ComplexCollider` | Complex collision shape. Attach three or more empties as children for this to work. Forms a polygon from the children in CCW order | |
31 | | -| `Empty` | Does nothing. Useful for attaching manager behaviors or marking positions (eg: spawnpoints) | |
32 | | -| `RichText` | Draw Pixi.js Rich Text in the world | |
33 | | -| `Rigidbody` | Entity that has simulated physics. Attach one or more colliders as children for this to work. | |
| 100 | +| Entity | Purpose | |
| 101 | +| --------------------- | ------------------------------------------------------------ | |
| 102 | +| `AudioSource` | Play sounds (optionally positional). | |
| 103 | +| `Camera` | Viewport controller—see the [Camera](./camera.mdx) page. | |
| 104 | +| `CharacterController` | Capsule collider with built-in movement checks. | |
| 105 | +| `Clickable` | Emits a signal when clicked. | |
| 106 | +| `Collider` | Basic physics collider. | |
| 107 | +| `ComplexCollider` | Polygon collider formed by 3 + child empties. | |
| 108 | +| `Empty` | Transform only—good for markers, parents, manager scripts. | |
| 109 | +| `RichText` | Draw PIXI rich text in-world. | |
| 110 | +| `Rigidbody` | Simulated physics body; attach one or more colliders. | |
| 111 | +| `Tilemap` | Create [Tilemaps](./tilemaps.mdx) using a predefined tileset | |
| 112 | + |
34 | 113 |
|
35 | 114 | ### Sprites |
36 | 115 |
|
37 | | -| Entity | Usage | |
38 | | -| ---------------- | -------------------------------------------------------------------------------------- | |
39 | | -| `AnimatedSprite` | Draws a JSON spritesheet as an animated image with configurable speed and frame range. | |
40 | | -| `Sprite` | Draw an image in the world | |
41 | | -| `TilingSprite` | Same as Sprite but with tiling options | |
| 116 | +| Entity | Purpose | |
| 117 | +| ---------------- | ---------------------------------------------------- | |
| 118 | +| `AnimatedSprite` | JSON spritesheet playback with speed / frame control | |
| 119 | +| `Sprite` | Draw a single image | |
| 120 | +| `TilingSprite` | Sprite that tiles its texture | |
42 | 121 |
|
43 | 122 | ### UI |
44 | 123 |
|
45 | | -:::info |
| 124 | +| Entity | Purpose | |
| 125 | +| --------- | -------------------------------------------------------------- | |
| 126 | +| `UILayer` | Full-screen DOM tree—ideal for HUDs | |
| 127 | +| `UIPanel` | DOM tree fixed in world space (nameplates, signs, etc.) | |
46 | 128 |
|
47 | | -Visit the [User Interface page](./user-interface.mdx) of the engine manual for more info on HTML UI. |
| 129 | +### Graphics |
48 | 130 |
|
49 | | -::: |
| 131 | +| Entity | Purpose | |
| 132 | +| --------------- | ---------------------------------------- | |
| 133 | +| `RawPixi` | Advanced: draw via PIXI `Graphics` API | |
| 134 | +| `ColoredSquare` | Solid-color rectangle | |
| 135 | +| `ColoredPolygon`| Solid-color regular polygon | |
50 | 136 |
|
51 | | -| Entity | Usage | |
52 | | -| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | |
53 | | -| `UILayer` | Create an HTML DOM tree that covers the entire game screen. Useful for static UI elements (eg: HUD) | |
54 | | -| `UIPanel` | Create an HTML DOM tree that exists in the world. Useful for UI elements that are fixed relative to the world (eg: signs, player nameplates, etc) | |
| 137 | +--- |
55 | 138 |
|
56 | | -### Graphics |
| 139 | +## 4. Common entity events |
| 140 | + |
| 141 | +Dreamlab uses events for entity lifecycle—subscribe with `entity.on(Event, handler)`. |
57 | 142 |
|
58 | | -| Entity | Usage | |
59 | | -| ---------------- | --------------------------------------------------------------- | |
60 | | -| `RawPixi` | **Advanced:** Draw using Pixi.js using their Graphics primitive | |
61 | | -| `ColoredSquare` | Render a solid color rectangle in the world | |
62 | | -| `ColoredPolygon` | Render a solid color regular polygon in the world | |
| 143 | +| Event | Fires when… | |
| 144 | +| ---------------------------- | -------------------------------------------------- | |
| 145 | +| `EntitySpawned` | the entity is first created (constructor done) | |
| 146 | +| `EntityDestroyed` | the entity is about to be removed | |
| 147 | +| `EntityChildSpawned` | a direct child is added | |
| 148 | +| `EntityDescendantDestroyed` | any level of descendant gets destroyed | |
| 149 | +| `EntityRenamed` | `entity.name` changes | |
| 150 | +| `EntityReparented` | `entity.parent` changes | |
| 151 | +| `EntityEnableChanged` | the effective `enabled` state toggles | |
0 commit comments