当前位置:网站首页>Entitas learning [iv] other common knowledge points
Entitas learning [iv] other common knowledge points
2022-07-04 11:31:00 【Mercury Note】
EntityIndex
[Game, Input, UI]
public class NumberComponent : IComponent
{
[EntityIndex]
public int number;
}
It is written as follows , When an attribute of a component has this tag ,Entitas When generating code based on components, the method to find based on this property will be generated , In a specified context , This attribute can be used as a unique id, Find the exact entity based on this .
It doesn't have to be int type ,string And so on.
Contexts.sharedInstance.game.GetEntitiesWithNumber(666);
The above code works everywhere , Even in monobehaviour In the script , But it is not recommended to access outside the framework Entitas Things inside .
GetEntitiesWithNumber This function only specifies EntityIndex Properties of will be generated .
public static class ContextsExtensions {
public static System.Collections.Generic.HashSet<GameEntity> GetEntitiesWithNumber(this GameContext context, int number) {
return ((Entitas.EntityIndex<GameEntity, int>)context.GetEntityIndex(Contexts.Number)).GetEntities(number);
}
public static System.Collections.Generic.HashSet<InputEntity> GetEntitiesWithNumber(this InputContext context, int number) {
return ((Entitas.EntityIndex<InputEntity, int>)context.GetEntityIndex(Contexts.Number)).GetEntities(number);
}
public static System.Collections.Generic.HashSet<UIEntity> GetEntitiesWithNumber(this UIContext context, int number) {
return ((Entitas.EntityIndex<UIEntity, int>)context.GetEntityIndex(Contexts.Number)).GetEntities(number);
}
}
Event
It is written as follows
[Event(EventTarget.Any, EventType.Added)]
public class NumberComponent : IComponent
{
[EntityIndex]
public int number;
}
In the use of the Event after , The generated code will be in this folder .
One of the
public interface IAnyNumberListener {
void OnAnyNumber(GameEntity entity, int number);
}
Inherit this interface rewrite OnAnyNumber Method ,
When NumberComponent When a component is added or deleted
Will be called back , When calling back, the value and entity of the component are passed in .
EventTarget There are two kinds of ,Any and Self
- The tag Any Of , All inherited IAnyNumberListener Of OnAnyNumber Will be called back , Not limited to Entitas Class .
- The tag Self Of , Only inheritance is limited IAnyNumberListener With NumberComponent The entity class of the component will be notified , So this kind of writing , We also need to be in our own script , Redefine the correlation by distributing classes context Below Entity, Let him inherit IAnyNumberListener Rewrite method .
Parameters EventType There are two options
public enum EventType
{
Added,
Removed,
}
Specify to call when the component is added or deleted
In fact, this label is added ,Entitas Will automatically generate a reaction system
public sealed class AnyNumberEventSystem : Entitas.ReactiveSystem<GameEntity> {
readonly Entitas.IGroup<GameEntity> _listeners;
readonly System.Collections.Generic.List<GameEntity> _entityBuffer;
readonly System.Collections.Generic.List<IAnyNumberListener> _listenerBuffer;
public AnyNumberEventSystem(Contexts contexts) : base(contexts.game) {
_listeners = contexts.game.GetGroup(GameMatcher.AnyNumberListener);
_entityBuffer = new System.Collections.Generic.List<GameEntity>();
_listenerBuffer = new System.Collections.Generic.List<IAnyNumberListener>();
}
protected override Entitas.ICollector<GameEntity> GetTrigger(Entitas.IContext<GameEntity> context) {
return Entitas.CollectorContextExtension.CreateCollector(
context, Entitas.TriggerOnEventMatcherExtension.Added(GameMatcher.Number)
);
}
protected override bool Filter(GameEntity entity) {
return entity.hasNumber;
}
protected override void Execute(System.Collections.Generic.List<GameEntity> entities) {
foreach (var e in entities) {
var component = e.number;
foreach (var listenerEntity in _listeners.GetEntities(_entityBuffer)) {
_listenerBuffer.Clear();
_listenerBuffer.AddRange(listenerEntity.anyNumberListener.value);
foreach (var listener in _listenerBuffer) {
listener.OnAnyNumber(e, component.number);
}
}
}
}
}
Different systems that generate different tags can change the tags themselves and then go in to see what is different .
The difference between different labels I mentioned above can be verified in this way .
Next, it will be recorded with Entitas Record Endless ball Key points of the case .
other
Adding Feature When , Different Feature The addition order of Feature Inside System The order of execution is influential , Therefore, when doing more in-depth, you should clarify when adding Feature Between the execution sequence of their own to do System The impact of the order of execution , That is, if two System Of Trigger identical , Then if two System When the execution order will affect the result , Consider two System Of Feature The order in which they are added , See if you want to belong Feature Changes . If Feature identical , Consider two System In this Feature The order in which they are added .
Reference resources
Unity Framework exploration ——ECS framework Entitas The basic concept of this chapter
EntityComponentSystem framework Entitas Multi context response system ( Four )
边栏推荐
- Reptile learning 3 (winter vacation learning)
- Exceptions and exception handling
- Take advantage of the world's sleeping gap to improve and surpass yourself -- get up early
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
- Sys module
- LxC shared directory addition and deletion
- Elevator dispatching (pairing project) ④
- Detailed explanation of classic process synchronization problems
- Canoe - the second simulation engineering - xvehicle - 2panel design (principle, idea)
- 2021-10-20
猜你喜欢
Failed to configure a DataSource: ‘url‘ attribute is not specified... Bug solution
Canoe - the second simulation engineering - xvehicle - 2panel design (principle, idea)
Replace() function
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9
TCP slicing and PSH understanding
Analysis function in SQL
OSI seven layer reference model
Btrace tells you how to debug online without restarting the JVM
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
2021-08-09
随机推荐
Canoe - the second simulation engineering - xvehicle - 2panel design (principle, idea)
Canoe test: two ways to create CAPL test module
SSH原理和公钥认证
If function in SQL
Post man JSON script version conversion
Btrace tells you how to debug online without restarting the JVM
Canoe - the second simulation project -xvihicle1 bus database design (operation)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
Reptile learning 3 (winter vacation learning)
Automatic translation between Chinese and English
How do std:: function and function pointer assign values to each other
Notes on writing test points in mind mapping
Is Sanli futures safe? How to open a futures account? How to reduce the handling charge of futures at present?
QQ group administrators
Canoe the second simulation engineering xvehicle 3 CAPL programming (operation)
Postman advanced
2021-10-20
(2021-08-20) web crawler learning 2
Summary of Shanghai Jiaotong University postgraduate entrance examination module firewall technology
Canoe: the difference between environment variables and system variables