From 916f730e9a263b939197b280d04ee454a8a076c1 Mon Sep 17 00:00:00 2001 From: Mark Marks Date: Sun, 1 Sep 2024 20:05:21 +0200 Subject: [PATCH] feat: Trackable creation of entities in sapphire-jecs references - Users are now able to track whenever an entity gets created through a reference via the sapphire_jecs.on_created signal. This is useful, for example, for tracking freshly replicated entities and setting them up. --- crates/sapphire-jecs/lib/init.luau | 5 ++++- crates/sapphire-jecs/lib/ref.luau | 12 +++++++++++- crates/sapphire-jecs/wally.toml | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/sapphire-jecs/lib/init.luau b/crates/sapphire-jecs/lib/init.luau index c23a93e..9779fd0 100644 --- a/crates/sapphire-jecs/lib/init.luau +++ b/crates/sapphire-jecs/lib/init.luau @@ -22,7 +22,9 @@ local pair = jecs.pair local world = require(script.world) local handle = require(script.handle) export type handle = handle.handle -local ref = require(script.ref) +local _ref = require(script.ref) +local ref = _ref.ref +local ref_on_created = _ref.on_created local spawner_type = require(script.spawner_type) local SapphireJecs = {} @@ -128,6 +130,7 @@ function SapphireJecs.handle(entity: entity): handle end SapphireJecs.ref = ref +SapphireJecs.on_created = ref_on_created --- Creates a new entity and returns its id. --- @return entity diff --git a/crates/sapphire-jecs/lib/ref.luau b/crates/sapphire-jecs/lib/ref.luau index cc22759..feaf6ce 100644 --- a/crates/sapphire-jecs/lib/ref.luau +++ b/crates/sapphire-jecs/lib/ref.luau @@ -1,4 +1,10 @@ --!strict +local signal = require(script.Parent.Parent.signal) +--- Fires whenever a reference with a given key creates a new entity. +--- Useful to track freshly replicated entities. +--- @type signal< entity, key> +local on_created: signal.Signal = signal() + local world = require(script.Parent.world) local handle = require(script.Parent.handle) local refs = {} @@ -17,9 +23,13 @@ local function ref(key: any): handle.handle if not entity then entity = world:entity() refs[key] = entity + on_created:Fire(entity, key) end return handle.new(entity) end -return ref +return { + ref = ref, + on_created = on_created, +} diff --git a/crates/sapphire-jecs/wally.toml b/crates/sapphire-jecs/wally.toml index 3a92c15..be79e00 100644 --- a/crates/sapphire-jecs/wally.toml +++ b/crates/sapphire-jecs/wally.toml @@ -1,6 +1,6 @@ [package] name = "mark-marks/sapphire-jecs" -version = "0.1.2" +version = "0.1.3" registry = "https://github.com/UpliftGames/wally-index" realm = "shared" license = "MIT"