Download

The right way to specify UObject subclasses properties

Hello, I’m writing a weapon system where each weapon can mount a module that defines weapon stats and other info (like muzzle particle system).
I want to create a lot of these modules and each module might have some specific functionalities triggered by the weapon (so I cannot think about these modules just as configurations).

I have a huge doubt about the architecture that I’m using here… this is what I wanted to achieve:

  • A base UWeaponModule class from which the various modules inherit
  • Each module can override base class values and behaviors

That being said, overriding the behaviors is quite simple since I can just override functions, but how can I override values? the only solution I found is to handle values as inline functions and have these functions returning the value… so that I can override these functions in the subclasses.

Here is an example of the base class… it defines a weapon that does a damage of 100, uses a specific particle system as the muzzle effect and cannot shoot bursts.


UCLASS()
class THEMIR_API UWeaponModule : public UObject
{
    GENERATED_BODY()

public:

    virtual bool burst() {
        return false
    }

    virtual float getDamage() {
        return 100.0f;
    }

    virtual FString getMuzzleEffectPath() {
        return  "/Game/Assets/Particles/Impacts/Particles/P_MuzzleSimple.P_MuzzleSimple";
    }

    virtual void fire(); // each module might have a different fire behavior.

};

Now I can easily subclass this class with an object that uses bursts, with a damage of 300 and so on…

I’m wondering if this is a good way to handle this logic… using a function to define what normally I’d define as a simple variable sounds a little bit strange to me.

That doesn’t make any sense to me.
UObject instances are serializable, everything there can simple be variables instead of those weird functions.

Thank you, how would you handle subclassing? How do you define the default value of a variable in a subclass? you cannot override class member variables…

Let’s say that now that I have the base class I wanted to create a subclass module, I can easily write its properties specification this way:


virtual float getDamage() **override **{ return 400.0f; }


I can’t find a way to obtain the same result without using functions… that is why I have written this question, any suggestions to improve this logic would be really appreciated.

All classes and child classes default objects can have their own default values serialized independently.

All child classes can set their own defaults in their constructors.

Are you suggesting to define the values inside the constructor of each subclass?
It means that I can’t define values from the header file… defining values inside the constructor directly sounds strange (yes, better than what I’ve written for sure…) Am I understanding correctly? I’m sorry to bother you but could share an example with code if you have time?

OK you are probably suggesting something like:



UCLASS()
class THEMIR_API UWeaponModule :
    public UObject {    
    GENERATED_BODY()  
    public:    
    UWeaponModule(){
        isBurst = false;
        damage = 100.0f;
        getMuzzleEffectPath = "/Game/Assets/Particles/Impacts/Particles/P_MuzzleSimple.P_MuzzleSimple";
    }
 . . . .  
 . . . .
 . . . .
 . . . .
};


sounds definitely better than what I’ve written. Thanks!