jecs/docs/learn/overview/get-started.md

200 lines
4.2 KiB
Markdown
Raw Normal View History

2025-02-19 16:10:38 +00:00
# Getting Started with Jecs
2025-02-19 16:10:38 +00:00
This guide will help you get Jecs up and running in your project.
2025-02-19 16:10:38 +00:00
## Installation
2025-02-19 16:10:38 +00:00
Choose your preferred installation method:
2025-02-19 16:10:38 +00:00
### Using Wally (Luau)
2025-02-19 16:10:38 +00:00
Add to your wally.toml:
::: code-group
```toml [wally.toml]
2025-02-19 16:10:38 +00:00
[dependencies]
jecs = "ukendio/jecs@0.2.3"
```
:::
2025-02-19 16:10:38 +00:00
Then run:
```bash
wally install
```
2025-02-19 16:10:38 +00:00
### Using npm (roblox-ts)
2025-02-19 16:10:38 +00:00
Use your preferred package manager:
::: code-group
```bash [npm]
npm i https://github.com/Ukendio/jecs.git
```
```bash [yarn]
yarn add https://github.com/Ukendio/jecs.git
```
```bash [pnpm]
pnpm add https://github.com/Ukendio/jecs.git
```
:::
2025-02-19 16:10:38 +00:00
### Standalone Installation
1. Navigate to the [releases page](https://github.com/Ukendio/jecs/releases)
2. Download `jecs.rbxm` from the assets
3. Import into your Roblox project
![jecs.rbxm](rbxm.png)
## Basic Usage
Here's a simple example to get you started:
::: code-group
2025-02-19 16:10:38 +00:00
```lua [luau]
local jecs = require(path.to.jecs)
2025-02-19 16:10:38 +00:00
-- Create a world
local world = jecs.World.new()
2025-02-19 16:10:38 +00:00
-- Define components
local Position = world:component() :: jecs.Entity<Vector3>
local Velocity = world:component() :: jecs.Entity<Vector3>
-- Create an entity
local entity = world:entity()
2025-02-19 16:10:38 +00:00
-- Add components
world:set(entity, Position, Vector3.new(0, 0, 0))
world:set(entity, Velocity, Vector3.new(1, 0, 0))
-- Update system
local function updatePositions()
for id, position, velocity in world:query(Position, Velocity) do
world:set(id, Position, position + velocity)
end
end
2025-02-19 16:10:38 +00:00
```
```typescript [typescript]
import { World } from "@rbxts/jecs";
// Create a world
const world = new World();
// Define components
const Position = world.component<Vector3>();
const Velocity = world.component<Vector3>();
// Create an entity
const entity = world.entity();
// Add components
world.set(entity, Position, new Vector3(0, 0, 0));
world.set(entity, Velocity, new Vector3(1, 0, 0));
2025-02-19 16:10:38 +00:00
// Update system
function updatePositions() {
for (const [id, position, velocity] of world.query(Position, Velocity)) {
world.set(id, Position, position.add(velocity));
}
}
```
:::
## Advanced Example: Relationships
Here's an example showing Jecs' relationship features:
::: code-group
```lua [luau]
local world = jecs.World.new()
local pair = jecs.pair
local Wildcard = jecs.Wildcard
-- Define components
local Name = world:component()
local Eats = world:component()
2025-02-19 16:10:38 +00:00
-- Create food types (components are entities!)
local Apples = world:component()
world:set(Apples, Name, "apples")
local Oranges = world:component()
world:set(Oranges, Name, "oranges")
2025-02-19 16:10:38 +00:00
-- Create entities with relationships
local bob = world:entity()
2025-02-19 16:10:38 +00:00
world:set(bob, Name, "bob")
world:set(bob, pair(Eats, Apples), 10)
world:set(bob, pair(Eats, Oranges), 5)
local alice = world:entity()
world:set(alice, Name, "alice")
2025-02-19 16:10:38 +00:00
world:set(alice, pair(Eats, Apples), 4)
2025-02-19 16:10:38 +00:00
-- Query relationships
for id, amount in world:query(pair(Eats, Wildcard)) do
local food = world:target(id, Eats)
2025-02-19 16:10:38 +00:00
print(string.format("%s eats %d %s",
world:get(id, Name),
amount,
world:get(food, Name)))
end
-- Output:
-- bob eats 10 apples
2025-02-19 16:10:38 +00:00
-- bob eats 5 oranges
-- alice eats 4 apples
```
2025-02-19 16:10:38 +00:00
:::
2025-02-19 16:10:38 +00:00
## Key Features
2025-02-19 16:10:38 +00:00
### Entity Management
```lua
-- Create entities
local entity = world:entity()
2025-02-19 16:10:38 +00:00
-- Delete entities
world:delete(entity)
2025-02-19 16:10:38 +00:00
-- Clear all components
world:clear(entity)
```
2025-02-19 16:10:38 +00:00
### Component Operations
```lua
-- Add component (tag)
world:add(entity, IsEnemy)
2025-02-19 16:10:38 +00:00
-- Set component value
world:set(entity, Health, 100)
2025-02-19 16:10:38 +00:00
-- Get component value
local health = world:get(entity, Health)
2025-02-19 16:10:38 +00:00
-- Remove component
world:remove(entity, Health)
```
2025-02-19 16:10:38 +00:00
### Relationships
```lua
-- Create parent-child relationship
world:add(child, pair(jecs.ChildOf, parent))
2025-02-19 16:10:38 +00:00
-- Query children
for child in world:query(pair(jecs.ChildOf, parent)) do
-- Process child entities
end
```
2025-02-19 16:10:38 +00:00
## Next Steps
2025-02-19 16:10:38 +00:00
1. Check out the [First Jecs Project](first-jecs-project.md) tutorial
2. Learn about [Entities and Components](../concepts/entities-and-components.md)
3. Explore [Queries](../concepts/queries.md) and [Relationships](../concepts/relationships.md)
4. Browse the [API Reference](../../api/jecs.md)
## Getting Help
2025-02-19 16:10:38 +00:00
- Join our [Discord server](https://discord.gg/h2NV8PqhAD)
- Check the [FAQ](../faq/common-issues.md)
- Report issues on [GitHub](https://github.com/ukendio/jecs/issues)