Safety
Uncover the inner workings of the Atomix Plugin's Architecture.
What Is Safety?
Safety in the context of Atomix means multiple things. Mainly we want to ensure Safe operations are being performed. Take the following example:
This will return a reference to the component assigned to the entity matching the specified type. But there are certain things we must consider.
- Does the Entity exist
- Is the Entity valid
- Does the Entity have the component
Given that we are returning a reference to the desired component, which cannot be null or invalid, how do we handle cases where we cannot provide the desired return value.
A more robust implementation of this would be:
The last piece of code requires 3 external function calls to provide safe operation. This is a completely acceptable implementation, but it can be optimised.
Atomix provides the developer with the ability to perform safe operations without much of the boilerplate code. You dont even need to change the methods you are using.
To enable Safe operation within scope, all you need to do is the following:
But sometimes you don't require the overhead of a safe operation, maybe in the case of a Registry View.
The actual overhead of doing these checks is very small, but given we are targeting efficient operation we should try and optimise where we can in most cases.
Safe operations are Thread Specific. So declaring a scope as Safe will only affect the local thread from which it was called. Calling safe mode on the game thread will not affect any async tasks or background threads that may also be interacting with the registry.
Opt-In
It seems odd to have Safety as an Opt-In principle, and you're right it is odd. But for good reason.
Performance is typically a primary concern for ECS implementations. Many ECS operations happen in tight loops where safety checks could significantly impact performance so we should aim to have valid data before we enter into any registry operations.