2024-07-30 13:41:28 +00:00
|
|
|
# World
|
2024-07-26 02:36:30 +00:00
|
|
|
|
|
|
|
A World contains entities which have components. The World is queryable and can be used to get entities with a specific set of components.
|
|
|
|
|
|
|
|
## Functions
|
|
|
|
|
|
|
|
### new()
|
2024-07-28 00:39:19 +00:00
|
|
|
```luau
|
2024-07-26 02:36:30 +00:00
|
|
|
function World.new(): World
|
|
|
|
```
|
|
|
|
Creates a new world.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
::: code-group
|
|
|
|
|
|
|
|
```luau [luau]
|
|
|
|
local world = jecs.World.new()
|
|
|
|
```
|
|
|
|
|
|
|
|
```ts [typescript]
|
|
|
|
import { World } from "@rbxts/jecs";
|
|
|
|
|
|
|
|
const world = new World();
|
|
|
|
```
|
|
|
|
|
|
|
|
:::
|
|
|
|
|
|
|
|
## entity()
|
|
|
|
```luau
|
2024-07-28 00:39:19 +00:00
|
|
|
function World:entity(): Entity -- The new entit.
|
2024-07-26 02:36:30 +00:00
|
|
|
```
|
|
|
|
Creates a new entity.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
::: code-group
|
|
|
|
|
|
|
|
```luau [luau]
|
|
|
|
local entity = world:entity()
|
|
|
|
```
|
|
|
|
|
|
|
|
```ts [typescript]
|
|
|
|
const entity = world.entity();
|
|
|
|
```
|
|
|
|
|
2024-07-28 00:39:19 +00:00
|
|
|
::
|
|
|
|
:
|
2024-07-26 02:36:30 +00:00
|
|
|
|
2024-07-28 00:39:19 +00:00
|
|
|
### component()
|
2024-07-26 02:36:30 +00:00
|
|
|
```luau
|
2024-07-28 00:39:19 +00:00
|
|
|
function World:component<T>(): Entity<T> -- The new componen.
|
2024-07-26 02:36:30 +00:00
|
|
|
```
|
|
|
|
Creates a new component.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
::: code-group
|
|
|
|
|
|
|
|
```luau [luau]
|
|
|
|
local Health = world:component() :: jecs.Entity<number>
|
|
|
|
```
|
|
|
|
|
|
|
|
```ts [typescript]
|
|
|
|
const Health = world.component<number>();
|
|
|
|
```
|
|
|
|
:::
|
|
|
|
|
|
|
|
::: info
|
|
|
|
You should use this when creating components.
|
|
|
|
|
|
|
|
For example, a Health type should be created using this.
|
|
|
|
:::
|
2024-07-28 00:39:19 +00:00
|
|
|
|
|
|
|
### get()
|
|
|
|
```luau
|
|
|
|
function World:get(
|
|
|
|
entity: Entity, -- The entity
|
|
|
|
...: Entity<T> -- The types to fetch
|
|
|
|
): ... -- Returns the component data in the same order they were passed in
|
|
|
|
```
|
|
|
|
Returns the data for each provided type for the corresponding entity.
|
|
|
|
|
|
|
|
:::
|
|
|
|
|
|
|
|
### add()
|
|
|
|
```luau
|
|
|
|
function World:add(
|
|
|
|
entity: Entity, -- The entity
|
|
|
|
id: Entity<T> -- The component ID to add
|
|
|
|
): ()
|
|
|
|
```
|
|
|
|
Adds a component ID to the entity.
|
|
|
|
|
|
|
|
This operation adds a single (component) id to an entity.
|
|
|
|
|
|
|
|
::: info
|
|
|
|
This function is idempotent, meaning if the entity already has the id, this operation will have no side effects.
|
|
|
|
:::
|
|
|
|
|
|
|
|
|
|
|
|
### set()
|
|
|
|
```luau
|
|
|
|
function World:set(
|
|
|
|
entity: Entity, -- The entity
|
|
|
|
id: Entity<T>, -- The component ID to set
|
|
|
|
data: T -- The data of the component's type
|
|
|
|
): ()
|
|
|
|
```
|
|
|
|
Adds or changes the entity's component.
|
|
|
|
|
|
|
|
### query()
|
|
|
|
```luau
|
|
|
|
function World:query(
|
2024-07-30 13:41:28 +00:00
|
|
|
...: Entity -- The IDs to query with
|
|
|
|
): Query -- Returns the Query
|
2024-07-28 00:39:19 +00:00
|
|
|
```
|
2024-07-30 13:41:28 +00:00
|
|
|
Creates a [`query`](query) with the given IDs. Entities that satisfies the conditions of the query will be returned and their corresponding data.
|
2024-07-28 00:39:19 +00:00
|
|
|
|
|
|
|
Example:
|
|
|
|
::: code-group
|
|
|
|
|
|
|
|
```luau [luau]
|
|
|
|
for id, position, velocity in world:query(Position, Velocity) do
|
|
|
|
-- Do something
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
```ts [typescript]
|
|
|
|
for (const [id, position, velocity] of world.query(Position, Velocity) {
|
|
|
|
// Do something
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
:::
|
2024-07-30 13:41:28 +00:00
|
|
|
|
|
|
|
:::info
|
|
|
|
Queries are uncached by default, this is generally very cheap unless you have high fragmentation from e.g. relationships.
|
|
|
|
:::
|