Skip to content

Commit 7c387f5

Browse files
committed
+ fixes for Events
1 parent c613cb7 commit 7c387f5

12 files changed

+105
-83
lines changed

Events/CompiledEventListener.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Scellecs.Morpeh.Events;
2+
3+
namespace Scellecs.Morpeh
4+
{
5+
public readonly struct CompiledEventListener<T> where T: IWorldEvent
6+
{
7+
public readonly IQuerySystem querySystem;
8+
public readonly EventListener<T> listener;
9+
10+
public CompiledEventListener(IQuerySystem querySystem, EventListener<T> listener)
11+
{
12+
this.querySystem = querySystem;
13+
this.listener = listener;
14+
}
15+
}
16+
}

Events/CompiledEventListener.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Events/Data.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Events/Data/EventWithEntity.cs.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Events/Data/IWorldEvent.cs.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Events/EventListener.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class EventListener<T> : EventListener, IEnumerable<T> where T : IWorldEv
2828
public bool HasPublishedEventsThisFrame { get; private set; }
2929
private bool IsUpdateScheduled => HasPublishedEventsThisFrame || hasScheduledEventsForNextFrame;
3030

31-
public EventListener(IQuerySystem querySystem, EventsFeature feature) : base(querySystem, feature)
31+
public EventListener(EventsFeature feature) : base(feature)
3232
{
3333
}
3434

@@ -133,11 +133,9 @@ private void TryCatchInvokeCallback()
133133
public abstract class EventListener
134134
{
135135
internal EventsFeature feature;
136-
internal IQuerySystem querySystem;
137136

138-
protected EventListener(IQuerySystem querySystem, EventsFeature feature)
137+
protected EventListener(EventsFeature feature)
139138
{
140-
this.querySystem = querySystem;
141139
this.feature = feature;
142140
}
143141

Events/EventListener.cs.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Events/EventListenerExtensions.cs

Lines changed: 59 additions & 59 deletions
Large diffs are not rendered by default.

Events/EventListenerExtensions.cs.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Events/EventSystemExtensions.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,25 @@ public static void ScheduleEvent<T>(this World world, T data) where T : IWorldEv
7171
// ------------------------------------------------- //
7272

7373
[PublicAPI]
74-
public static EventListener<T> CreateEventListener<T>(this IQuerySystem querySystem) where T : IWorldEvent
74+
public static CompiledEventListener<T> CreateEventListener<T>(this IQuerySystem querySystem) where T : IWorldEvent
7575
{
7676
var type = typeof(T);
77-
var eventFeature = querySystem.World.GetFeature<EventsFeature>();
77+
if (!querySystem.World.TryGetFeature(out EventsFeature eventFeature))
78+
{
79+
Debug.LogError($"You should enable [{nameof(EventsFeature)}] for world [{querySystem.World}] before using [{nameof(CreateEventListener)}]!");
80+
return default;
81+
}
82+
7883
if (eventFeature.eventListenersByEventType.TryGetValue(type, out var registeredEvent))
79-
return (EventListener<T>)registeredEvent;
84+
return new CompiledEventListener<T>(querySystem, (EventListener<T>)registeredEvent);
8085

81-
registeredEvent = new EventListener<T>(querySystem, eventFeature);
86+
registeredEvent = new EventListener<T>(eventFeature);
8287
eventFeature.eventListenersByEventType.Add(type, registeredEvent);
83-
return (EventListener<T>)registeredEvent;
88+
return new CompiledEventListener<T>(querySystem, (EventListener<T>)registeredEvent);
8489
}
8590

8691
[PublicAPI]
87-
public static EventListener<EventWithEntity<T>> CreateEntityEventListener<T>(this IQuerySystem querySystem) where T : IWorldEvent
92+
public static CompiledEventListener<EventWithEntity<T>> CreateEntityEventListener<T>(this IQuerySystem querySystem) where T : IWorldEvent
8893
{
8994
return CreateEventListener<EventWithEntity<T>>(querySystem);
9095
}

0 commit comments

Comments
 (0)