So I when ahead and made some functions in one blueprint which I need in another blueprint and I was wondering which way would be the best to use functions from another blueprint. Putting aside the fact that I have no idea how cast to works, I want to know which of the two options is less CPU intensive.
How the cast to node works
The Cast to Node verifys if a object is from the class you are casting to. For example let’s say i have a actor blueprint called lamp. In my blueprint i also have a variable called interactable. This variable’s type is a actor reference. So if i want to verify if my interactable is a lamp i use the Cast to Node. Because my interactable is a actor and the “lamp” class is a child from the actor (i know this because every actor blueprint is a child from the class actor) the cast will work if the interactable is a lamp, and will fail (cast failed) if it isn’t). You also can pick variables from the class you’re casting to, like for instance let’s say my cast was successful and it is a lamp, then i can use the “As Lamp” output to pick the variables inside the object lamp, for example i know that my interactable is a lamp, now i want to know if is lit or not than i can make a variable inside the lamp actor that hold this value and get it in the cast to node.
Which is more CPU intensive
I think that the blueprint interface could be considered the less CPU intensive but tbh that is really not something that makes to much diference. Unless there are thousands of those instructions it can really make no diference at all. You should be using then in a different way.
Here is a video that might help with your doubts:
Hope that helps.
No need to worry about CPU in this case the difference is negligible. Every time you cast to something you make what is known as a “hard reference” which means that now this type has to be loaded. Interfaces solves this by letting you call an interface function that may or may not be on the target.
The question should be when do you need an interface and when do you need a cast. While both can allow you to access variables and functions from within the casted/interface class a cast is the way to go if you are only going to work with one particular class type and will need access to it at multiple points in the blueprint you are working in. For example you have a widget and it needs to communicate with the player character and constantly update score, health, time etc. All variables you placed on the player character. A cast node is appropriate here. An interface is better if you only need to do something once and it should happen in many different classes for example applying damage on overlap of a weapon projectile. Here you don’t need a specific reference you just want to apply a certain amount of damage to any object this weapon projectile collides with. An interface makes more sense here. If you want to see examples of both interfaces and casting in action check out video #1 and 24 in the link below. Video #25 may also be useful as it goes over how to create references for cast nodes.