Blueprint Variable Casting Problem

So I am trying to test out how to cast a variable from one blueprint to another with using casting. What I trying to do i pull a variable value from my character class, and use it in my playercontroller class. A side note is that obviously the playercontroller class is not added to the scene prior to game start, and so I can’t reference it through the default value menu. Therefore, I am trying the casting method of doing it. Below is the way I tried to cast for testing purposes. Everytime I get a cast failed. So I am wondering what I am doing wrong. I am obviously missing something simple since this should be a breeze.

Please help. Thanks for any questions or answers given in advance.

Just a quick explination on how I did this network. I first created the refence variable by making a new actor variable and naming it reference. Then I dragged the cast to MyCharacter pin from that and then pulled another pin to find the “Sine Bullet Rate” variable found in the MyCharacter class. Then I wanted to print the values to see if the cast worked properly. Everytime I run the scene I get a cast failed message. Please help. Thanks!

Your Reference is always ‘None’ or ‘Undefined’. SO it wont cast to any object which is why its failing. The variable being casted must point to an existing object for it to work.

You can use the Is Valid node to see if your Object reference is valid or not.

It seems to me that you are trying to reference your Player Character. The best option here would be to use Get Player Pawn from Controller and then cast it to MyCharacter.
Another option would be to select all instances of MyCharacter from the scene using Get Objects of Class node. Then iterate through the result using a for-each.

You were correct. It seemed that I was trying to reference the player pawn but didn’t know how to get the Get Player Pawn Node. I replaced the Reference node with that, and bingo. Perfect. Thanks!

Hi dougbott,

I believe you are off by one node. The reference variable you are trying to cast to MyCharacter should be a Get ContolledPawn node. The reason for this is to be able to successfully cast to your character you need to get an blueprint/object that is interacting with or (looking at) your character.

I would also suggest changing the Event Tick node out for something less memory intensive. I’m not sure exactly how you plan to apply this setup, just make certain you really need an Event Tick before using it. If you are trying to do something every so often I would suggest using a Timeline.

10684-ahub_castingissue.jpg

Thanks, TJ

Yes, You are correct as well. The Tick was just for testing purposes. Thanks!

One other question if you don’t mind me asking. If I wasn’t casting from the player pawn and instead was trying to grab a variable from a random blueprint that wasn’t part of the scene. How would I go about that? Thanks.

yo dougbott,
for getting any other blueprint in the level i often use the “GetAllActorsofClass” (i think thats the name) it will return an array of all those particular actors, which is really only handy if you have only one of them or if you know its name or something else to cross check.

alternatively.

i make actors ‘register’ themselves to some custom variable in my gamemode class. the only reason i do this is that every class extended from actor has the function GetGameMode which then you typecast to your used gamemode class and access said custom variable via that. :slight_smile:

also just save that ‘MyCharacter’ as a variable for later access so you dont have that process intensive typecasting every tick.

Would you mind showing an example of that alternate method? I can’t for the life of me figure out casting from actor blueprint to actor blueprint.

Hi TheDiceParadox,

The above images should work for a majority of casting. Can you show me a screenshot of what you have? I might be able to see what is occurring.