Skip to content

Commit 28cffd0

Browse files
committed
Some safety
1 parent 04d52e7 commit 28cffd0

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/Arch/Core/Extensions/EntityExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ public static bool Has<T>(this in Entity entity)
124124
[Pure]
125125
public static ref T Get<T>(this in Entity entity)
126126
{
127+
if (!entity.Has<T>())
128+
{
129+
throw new InvalidOperationException($"Entity {entity} does not have a component of type {typeof(T).Name}.");
130+
}
131+
127132
var world = World.Worlds.DangerousGetReferenceAt(entity.WorldId);
128133
return ref world.Get<T>(entity);
129134
}
@@ -182,6 +187,11 @@ public static ref T AddOrGet<T>(this in Entity entity, T? component = default)
182187

183188
public static void Add<T>(this in Entity entity, in T? component = default)
184189
{
190+
if(entity.Has<T>())
191+
{
192+
throw new InvalidOperationException($"Entity {entity} already has a component of type {typeof(T).Name}.");
193+
}
194+
185195
var world = World.Worlds.DangerousGetReferenceAt(entity.WorldId);
186196
world.Add(entity, component);
187197
}

0 commit comments

Comments
 (0)