Download

Does casting to the parent class affect children?

What I’m trying to do is cast to the Ore to see if that’s what has been hit with the mining laser. I made children of the Ore so they all had the same code but when I’m casting to the parent it only casts to the parent and I want it to cast to all the children as well. Basically, is there a way to cast to ALL the children of a specific parent without casting to each and every single one?

This is the cast node in the player -

It is the parent of these 2 'ores -

When the cast is successful I want it to

you show the same picture two times.

You probably misunderstand the concept of inheritance and casting.

Inheritance in OOP Design means that a Base Class(BP) can be inherited.
When the Class is inherited, the instance is only the child class and can’t be magically casted to a “sibling” of this class.
A cast is only used to get special behaviour from child classes.

For example you have a base class called “Ore”

Children classes could be “Iron Ore” and “Silver Ore”.

If you trace against a Ore, you may need to distinguish between Iron and Silver.
To do so, you cast your base Ore to Iron. If the cast is successful, you do Iron Special Behaviour. If the Cast fail, do a second cast against Silver Ore and do the Silver Ore special behaviour.

I’d suggest to use a base class with an interface and implement concrete (= Silver and Iron) behaviour in the children classes.
This way you have a base interface that every ore uses, but has different behaviour.

1 Like

I want all the ores to have the same behaviour.

Create a class named ‘Ore’. Put all the code that is the same for all types of Ores here. Now you can extend from this class to create different types of Ores such as Iron Ore, Silver Ore… You just add/modify code specific to those.

Why do you want to cast to all child classes? It does not make any sense. Are you trying to identify which child class a specific Ore instance belongs to. Ie, you have an instance of Ore that you got froma line trace. Now you want to see if that one is Iron Ore, Silver Ore or Copper Ore?

If that is the case you can use GetClass and ClassIsChildOf nodes.
Here is an example:
3513bd0c01e4c90f578f36b7432d6f09eed8a776.png

Yes. If you created “Ore Base” and then inherited “Silver” “Bronze” “Gold” etc.
Lets say you Overlap or Trace actor - if you use “cast to ore base”, it is cast to all inherited classes…

But lets say, if you have “explosive ore” and only in explosive ore you have “BOOM” function, then when you use cast to ore base, you cant use “boom” function, you must use casto to explosive ore…

You should read something about dynamic binding and static binding.

If you have a base Type “Ore” and some other subtypes like “Iron Ore”, but you keep an array of “Ore”, you can put your Iron Ore into the Array.

In this Array, the Static Type is Ore, but the Dynamic Type is whatever your ore is “in reality”.
If you want to get the “real” Type of one of those array elements, you need to cast/check.
If you want to call methods, that every Ore has inherited from Base Ore, the “real” function(=the overriden implementation) is called.

Thanks guys it works now