Class/Struct Info |
|---|
public sealed class CommandQueue : ICommandQueue 指令队列,系统核心扩展功能,主要用于记录查询循环过程中的结构性变更操作(即组件、标签、实体、子实体的增删操作),并在固定节点统一延后处理,防止循环中的结构性变更导致异常。 若在系统执行查询循环过程中需要进行结构性变更操作,均要通过CommandQueue执行。 |
Name | Type | Description |
|---|---|---|
EnableReuse | bool | 指令队列重用标志。 |
TSCommandQueue | ThreadSafeCommandQueue | 线程安全指令队列接口。 |
EntityCommandCount | int | 实体指令数。 |
TagCommandCount | int | 标签指令数。 |
ComponentCommandCount | int | 组件指令数。 |
ChildCommandCount | int | 子实体指令数。 |
TotalCommandCount | int | 总指令数。 |
var world = new IECSWorld();
// 获取CommandQueue,EnableReuse默认为false,即CommandQueue调用Apply()后会被回收,不能继续使用
var commandQueue = world.GetCommandQueue();
// 允许重用,Apply()后还能再次添加指令,再次Apply()
commandQueue.EnableReuse = true;
// 并行任务等多线程操作中,建议使用TSCommandQueue添加指令和应用指令
var query = world.Query<PgdPosition>();
query.ParallelForEach((ref PgdPosition pos, IEntity entity) =>
{
commandQueue.TSCommandQueue.AddComponent<PgdRotation>(entity.Id);
});
commandQueue.TSCommandQueue.Apply(); Qualifier and Type | Method Name and Description |
|---|---|
string | ToString() 返回指令队列信息字符串。 |
void | Clear() 清空指令队列。 |
void | Apply() 处理队列中的指令。 |
void | 将指令队列返回World的指令队列池。 |
void | AddComponent<T>(int entityId) 生成给实体添加组件的指令,如果该实体存在该组件,则会覆盖组件值。 |
void | AddComponent<T>(int entityId, in T component) 生成给实体添加组件(并且包含具体组件值)的指令,如果该实体存在该组件,则会覆盖组件值。 |
void | RemoveComponent<T>(int entityId) 生成给实体移除组件的指令。 |
void | AddTag<T>(int entityId) 生成给实体添加标签的指令。 |
void | AddTags(int entityId, ITags tags) 生成给实体批量添加标签的指令。 |
void | RemoveTag<T>(int entityId) 生成给实体移除标签的指令。 |
void | RemoveTags(int entityId, ITags tags) 生成给实体批量移除标签的指令。 |
void | AddChild(int parentEntityId, int childEntityId) 生成给实体添加子实体的指令。 |
void | RemoveChild(int parentEntityId, int childEntityId) 生成给实体移除子实体的指令。 |
int | 生成创建实体的指令。 |
void | DeleteEntity(int entityId) 生成删除实体的指令。 |
Method |
|---|
string ToString() 返回指令队列信息字符串。 |
Return
Type | Description |
|---|---|
string | 指令队列信息。 |
Sample Code
var world = new IECSWorld();
var commandQueue = world.GetCommandQueue();
commandQueue.AddComponent<PgdPosition>(1);
// 打印指令数量信息
commandQueue.ToString(); // "ComponentCommandCount: {1}, TagCommandCount: {0}, EntityCommandCount: {0}, ChildCommandCount: {0}" Method |
|---|
void Clear() 清空指令队列。 |
Sample Code
var world = new IECSWorld(); var commandQueue = world.GetCommandQueue(); commandQueue.AddComponent<PgdPosition>(1); commandQueue.ComponentCommandCount; // 1 // 清空指令队列 commandQueue.Clear(); commandQueue.ComponentCommandCount; // 0
Method |
|---|
void Apply() 处理队列中的指令。 |
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(1); var commandQueue = world.GetCommandQueue(); commandQueue.AddComponent<PgdPosition>(1); // 此时实体还没有添加PgdPosition // 应用指令 commandQueue.Apply(); // 此时实体拥有了PgdPosition
Method |
|---|
void ReturnToPool() 将指令队列返回World的指令队列池。 |
Sample Code
var world = new IECSWorld(); var commandQueue = world.GetCommandQueue(); // 回收指令队列,不能继续使用 commandQueue.ReturnToPool();
Method |
|---|
void AddComponent<T>(int entityId) 生成给实体添加组件的指令,如果该实体存在该组件,则会覆盖组件值。 如果需要线程安全,需要通过线程安全指令队列接口调用该方法(ThreadSafeCommandQueue.AddComponent<T>(int entityId))。 T为IComponent。 |
Parameters
Name | Type | Description |
|---|---|---|
entityId | int | 实体标识。 |
Sample Code
var world = new IECSWorld(); var commandQueue = world.GetCommandQueue(); // 添加给实体添加组件的指令 commandQueue.AddComponent<PgdPosition>(1); // 给id为1的实体添加PgdPosition的指令,组件值为默认值
Method |
|---|
void AddComponent<T>(int entityId, in T component) 生成给实体添加组件(并且包含具体组件值)的指令,如果该实体存在该组件,则会覆盖组件值。 如果需要线程安全,需要通过线程安全指令队列接口调用该方法(ThreadSafeCommandQueue.AddComponent<T>(int entityId, in T component))。 T为IComponent。 |
Parameters
Name | Type | Description |
|---|---|---|
entityId | int | 实体标识。 |
component | T | 组件值。 |
Sample Code
var world = new IECSWorld(); var commandQueue = world.GetCommandQueue(); // 添加给实体添加组件的指令 commandQueue.AddComponent<PgdPosition>(1, new PgdPosition(1,0,0)); // 给id为1的实体添加PgdPosition的指令,组件值为输入值
Method |
|---|
void RemoveComponent<T>(int entityId) 生成给实体移除组件的指令。 如果需要线程安全,需要通过线程安全指令队列接口调用该方法(ThreadSafeCommandQueue.RemoveComponent<T>(int entityId))。 T为IComponent。 |
Parameters
Name | Type | Description |
|---|---|---|
entityId | int | 实体标识。 |
Sample Code
var world = new IECSWorld(); var commandQueue = world.GetCommandQueue(); // 添加给实体移除组件的指令 commandQueue.RemoveComponent<PgdPosition>(1); // 给id为1的实体移除PgdPosition的指令
Method |
|---|
void AddTag<T>(int entityId) 生成给实体添加标签的指令。 如果需要线程安全,需要通过线程安全指令队列接口调用该方法(ThreadSafeCommandQueue.AddTag<T>(int entityId))。 T为ITag。 |
Parameters
Name | Type | Description |
|---|---|---|
entityId | int | 实体标识。 |
Sample Code
var world = new IECSWorld(); var commandQueue = world.GetCommandQueue(); // 添加给实体添加标签的指令 commandQueue.AddTag<TestTag1>(1); // 给id为1的实体添加TestTag1的指令
Method |
|---|
void AddTags(int entityId, ITags tags) 生成给实体批量添加标签的指令。 如果需要线程安全,需要通过线程安全指令队列接口调用该方法(ThreadSafeCommandQueue.AddTags(int entityId, ITags tags))。 |
Parameters
Name | Type | Description |
|---|---|---|
entityId | int | 实体标识。 |
tags | ITags | 标签集合。 |
Sample Code
var world = new IECSWorld(); var commandQueue = world.GetCommandQueue(); // 添加给实体添加一组标签的指令 commandQueue.AddTags(1, ITags.Get<TestTag1, TestTag2>()); // 给id为1的实体添加一组标签的指令
Method |
|---|
void RemoveTag<T>(int entityId) 生成给实体移除标签的指令。 如果需要线程安全,需要通过线程安全指令队列接口调用该方法(ThreadSafeCommandQueue.RemoveTag<T>(int entityId))。 T为ITag。 |
Parameters
Name | Type | Description |
|---|---|---|
entityId | int | 实体标识。 |
Sample Code
var world = new IECSWorld(); var commandQueue = world.GetCommandQueue(); // 添加给实体移除标签的指令 commandQueue.RemoveTag<TestTag1>(1); // 给id为1的实体移除TestTag1的指令
Method |
|---|
void RemoveTags(int entityId, ITags tags) 生成给实体批量移除标签的指令。 如果需要线程安全,需要通过线程安全指令队列接口调用该方法(ThreadSafeCommandQueue.RemoveTags(int entityId, ITags tags))。 |
Parameters
Name | Type | Description |
|---|---|---|
entityId | int | 实体标识。 |
tags | ITags | 标签集合。 |
Sample Code
var world = new IECSWorld(); var commandQueue = world.GetCommandQueue(); // 添加给实体移除一组标签的指令 commandQueue.RemoveTags(1, ITags.Get<TestTag1, TestTag2>()); // 给id为1的实体移除一组标签的指令
Method |
|---|
void AddChild(int parentEntityId, int childEntityId) 生成给实体添加子实体的指令。 如果需要线程安全,需要通过线程安全指令队列接口调用该方法(ThreadSafeCommandQueue.AddChild(int parentEntityId, int childEntityId))。 |
Parameters
Name | Type | Description |
|---|---|---|
parentEntityId | int | 父实体标识。 |
childEntityId | int | 子实体标识。 |
Sample Code
var world = new IECSWorld(); var commandQueue = world.GetCommandQueue(); // 添加给实体添加子实体的指令 commandQueue.AddChild(1, 2); // 给id为1的实体添加id为2的子实体的指令
Method |
|---|
void RemoveChild(int parentEntityId, int childEntityId) 生成给实体移除子实体的指令。 如果需要线程安全,需要通过线程安全指令队列接口调用该方法(ThreadSafeCommandQueue.RemoveChild(int parentEntityId, int childEntityId))。 |
Parameters
Name | Type | Description |
|---|---|---|
parentEntityId | int | 父实体标识。 |
childEntityId | int | 子实体标识。 |
Sample Code
var world = new IECSWorld(); var commandQueue = world.GetCommandQueue(); // 添加给实体移除子实体的指令 commandQueue.RemoveChild(1, 2); // 给id为1的实体移除id为2的子实体的指令
Method |
|---|
int CreateEntity() 生成创建实体的指令。 如果需要线程安全,需要通过线程安全指令队列接口调用该方法(ThreadSafeCommandQueue.CreateEntity())。 |
Return
Type | Description |
|---|---|
int | 创建的实体标识。 |
Sample Code
var world = new IECSWorld(); var commandQueue = world.GetCommandQueue(); // 创建实体的指令,返回创建的实体的id var entityId = commandQueue.CreateEntity();
Method |
|---|
void DeleteEntity(int entityId) 生成删除实体的指令。 如果需要线程安全,需要通过线程安全指令队列接口调用该方法(ThreadSafeCommandQueue.DeleteEntity(int entityId))。 |
Parameters
Name | Type | Description |
|---|---|---|
entityId | int | 实体标识。 |
Sample Code
var world = new IECSWorld(); var commandQueue = world.GetCommandQueue(); // 删除实体的指令 commandQueue.DeleteEntity(1); // 删除id为1的实体