I realize that Decorators are not supposed to have their values changed, hence they are defined as const
. But say that I have a reference to a custom-defined Actor, and I want to use Cast
to get some specific variables from that Actor; without having to Cast<>
inside the CalculateRawConditionValue()
function every time I need to get that variable. Is there a way to initialize that Cast once and never do it again?
Not that I know of. At the risk of giving some unsolicited advice, usually when I run into cases like this I re-evaluate my class design. I.e. if you need to do casts to access subclass elements it means that your OOP design is not optimal. I.e. you are introducing subclass-specific conditions and handling into the design, which is not good.
One solution to this is to have functions/properties on your base class which can then be accessed by anything with a reference to that base class (even if the specific reference might be of a subclass).
E.g. suppose you have a class hierarchy as follows:
Weapon
----MeleeWeapon
--------KnifeWeapon
--------BaseballBatWeapon
----HandgunWeapon
--------M1911Weapon
--------GlockWeapon
and you have the following (admittedly contrived) functions on the following:
MeleeWeapon:
// Returns how long it takes to slash
float GetSlashTime()
HandgunWeapon:
// Returns how long it takes to fire
float GetFireTime()
This type of function should be moved to Weapon and called GetAttackTime() or something similar.
Each derived class can then override this function with their own implementations to give you the required output.
P.S.
Some purists might shoot me on sight for this, but I even put the “Reload” function on Weapon (instead of HandgunWeapon) with an empty implementation, and override this in HandgunWeapon to actually implement it when requried.
This is because Reload is generally something called from outside the Weapon class (e.g. from the Character) and it allows me to not worry about casting when trying to call Reload.