Compare commits

...

5 commits

Author SHA1 Message Date
Ukendio
0b6bfea5c8 Return nil if nth is over count
Some checks failed
analysis / Run Luau Analyze (push) Has been cancelled
deploy-docs / build (push) Has been cancelled
publish-npm / publish (push) Has been cancelled
unit-testing / Run Luau Tests (push) Has been cancelled
deploy-docs / Deploy (push) Has been cancelled
2025-08-02 23:57:50 +02:00
Ukendio
3cfce10a4a Increment component records after registering
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
2025-08-02 18:58:19 +02:00
Ukendio
add9ad3939 Support setting signal on cached Relation
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
2025-08-02 06:20:53 +02:00
Ukendio
4153a7cdfe Monitors and observers need to able to accept pair terms
Some checks failed
analysis / Run Luau Analyze (push) Has been cancelled
deploy-docs / build (push) Has been cancelled
publish-npm / publish (push) Has been cancelled
unit-testing / Run Luau Tests (push) Has been cancelled
deploy-docs / Deploy (push) Has been cancelled
2025-07-29 21:08:23 +02:00
Ukendio
4230a0a797 Version 2025-07-29 21:07:59 +02:00
8 changed files with 96 additions and 27 deletions

View file

@ -69,6 +69,9 @@ local function observers_new<T...>(
end
for _, term in terms do
if jecs.IS_PAIR(term) then
term = jecs.ECS_PAIR_FIRST(term)
end
world:added(term, emplaced)
world:changed(term, emplaced)
end
@ -151,7 +154,7 @@ local function monitors_new<T...>(
i += 1
entities[i] = entity
if callback ~= nil then
callback(entity, id, value)
callback(entity, jecs.OnAdd)
end
end
end
@ -164,6 +167,9 @@ local function monitors_new<T...>(
end
for _, term in terms do
if jecs.IS_PAIR(term) then
term = jecs.ECS_PAIR_FIRST(term)
end
world:added(term, emplaced)
world:removed(term, removed)
end

View file

@ -1,12 +1,11 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local jecs = require(ReplicatedStorage.ecs)
local schedule = require(ReplicatedStorage.schedule)
local observers_add = require(ReplicatedStorage.observers_add)
local SYSTEM = schedule.SYSTEM
local RUN = schedule.RUN
require(ReplicatedStorage.components)
local world = observers_add(jecs.world())
local world = jecs.world()
local systems = ReplicatedStorage.systems
SYSTEM(world, systems.receive_replication)

View file

@ -1,7 +1,6 @@
local jecs = require(game:GetService("ReplicatedStorage").ecs)
local observers_add = require("../ReplicatedStorage/observers_add")
export type World = typeof(observers_add(jecs.world()))
export type World = typeof(jecs.world())
export type Entity = jecs.Entity
export type Id<T> = jecs.Id<T>
export type Snapshot = {

View file

@ -24,10 +24,10 @@ export type Archetype = {
export type QueryInner = {
compatible_archetypes: { Archetype },
ids: { i53 },
filter_with: { i53 },
filter_without: { i53 },
next: () -> (number, ...any),
ids: { Id },
filter_with: { Id },
filter_without: { Id },
next: () -> (Entity, ...any),
world: World,
}
@ -54,6 +54,8 @@ export type Query<T...> = typeof(setmetatable(
archetypes: (self: Query<T...>) -> { Archetype },
cached: (self: Query<T...>) -> Query<T...>,
ids: { Id<any> },
patch: (self: Query<T...>, fn: (T...) -> (T...)) -> (),
view: (self: Query<T...>) -> View<T...>,
-- world: World
},
{} :: {
@ -68,6 +70,12 @@ export type Observer = {
query: QueryInner,
}
export type observer = {
callback: (archetype: archetype) -> (),
query: query,
}
type archetype = {
id: number,
types: { i53 },
@ -504,7 +512,7 @@ local function ecs_pair_second(world: world, e: i53)
return ecs_get_alive(world, obj)
end
local function query_match(query: QueryInner, archetype: archetype)
local function query_match(query: query, archetype: archetype)
local columns_map = archetype.columns_map
local with = query.filter_with
@ -526,7 +534,7 @@ local function query_match(query: QueryInner, archetype: archetype)
return true
end
local function find_observers(world: world, event: i53, component: i53): { Observer }?
local function find_observers(world: world, event: i53, component: i53): { observer }?
local cache = world.observable[event]
if not cache then
return nil
@ -725,7 +733,7 @@ local function world_target(world: world, entity: i53, relation: i53, index: num
local nth = index or 0
if nth >= count then
nth = nth + count + 1
return nil
end
nth = archetype.types[nth + idr.records[archetype_id]]
@ -871,6 +879,7 @@ local function archetype_create(world: world, id_types: { i53 }, ty, prev: i53?)
for i, component_id in archetype.types do
local idr = id_record_ensure(world, component_id)
idr.size += 1
local is_tag = bit32.btest(idr.flags, ECS_ID_IS_TAG)
local column = if is_tag then NULL_ARRAY else {}
columns[i] = column
@ -882,11 +891,13 @@ local function archetype_create(world: world, id_types: { i53 }, ty, prev: i53?)
local object = ECS_PAIR_SECOND(component_id)
local r = ECS_PAIR(relation, EcsWildcard)
local idr_r = id_record_ensure(world, r)
idr_r.size += 1
archetype_append_to_records(idr_r, archetype_id, columns_map, r, i, column)
local t = ECS_PAIR(EcsWildcard, object)
local idr_t = id_record_ensure(world, t)
idr_t.size += 1
archetype_append_to_records(idr_t, archetype_id, columns_map, t, i, column)
end
@ -2615,7 +2626,7 @@ local function world_new()
table.insert(listeners, existing_hook)
end
local idr = world.component_index[component]
local idr = component_index[ECS_PAIR(component, EcsWildcard)] or component_index[component]
if idr then
idr.on_add = on_add
else
@ -2650,7 +2661,7 @@ local function world_new()
table.insert(listeners, existing_hook)
end
local idr = world.component_index[component]
local idr = component_index[ECS_PAIR(component, EcsWildcard)] or component_index[component]
if idr then
idr.on_change = on_change
else
@ -2681,7 +2692,7 @@ local function world_new()
table.insert(listeners, existing_hook)
end
local idr = world.component_index[component]
local idr = component_index[ECS_PAIR(component, EcsWildcard)] or component_index[component]
if idr then
idr.on_remove = on_remove
else
@ -2748,7 +2759,7 @@ local function world_new()
local nth = index or 0
if nth >= count then
nth = nth + count + 1
return nil
end
nth = archetype.types[nth + idr.records[archetype_id]]

View file

@ -1,6 +1,6 @@
{
"name": "@rbxts/jecs",
"version": "0.9.0-rc.6",
"version": "0.9.0-rc.8",
"description": "Stupidly fast Entity Component System",
"main": "jecs.luau",
"repository": {

View file

@ -2,11 +2,28 @@ local jecs = require("@jecs")
local testkit = require("@testkit")
local test = testkit.test()
local CASE, TEST, FINISH, CHECK = test.CASE, test.TEST, test.FINISH, test.CHECK
local FOCUS = test.FOCUS
local ob = require("@addons/ob")
TEST("addons/observers", function()
local world = jecs.world()
local world = jecs.world()
do CASE "monitors should accept pairs"
local A = world:component()
local B = world:component()
local c = 1
ob.monitor(world:query(jecs.pair(A, B)), function (_, event)
c += 1
end)
local child = world:entity()
world:add(child, jecs.pair(A, B))
CHECK(c == 2)
world:remove(child, jecs.pair(A, B))
CHECK(c == 3)
end
do CASE "Ensure ordering between signals and observers"
local A = world:component()
local B = world:component()

View file

@ -1064,40 +1064,77 @@ end)
TEST("world:added", function()
local world = jecs.world()
do CASE "Should work even if set after the component has been used"
local A = world:component()
world:set(world:entity(), A, 2)
local ran = false
world:added(A, function()
ran = true
end)
local entity = world:entity()
world:set(entity, A, 3)
CHECK(ran)
end
do CASE "Should work even if set after the pair has been used"
local A = world:component()
local B = world:component()
world:set(world:entity(), A, 2)
world:set(world:entity(), pair(A, B), 2)
world:added(A, function()
ran = true
end)
local entity = world:entity()
world:set(entity, pair(A, B), 3)
CHECK(ran)
end
do CASE "Should allow setting signal after Relation has been used as a component"
local A = world:component()
local B = world:component()
world:add(world:entity(), A)
world:added(A, function()
ran = true
end)
world:add(world:entity(), pair(A, B))
CHECK(ran)
end
do CASE "Should invoke signal for the Relation being set as a key despite a pair with Relation having been cached"
local A = world:component()
local B = world:component()
world:add(world:entity(), pair(A, B))
world:added(A, function()
ran = true
end)
world:add(world:entity(), A)
CHECK(ran)
end
do CASE "Should not override hook"
local A = world:component()
local count = 1
local function counter()
count += 1
end
world:set(A, jecs.OnAdd, counter)
world:added(A, counter)
world:set(world:entity(), A, false)
CHECK(count == (1 + 2))
world:set(world:entity(), A, false)
CHECK(count == (1 + (2 * 2)))
end
end)
TEST("world:range()", function()

View file

@ -1,6 +1,6 @@
[package]
name = "ukendio/jecs"
version = "0.9.0-rc.6"
version = "0.9.0-rc.8"
registry = "https://github.com/UpliftGames/wally-index"
realm = "shared"
license = "MIT"