Casting Question

What goes in the ‘object’ pin. I know it’s a reference to the object your casting to, but how do you make that reference?

I’ve tried every method I could find on Google, but nothing seems to work.

I want to access a variable in another blueprint from my player controller and I can get anything to work. If it was the other way around, I could ‘get player controller’ and plug it into the object, but I need the player controller to see a variable in my other blueprint.

I’ve tried creating an ‘object’ variable and setting that variable to the blueprint in question, but that cast always fails.

I’ve tried creating a class reference and then using ‘Get Class Defaults’, but that always ends up not showing the class defaults.

I’ve tried several other methods as well, but so far, nothing has worked.

Maybe I need something other than a cast, don’t know, I’m willing to use anything that will allow my player controller to read a variable in another blueprint. I don’t need to change the variable, just read it.

What is the best way to accomplish this? What is any way of accomplishing this?

Thanks

I feel exactly like a Player Controller. You tell me there is an instance of an object but I don’t know where it is. You ask to check what the object is and convert it to the requested type (Cast To) - but which object? I don’t know how you made it. :expressionless:

How & when is it created, who created it:

  • some other blueprint Spawns Actor from Class?
  • a child actor component creates an instance ?
  • it was constructed in LB by dragging it from the Content Browser to the scene?
  • how many are there? and if there’s more than one - how do I know which one you mean?

It’s usually one of the above. But the player controller does not know what you chose / did / when it happens. If there’s only 1 object, you can always…

image

…and do not even need to Cast. But what if you have 2 actor instances of the same class?

how do you make that reference

For dynamically spawned actors your generally create a new variable and point it at a specific instance of an object:

1 Like

Really appreciate the learning references. I’ve already been through most of them including Epic’s Zak Parish BP comms tutorial

I already read all the forum entries I could find on the subject. There seems to be a great deal of reluctance to explain how casting works. I suspect this is because most people don’t actually know, but I will go through the ones you specifically mentioned tonight at work. Hopefully what I need is in there.

I’ve been to the Epic Online Learning. When I search for “casting” there are four courses that show up. Two have nothing to do with casting (i.e. “Converting Blueprint to C++”) and the other two use only the very basic examples (i.e. Cast to PlayerController with GetPlayerController). I’ve been through both of those courses already, and neither taught what I’m trying to figure out.

For the moment, I’ve just moved all object variables that I need to access outside of that object to something I can easily cast to (Player Controller, GameMode, GameInstance, PlayerPawn) This seems to defeat the purpose of OOP, but it’s the only thing I can get to work. If I can’t access an object’s variables outside of that object, then I have to put those variables in a place where I can access them.

I’ll look for some market place projects to dissect. That’s a great suggestion.

Thanks for the help. I hope the answer is in the materials you linked to. I’ll give them a read tonight.

I have 26 actors of that class :sleepy:. Though I’m thinking to dispense with that mess and just make 26 individual classes so that it will be easy to cast to them, but that seems to much like giving up, and I really want to learn to do things the proper way. I have been able to store them all in an array, but I can’t access any specific ones, because I’m not able to determine which specific instance goes with which specific index in the array. I’m still working on how to do that.

For the moment, I’ve put all their variables that I need to access from the PlayerController in the PlayerController. It’s east to cast to the PlayerController, but difficult to go the other way. I have managed it so some degree, but I’m still not there yet.

I’m going to keep working on the casting thing. I’ll try the ‘promote to variable’ thing and see how that goes. Not sure if that’s the solution for me as I’m not spawning the instances, they’re in the level at the start, but maybe I can make them into a variable in each one’s beginplay or something. I’ll check it out when I get time at work tonight.

I liked UnrealEnterprise’s suggestion of taking other projects apart. I have done this some, but I could definitely go further into that.

As always, your input is much appreciated.