Download

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

Was going to pass on this thread. Then I read your biog and had a change of heart. I’m not going to answer your question directly, not yet. I’m going to offer some advice and background links for you to try to work through first. Also a gentle warning. Learning game dev / coding through google / youtube doesn’t work. If you can’t learn from Epic’s learning site, you’re better off taking projects apart. Overall, Casting is relatively easy (learning to ride a bicycle concept), its just that its often explained really badly in lots of places, especially by micos on youtube. But I’ll try and point you to threads that count.

The most important thing to take away is, all objects in Unreal are connected to each other via an object model. When getting used to Casting, it can help sometimes to right-click on the object-input and the blue-output of a Cast-To node. Then click on Promote-To-Variable and then look at what is expected. When you see actual realized references to variables, its often easier to see what you have to solve and so may help take the mystery away. But again taking projects apart is key. So search for some interesting Community-Tools / Marketplace packs, as that’s the fastest way to learn, if you can work that way.

Also it can help to learn Interfaces and Direct-References now (Epic’s Zak Parish BP comms tutorial). If you can learn Interfaces and Direct-References you can eliminate most Casting in your code. So why does Casting exist, or why does Epic make it so hard to access everything unlike simpler programming languages? The short answer is, it has to do with OOP (wiki that). But for now… Imagine you work in a Law library stacked with books from floor to ceiling. Casting is like the ladder to get to each level of books. But in this case the books have to be arranged in relation to each other in order for Casting to work.

It may help to read other devs struggles with Casting including my own. So see how you get on with the links below. But instead of making the fatal mistake of focusing too much on just solving this one Cast, and then tripping up again on the next. Solve this for good. Taking UI projects apart may help the most, as some of this is about muscle memory and repetition. Its also important to remember Get object helpers you get for free. :wink:

1 Like

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.