Skip to content

Commit f1a4ff3

Browse files
committed
call struct constructor in EntityUtils.EntityUtils.AddEntityComponent()
1 parent 94182e7 commit f1a4ff3

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/ECS/Base/Types/ComponentType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ internal override bool RemoveEntityComponent(Entity entity) {
9898
}
9999

100100
internal override bool AddEntityComponent(Entity entity) {
101-
return entity.AddComponent<T>(default);
101+
return entity.AddComponent(new T());
102102
}
103103

104104
internal override bool AddEntityComponentValue(Entity entity, object value) {

src/Tests-internal/ECS/Test_Entity.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,23 @@ public static void Test_Entity_GetEntityByRawEntity()
268268
});
269269
AreEqual("id: 128. expect in [0, current max id: 127]", e!.Message);
270270
}
271+
272+
private struct StructWithConstructor : IComponent {
273+
public int value = 123;
274+
public StructWithConstructor() { } // requires C# 10
275+
}
276+
277+
[Test]
278+
public static void Test_Entity_AddEntityComponent()
279+
{
280+
var store = new EntityStore();
281+
var entity = store.CreateEntity();
282+
var schema = EntityStore.GetEntitySchema();
283+
var componentType = schema.ComponentTypeByType[typeof(StructWithConstructor)];
284+
EntityUtils.AddEntityComponent(entity, componentType);
285+
var component = entity.GetComponent<StructWithConstructor>();
286+
AreEqual(123, component.value);
287+
}
271288
}
272289

273290
internal struct MyTag : ITag { }

src/Tests-internal/Tests-internal.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<!-- packaging -->
1212
<PublishRepositoryUrl>false</PublishRepositoryUrl>
1313
<IsPackable>false</IsPackable>
14-
<LangVersion>9</LangVersion>
14+
<LangVersion>10</LangVersion>
1515
<!-- packaging: end -->
1616
</PropertyGroup>
1717

0 commit comments

Comments
 (0)