Is it possible to check, if class have a variable?

Good day!
I want to know, do specific class have custom variable. I don’t want to cast to specific class, I want to cast to variable - is it possible in blueprints? Or maybe c++?
For example - I have different pawns in scene (they have different classes, but one parent class), but two pawns have a specific similar variable - other pawns don’t have it. Seems like a bad idea to cast for a each pawn with that specific variable. Is it possible to get that variable?

nope, the better way to do this is using inheritance, you can create a parent class and put variables and functions there, then all the actors that are subclasses of the parent class will have the same variables and functions from the parent (but they may also have extra functions and variables, and their functions can also have a different behaviour, but with the same name), you can also create a interface, all the class that implements that interface will have the same function (with the same name, but probably the functions will behave differently).

there’s a workaround, you can make these classes implement the same interface, in the interface you define 2 functions getVariable and setVariable, the first one returns a variable and the second one requires a variable as a parameter, then you implement the get/sets in these classes (the get returns the value of the variable and the set changes the variable to the value it received as a parameter), then as a result you will be able to ask “does implement interface” to check if they have the variable (to be more specific, you will ask if they got the get/set implemented), then if true you can get or set the variable you wanted

1 Like

As an alternative to an inheritance based design, you can capsule the variable in a component derived from UActorComponent. The component is added to the Actor using youractor->CreateDefaultSubObject in the actors construcor.

You can now check if an actor has the component by calling youractor->FindComponentByClass>UYourComponent> and checking the returned pointer for nullptr

I figured it out that I can use that:

GetClass()->FindPropertyByName("DialogueTree")