I want to explain the difference between passive and active game objects. What they are and when to use them. This is in my mind quite some time. I hope I can express it in an understandable way.
This thread discusses Game Objects only (it does not discuss the interaction to Physics, Rendering or other parts of the BGE).
What is an Active Game Object?
An Active Game Object processes the logic of a feature on itself. It extracts data from itself and it modifies its own states. It behaves as a Single Object.
What is a Passive Game Object?
A Passive Game Object is an object whose logic is processed by another game object. This means the data is extracted by another game object and modified by another game object.
The active object that performs the logic on the Passive Object is a Manager.
In terms of programming the passive game object behaves like data. The processing belongs to the manager (an active game object).
Can a game object be active and passive?
This means the object itself performs logic and another object performs logic on it.
If both processes perform the same logic this situation creates a conflict. It is strongly advised to avoid such conflicts as it easily leads to unpredictable results.
Therefore it is important to decide early if active or passive processing is used.
If active and passive logic perform different tasks the logic is independent from each other (separation of concerns). If seen separately the object will be active for one processing and passive for the other processing .
A) Selection processing:
Selection logic can be performed by a SelectionManager. The SelectionManager performs a centralized selection process. All UI regarding selection will be processed by the SelectionManager. The SelectionManager knows what objects are selected.
The SelectionManager deals with other game objects. In terms of selection processing these game objects are passive as they do not add any logic to the selection process.
A’)Alternative Selection process:
The game objects perform the selection process by them self. Each one can have an individual processing.
B) Selection Visualization:
Each game object can process it’s own selection visualization logic (it shows that it is selected or not). This makes the game objects active in terms of selection visualization (but not in terms of selection processing).
Both the selection process and the selection visualization are two separate features. These features have a process dependency as the selection visualization needs the output of the selection process. None of them need to know how the other process works.
When to use what?
This is a difficult decision and strongly depends on the situation.
- there is a lot of repeated processing (e.g. The selection process is always the same on all selectable objects)
- the process needs consistent processing (e.g Selection is always on <LMK>)
- the involved game objects require individual processing (e.g. a Door gets a different selection mark than a Character)
- there is only one game object involved
If a Manager has to deal with a lot of exceptions it is better to split the processing. Better let each manager perform its own processing. Just make sure they uniquely identify the passive game objects they need. E.g. a Selection manager for waypoints and a Selection manager for characters.
The method to manage (several) equal behaving objects is summarized in the Flyweight pattern