Download

C++ - How to check Struc type in runtime

Hi,

I’m a little bit lost when using struct

let’s say:

struct A {
int8 Prop
}

struct B: A
{
int8 Prop2
}

Now, I have a property with “A MyProp;”. I set it with a B Struct like A = B();

Now how do I know if I can safely cast MyProp to a B struct?

Dynamic_cast won’t work as the struct is not polymorph (no virtual function).
Static_cast is risky, as if I have a A struc in MyProp, it will be converted in B with “bad information”.

Thanks for your help.

Hi !

You can find a good example on the TakeDamage function of Actor :
https://github.com/EpicGames/UnrealEngine/blob/b281c38557ad245450fe44f39b9ae28fea3cbe97/Engine/Source/Runtime/Engine/Private/Actor.cpp#L1831

There is a FDamageEvent struct with a ClassID number (uint8) in the base struct with a IsOfType() function to check if the type is correct.
Doc about FDamageEvent : https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Engine/FDamageEvent/index.html

I hope this help you ! :slight_smile:

Hi,

Thanks, but this is a bit hacky and this is only working if you “master” the base struct. In my case, the base struct is in the OSS module, I wanted to inherit and add new properties, but sadly, I have no way to know in the interfaces function if I’m using the base struct or my struct.

The only way will be to change the UE Code, and for the moment, I would like to avoid this to ease the deployment in my team (not in single site).

thanks,