Skip to content

Commit f23e214

Browse files
authored
Merge pull request #22 from SemihBudak/master
Added id overload for EntityCollection.CreateEntity
2 parents 76f9439 + adacb00 commit f23e214

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/EcsRx/Collections/Entity/EntityCollection.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@ public void SubscribeToEntity(IEntity entity)
5858
public void UnsubscribeFromEntity(int entityId)
5959
{ EntitySubscriptions.RemoveAndDispose(entityId); }
6060

61-
public IEntity CreateEntity(IBlueprint blueprint = null)
61+
public IEntity CreateEntity(IBlueprint blueprint = null, int? id = null)
6262
{
63-
var entity = EntityFactory.Create(null);
63+
if (id.HasValue && EntityLookup.Contains(id.Value))
64+
{ throw new InvalidOperationException("id already exists"); }
65+
66+
var entity = EntityFactory.Create(id);
6467

6568
EntityLookup.Add(entity);
6669
_onEntityAdded.OnNext(new CollectionEntityEvent(entity));

src/EcsRx/Collections/Entity/IEntityCollection.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ public interface IEntityCollection : IReadOnlyList<IEntity>, INotifyingEntityCol
1414
/// Name of the collection
1515
/// </summary>
1616
int Id { get; }
17-
17+
1818
/// <summary>
1919
/// This will create and return a new entity.
2020
/// If required you can pass in a blueprint which the created entity will conform to
2121
/// </summary>
2222
/// <param name="blueprint">Optional blueprint to use for the entity (defaults to null)</param>
23+
/// <param name="id">Id to use for the entity (defaults to null, meaning it'll automatically get the next available id)</param>
2324
/// <returns></returns>
24-
IEntity CreateEntity(IBlueprint blueprint = null);
25+
IEntity CreateEntity(IBlueprint blueprint = null, int? id = null);
2526

2627
/// <summary>
2728
/// This will add an existing entity into the group, it is mainly used for pre-made

0 commit comments

Comments
 (0)