Skip to content

Commit 8965b3e

Browse files
Port new entity event feature
1 parent 886d7bf commit 8965b3e

File tree

3 files changed

+92
-11
lines changed

3 files changed

+92
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Entity events are events that are emitted and observed for a specific entity.
2+
// They are a thin wrapper around regular observers, which match against queries
3+
// instead of single entities. While they work similarly under the hood, entity
4+
// events provide a much simpler API.
5+
//
6+
// An entity event only needs two pieces of data:
7+
// - The entity on which to emit the event
8+
// - The event to emit
9+
10+
#if Cpp_Observers_EntityEvent
11+
12+
using Flecs.NET.Core;
13+
14+
{
15+
using World world = World.Create();
16+
17+
// Create a widget entity
18+
Entity widget = world.Entity("MyWidget");
19+
20+
// Observe the OnClick event on the widget entity
21+
widget.Observe<OnClick>((Iter it) =>
22+
{
23+
// The event source can be obtained with it.Src(1). This allows the same
24+
// event function to be used for different entities.
25+
Console.WriteLine($"Clicked on {it.Src(1).Path()}!");
26+
});
27+
28+
// Emit the OnClick event for the widget
29+
widget.Emit<OnClick>();
30+
}
31+
32+
// The event to emit.
33+
public struct OnClick { }
34+
35+
#endif
36+
37+
// Output:
38+
// Clicked on MyWidget!

src/Flecs.NET/Core/Entity.cs

+53
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,27 @@ public TEnum ToConstant<TEnum>() where TEnum : unmanaged, Enum
10431043
return *ptr;
10441044
}
10451045

1046+
/// <summary>
1047+
/// Emits an event for this entity.
1048+
/// </summary>
1049+
/// <param name="evt"></param>
1050+
public void Emit(ulong evt)
1051+
{
1052+
new World(World)
1053+
.Event(evt)
1054+
.Entity(Id)
1055+
.Emit();
1056+
}
1057+
1058+
/// <summary>
1059+
/// Emits an event for this entity.
1060+
/// </summary>
1061+
/// <typeparam name="T"></typeparam>
1062+
public void Emit<T>()
1063+
{
1064+
Emit(Type<T>.Id(World));
1065+
}
1066+
10461067
/// <summary>
10471068
/// Short for Has(EcsChildOf, entity).
10481069
/// </summary>
@@ -2212,6 +2233,38 @@ public ref Entity SetJsonSecond<TSecond>(ulong first, string json, ecs_from_json
22122233
return ref SetJson(Macros.PairSecond<TSecond>(first, World), json, desc);
22132234
}
22142235

2236+
/// <summary>
2237+
/// Observe an event on this entity.
2238+
/// </summary>
2239+
/// <param name="evt"></param>
2240+
/// <param name="callback"></param>
2241+
/// <returns></returns>
2242+
public ref Entity Observe(ulong evt, Ecs.IterCallback callback)
2243+
{
2244+
World world = new World(World);
2245+
2246+
Observer observer = world.Observer(
2247+
filter: world.FilterBuilder().With(EcsAny).Src(Id),
2248+
observer: world.ObserverBuilder().Event(evt),
2249+
callback: callback
2250+
);
2251+
2252+
observer.Entity.ChildOf(Id);
2253+
2254+
return ref this;
2255+
}
2256+
2257+
/// <summary>
2258+
/// Observe an event on this entity.
2259+
/// </summary>
2260+
/// <param name="callback"></param>
2261+
/// <typeparam name="T"></typeparam>
2262+
/// <returns></returns>
2263+
public ref Entity Observe<T>(Ecs.IterCallback callback)
2264+
{
2265+
return ref Observe(Type<T>.Id(World), callback);
2266+
}
2267+
22152268
/// <summary>
22162269
/// Get mutable component value (untyped).
22172270
/// </summary>

src/Flecs.NET/Core/EventBuilder.cs

+1-11
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,7 @@ public ref EventBuilder Id<TFirst, TSecond>()
104104
/// <returns></returns>
105105
public ref EventBuilder Entity(ulong entity)
106106
{
107-
ecs_record_t* r = ecs_record_find(World, entity);
108-
109-
Ecs.Assert(r != null, nameof(ECS_INVALID_PARAMETER));
110-
Ecs.Assert(r->table != null, nameof(ECS_INVALID_PARAMETER));
111-
112-
_desc.table = r->table;
113-
_desc.offset = Macros.RecordToRow(r->row);
114-
_desc.count = 1;
107+
Desc.entity = entity;
115108
return ref this;
116109
}
117110

@@ -158,9 +151,6 @@ public ref EventBuilder Ctx<T>(T* data) where T : unmanaged
158151
/// </summary>
159152
public void Emit()
160153
{
161-
Ecs.Assert(_ids.count != 0, nameof(ECS_INVALID_PARAMETER));
162-
Ecs.Assert(_desc.table != null, nameof(ECS_INVALID_PARAMETER));
163-
164154
fixed (EventBuilder* self = &this)
165155
{
166156
_ids.array = self->_idsArray;

0 commit comments

Comments
 (0)