Common Patterns
Singleton (Unique)
Sometimes you need to get an instance of a certain Subject that has a globally unique combination of Traits. This can be thought of something like a singleton pattern within a classical OOP approach.
You can totally achieve that with the help of a simple method like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
template < typename T, typename ...Ts > T GetSingleton(AMechanism* const Mechanism) { const auto Filter = FFilter::Make<T, Ts...>(); const auto Chain = Mechanism->Enchain(Filter); auto Cursor = Chain->Iterate(0, 1); if (!Cursor.Provide()) { checkNoEntry(); return T(); } const auto Trait = Cursor.GetTrait<T>(); verifyf(!Cursor.Provide(), TEXT( "Two singleton objects detected!" )); return Trait; } |
You can then query for the unique trait like so:
1 |
auto EnemyBalance = GetSingleton<FGameBalance, FEnemy>(Mechanism); |