"Cast to" help

Hi, I’m trying to understand how to us the “cast to” node properly using blueprints.

I am using a character BP for the player and an actor BP for the player ability “projectile”

I plan on having multiple abilities and I wanted them to scale off the player’s damage, range, speed etc.

I did find a way to get a variable from a different BP but it goes through an array and it seems like it would be expensive in the long run.

Any help or suggestions would be greatly appreciated:)

1 Like

This does work

image

IF, you set the variable.

You probably tried it and got ‘none’ errors. That’s because you hadn’t set the variable to the specific actor you want to use.

But, yes, it’s not scalable, because you have to manually set the variable for each type of actor you might interact with.

‘Get actors’ is fine, if it’s under control. IE you’re not doing it every frame. There is a ‘get actor of class’ node BTW, so you don’t need the array code.

The two final answers are

  1. Have a reference to what you want to interact with, which you set when you spawn or introduce the object into the game.

  2. Use blueprint interfaces. Then you can talk to anything you can get a reference to, without casting. You would probably get the reference from a dynamic process like a line trace.

2 Likes

https://www.youtube.com/watch?v=HBplrWCrHtc

If you don’t understand what a cast is.

If you have 2 classes, Food and Apple (deriving from Food), you can say these things in programming:
*Apple is a food.
*Food is not an apple.

If the Food class has a property “Taste”, Apple will have it too. Apple can declare a new property “Color” on itself which its parent class Food will not have. To access the Color property you need to cast whatever object reference you have to Apple.

Casting is like a question. Is this object an apple? a car? a person? and if so, you can use the result of the cast to access properties and methods which belong to it. You would not cast Apple to Food, because it is Food already. if the thing you cast is invalid (nullptr) it will fail the cast as well, being nothing.

1 Like

Thank you for the reply! I did not know what the expose on spawn option existed, and I am learning still and trying to get a grasp on how to use interfaces but expose on spawn works great for now.

Thank you again!:slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.