sschmid / Entitas

Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
MIT License
7.08k stars 1.11k forks source link

I‘m a program noob. Is there a way to make "if(entity.hasComponent){ entity.component... }" shorter? #952

Closed atkdefender closed 3 years ago

atkdefender commented 3 years ago

C# in unity

Without "if(entity.hasComponent)", "entity.component" sometimes cause a error. But it make my code to long.

ghost commented 3 years ago

It will not sometimes indicate an error, but always. You try to access data that does not exist.

Tip: If you use ReactiveSystems you can also set the filter method correctly in advance. Like in this example here: https://github.com/sschmid/Match-One/blob/master/Assets/Features/Board/Systems/FallSystem.cs The filter also checks if it is a piece before the execute is called.

How would you wish what would happen if the component did not exist on the entity?

atkdefender commented 3 years ago

It will not sometimes indicate an error, but always. You try to access data that does not exist.

Tip: If you use ReactiveSystems you can also set the filter method correctly in advance. Like in this example here: https://github.com/sschmid/Match-One/blob/master/Assets/Features/Board/Systems/FallSystem.cs The filter also checks if it is a piece before the execute is called.

How would you wish what would happen if the component did not exist on the entity?

Something like "entity.component?.value=...", if it doesn't cause error, that will be great.

I know the reason now. I allways use entitas and coroutine together. When the coroutine ends, some entities is don't have that component already. Maybe I should program the "EntityGroup", instead of one "Entity".