jecs/docs/learn/overview/get-started.md
Nidoxs 1a7b1b02b6
Some checks are pending
analysis / Run Luau Analyze (push) Waiting to run
deploy-docs / build (push) Waiting to run
deploy-docs / Deploy (push) Blocked by required conditions
publish-npm / publish (push) Waiting to run
unit-testing / Run Luau Tests (push) Waiting to run
Update Jecs version to latest in get-started.md (#199)
2025-02-23 18:34:53 +01:00

2.7 KiB

Getting Started

Installation

Installing Standalone

Navigate to the releases page and download jecs.rbxm from the assets.

jecs.rbxm

Using Wally

Add the following to your wally configuration:

::: code-group

jecs = "ukendio/jecs@0.5.3"

:::

Using npm (roblox-ts)

Use one of the following commands on your root project directory:

::: code-group

npm i https://github.com/Ukendio/jecs.git
yarn add https://github.com/Ukendio/jecs.git
pnpm add https://github.com/Ukendio/jecs.git

:::

Example Usage

::: code-group

local world = jecs.World.new()
local pair = jecs.pair
local Wildcard = jecs.Wildcard

local Name = world:component()

local function getName(e)
    return world:get(e, Name)
end

local Eats = world:component()

-- Relationship objects
local Apples = world:component()
-- components are entities, so you can add components to components
world:set(Apples, Name, "apples")
local Oranges = world:component()
world:set(Oranges, Name, "oranges")

local bob = world:entity()
-- Pairs can be constructed from two entities

world:set(bob, pair(Eats, Apples), 10)
world:set(bob, pair(Eats, Oranges), 5)
world:set(bob, Name, "bob")

local alice = world:entity()
world:set(alice, pair(Eats, Apples), 4)
world:set(alice, Name, "alice")

for id, amount in world:query(pair(Eats, Wildcard)) do
    -- get the second target of the pair
    local food = world:target(id, Eats)
    print(string.format("%s eats %d %s", getName(id), amount, getName(food)))
end

-- Output:
--   bob eats 10 apples
--   bob eats 5 pears
--   alice eats 4 apples  
import { Wildcard, pair, World } from "@rbxts/jecs"


const world = new World()
const Name = world.component()
function getName(e) {
  return world.get(e, Name)
}

const Eats = world.component()

// Relationship objects
const Apples = world.component()
// components are entities, so you can add components to components
world.set(Apples, Name, "apples")
const Oranges = world.component()
world.set(Oranges, Name, "oranges")

const bob = world.entity()
// Pairs can be constructed from two entities

world.set(bob, pair(Eats, Apples), 10)
world.set(bob, pair(Eats, Oranges), 5)
world.set(bob, Name, "bob")

const alice = world.entity()
world.set(alice, pair(Eats, Apples), 4)
world.set(alice, Name, "alice")

for (const [id, amount] of world.query(pair(Eats, Wildcard))) {
  // get the second target of the pair
  const food = world:target(id, Eats)
  print(string.format("%s eats %d %s", getName(id), amount, getName(food)))
}

// Output:
//   bob eats 10 apples
//   bob eats 5 pears
//   alice eats 4 apples