Download

Casting to custom blueprint

Hello guys,

Sorry if this question has already been answered but i’m still stuck after reading lots of thing on this.

Basically i have created a new blueprint “actor” and setted a variable in it named “CompassDirection” (the purpose is to do a compass), i’d like to show it on my screen through an UMG widget (which is already showing few things).

Here is the content of my “CompassBP” (which is added to the world of course), pretty simple :
4216c004f99291c1314c36919329c48b9af48cc0.png

Next i’ve created my “text” object in my HUD widget, and created a binder.
I have also added a “cast to” node to my CompassClass in the event constructor, to be able to get my “CompassDirection” variable, but the casting fail each time and i don’t know why :frowning:

Event construct : the “Cast to FirstPersonCharacter” is here for others things showed in my HUD (which are working perfectly without the failing cast) :
f64b567470ced253d7f23977e617bbcc53916c79.png

Binder : Nothing special here, the goal is to get the variable from the compassBP, the editor find it easily in context sensitive mode… :
c10fd294f21d2e31f8bcf50e444aebad5b294a6c.png

I’m sure i’m close to the solution, i think it may be related to the “Object” in the “Cast To” node, but the only thing i found to put here is the CompassBP blueprint itself… I’ve also tried to put the “Get Player character” output but it doesn’t work either… I think i need a new explanation on this Cast To node, i’m missing something :stuck_out_tongue:

I hope my problem is easy to understand !

Thanks for your help !

//Arc

Cast is like Type Casting in programming where you are changing an object of 1 type to another like a subclass (PlayerController -> Custom PlayerController). That doesn’t seem like the case here.

Which blueprint is the event construct node coming from? Is it the CompassBP itself? You don’t need cast in that case and you would just set it. But yeah you’re probably right, it may be related to the object pin in the cast. If Event Construct is NOT in CompassBP then you need to get a reference to your CompassBP.

You could do what I do and store any variables that need public access in your game mode, and then you can just cast to game mode with ‘get game mode’ as the object.

Unless it’s a multiplayer game and suddenly the clients don’t have access to game mode. If they’re player related I’d go with player state since that’s replicated. I actually did a compass and stored the direction the player is facing in the player state. All the logic was in the player class. I since removed the feature.

Thanks for your answers, at the beginning the compass logic was in the player Character blueprint (which is also “casted” in my HUD) and this was working fine.

Now i just want to make it independant to make migrations easier, it’s applicable in this case but i expect to have many others blueprint like that.

So the goal is not to store the variables into MyGame for example (which is a good idea btw) but to understand here why the Casting is not working ^^

The “Event construct” is in my HUD widget, that’s why i need to retrieve the compass direction from its blueprint.

//Arc

Then you need to spawn your CompassBP in the level and get a reference to it in your HUD widget. Or somehow attach it to your player. Some way or another. You’re casting from nothing.

Hello,

CompassBP is indeed in the level, if i put a simple print directly in the BP it will show on the screen.

The reference is what i’m trying to achieve in the HUD widget, that’s the goal of the cast here right ? Or maybe there is another way…
This is the “easiest” way i know to access another blueprint functions, variables…

//Arc

Hello,
As you have only one compass_Bp in your level, you can do a “get all actors”, selecting “compass_Bp”, drag a “get” with index 0 and link it to the "cast to compass_Bp blue input.

Oh i didn’t think of that, thanks Fen i will try !

So my problem would be the input actor missing ?

Tiens un autre frenchie, je suis pas le seul :wink:

Yep, you need something to smash on, or you smash on yourself :stuck_out_tongue:

Si tu veux d’autres frenchies : Unreal engine 4 communauté Française sur fb, avec un steam speak et un mini fofo, pas super actif mais super sympa et skype aussi bien sûr.

Thanks a lot it’s working :slight_smile:

Result :
fd4cb4c54180749d67ea286cf947a1f9da2c276d.png

Literally how could we translate the “cast to” flow ? I mean with words ^^ It often helps to understand easily how it’s working ^^

Ok you don’t need the Cast when you directly get the BP from Get all actors of class. That object is already of type CompassBP.

Now i realize that as you ever have determined the class in your “get all actors”, you no more need the “cast to” and can directly link the output of “get” to your “set my compass”.

literally : fit the mold. Compare the type of the selected actor (the one in input of “cast to”) with a specific type of actor (the one of “cast to”) if types are identical, all possibilities of the actor (functions, variables) are available.
literally : couler dans le moule. Comparer le type d’acteur sélectionné (celui en input du “cast to”) avec un type d’acteur spécifique (celui du “cast to”) Si les deux types sont identiques, toutes les possibilités de l’acteur (fonctions, variables) sont disponibles. (no, i’ll not apologize for my french ! :slight_smile: )

Hum ok i think i start to understand the purpose of this, but what do you mean by the “type” of actor ?

For example if, in my widget HUD, i “compare” my CompassBP which is an actor class (if you mean this by “type”) to another actor class which contains a random function, since it’s the same “actor class” type, CompassBP will be able to access the function ? But the widget HUD will still “work” with the CompassBP, right ? Something like that ? :wink:
Counterpart would be to “compare” the CompassBP with the PlayerCharacter BP, which is in my case a child of the Sandbox.SandboxCharacter one, this casting should fail, isn’t it ?

It seems “complicated” to understand, but i’m sure it’s easy afterall, i just need to understand the logic of this thing ^^

The type is the variable type. The one you select when you create a variable in details. The precision of type you use define things you will be able to do with.
Let say that you have a blueprint parent with 2 children.
If you cast to actor, you’ll check that your variable is an actor and have access to generic actor abilities only. But all actors will be ok.
If you cast to parent blueprint, you’ll can access to all functions and variables created in parent (which is the idea for an inventory system for example) for both children. But only those ones.
If you cast to first child, second child will return fail but you’ll can acccess with first child to all functions, variables set in parent and in first child.

Oh gosh i’m kind of lost :cool:

  1. I’ve removed the “cast to” node in my widget HUD (only the Get all actors of Class, GET (index 0) and SET for the local variable stay), indeed it’s working too.
  2. If i understand i can now access everything in my CompassBP blueprint only with this ?
  3. Which variable are you talking about ? The one i’m setting in the HUD (eg. “MyCompass”) ? The Type is “CompassBP”, which is the class… It’s not something “common” like a Float for example.
  4. Ok i’ve just found that there is a load of different “cast to” nodes, but… why i don’t need it anymore in my example ? The purpose was to access to functions and variables in the “external” blueprint, and now i can access to them with a simple “get actor” lol…

Je pense qu’il faut que tu m’expliques sur Skype, je m’embrouille pour rien je pense !

Sure for skype, add me ^^

I dunno if this will help you or not. “Cast to” node is Type Conversion. A programming term: http://en.wikipedia.org/wiki/Type_conversion

Like, if I want to get my current player, I would use “cast to” to my current player class. So in UE it’s something like this:

Get Player Pawn -> Cast to “MyCustomCharacterBP” -> Set Property.

In your case, you’re already getting the custom class you need right from the “Get all actors of class”. So you’re done. No cast needed. In the above we’re getting a reference to our player pawn. So you have to convert its type to your custom subclass (should be a child of the type, “Get Player Pawn” returns Pawn class so your custom subclass must be a child of Pawn…)