Is it possible to change a type of object at runtime? I’m trying to cast actors that already exists in the world to other types of actor. But it doesn’t work. The cast always return null.
AMyCustomActor *myCustomActor = Cast<AMyCustomActor>(myActor);
I’m trying to change a StaticMesh to my custom StaticMesh actor.
No, you have to create your static meshes as AMyCustomActor initially. To create them you can use blueprints - add Blueprintable flag to your actor and create a Blueprint based on this class in the editor. Then you will be able to setup properties and insert an object to the level. Other way is to create an editor module and register new assets or asset actions which allow you to add your object to the map directly.
Casting in C++ is not meant to actually change the type of the object you are casting. It is meant to be able to treat an object as if it were something different.
This works for example if you have a child object and cast it to the parent class: it will work, because the child object has everything that the parent object has, it can therefore be treated as if it was a parent object. The other way around (like you are trying to do here) does not work, since the parent object may not have all the variables the child class has and can therefore not be treated as a child object. The Cast checks if a cast is possible and returns nullptr if it’s not possible (which is what you get).
So casting doesn’t change the type of an object, just how you can treat it. If you want an object of a type, you have to create as that type.
You’re correct but I would like to comment on one thing you said. It is not meant “to treat an object as if it were something different”, when you have a child class of a class, for example the class “Car” inherits from the class “Vehicle”, then the class “Car” IS a “Vehicle”. Therefor you are able to cast it, because it is already the same class. It is a subset of a veichle.
It is good practise to always consider the child classes with an “IS/ARE” relationship, such as “the Car IS a Vehicle”.
That is true, but only in object orientation. If we think outside of that there still is a lot of casting that actually transforms the data (which I got a bit wrong myself). For example: If you cast base data types like integers or floating point numbers, the cast actually transforms the data. If I cast
float f = 1.0; int i = (int) f;
then the value of f is copied and actually transformed into an integer.
On the other hand you of course have the object orientated casting, which is basically there to give the compiler information that it can not know without you telling it. If you want to get a child component the compiler only knows that it is a SceneComponent. You, as the person who put that component there in the first place may know more about it, for example that its actually of type MySceneComponent. The compiler can not know that without you telling it, so you use a cast to do that. This only works if MySceneComponent also IS a SceneComponent. So it has to be a child class of SceneComponent. The reason why that doesn’t change any data is because all you do is cast a pointer to an object. You don’t actually cast that object in that case!
Thanks for the clarification guys.