Arch-ECS
💬 Join the discord!☕ Buy us a coffee!
  • 🌄Why Arch?
  • 📖Documentation
    • Concepts
    • World
    • Entity
    • Query
    • Archetypes & Chunks
    • Optimizations
      • Query-Techniques
      • Pass on data
      • Batch and Bulk
      • PURE_ECS
      • Multithreading
      • EntityData
    • Utilities
      • Component Registration
      • Non-generic API
      • CommandBuffer
      • Events
      • Dangerous Extensions
  • 🧩Extensions
    • Arch.Extended
      • Arch.System
      • Arch.System.SourceGenerator
      • Arch.EventBus
      • Arch.AOT.SourceGenerator
      • Arch.LowLevel
      • Arch.Persistence
      • Arch.Relationships
  • 💡Examples & Guidelines
    • Arch.Samples
    • Entities in Query
    • Structural changes
  • Unity
  • 🎮Projects using Arch
    • Skylandkingdoms
    • Cubetory
    • SS14
    • EquilibriumEngine-CSharp
    • Rougelite-Survivor
  • ✏️Misc
    • Roadmap
    • FAQ
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Examples & Guidelines

Structural changes

Structural changes, what you need to consider to avoid shooting yourself in the foot.

PreviousEntities in QueryNextUnity

Last updated 3 months ago

Was this helpful?

When making changes to the structure of an , there are also a few things to consider. In most cases, you can simply edit an entity using Add and Remove.

var entity = World.Create<Dwarf, Position, Velocity>();
entity.Add<Pickaxe>(); 

The entity gets a new component and does not have it yet, that fits.

But sometimes you need to take a closer look.

var entity = GetRandomEntity();  // Returns an entirely random entity
entity.Add<Pickaxe>();           // What if it already has that one?
entity.Remove<Helmet>();         // What if it does NOT have this one? 

Add and Remove do not check whether the already has these components. In this case, the entity could already have these components and it would lead to a corrupted memory.

It is therefore recommended that you always check beforehand if you cannot guarantee that an entity has or does not have a component.

var entity = GetRandomEntity();  // Returns an entirely random entity
if(!entity.Has<Pickaxe>() entity.Add<Pickaxe>();           
if(entity.Has<Helmet>()) entity.Remove<Helmet>();         

Why is that?

The reason

You're probably wondering why we don't just install a few security checks to prevent this kind of behavior? Good question!

Arch's mantra is “Pay what you use” to ensure maximum performance. Arch does nothing in the background to slow down your code, so you need to know what you are doing and sometimes check yourself.

This has pros and cons, one advantage is clean code and performance. In the future Arch will get optional security checks.

💡
Entity
Entity