How do I "Cast to" these Blueprints?
This is just a general casting question so I can better understand things....
So I can "Cast To" things like my player, my game mode, my game instance easily through setups like the ones seen on the left hand side in the pic below. How, though, can I easily cast to OTHER blueprint actors I wish to cast to (like a widget blueprint that I created)? For example, let's say I wanted to access "cast to" a widget blueprint I created so I could modify some variables within it....what can I input as the "Object" of the "Cast To" node?
As another "general understanding" type of question pertaining to casting, when I AM inputting nodes such as "Get Player Character," "Get Game Mode," and "Get Game Instance" into the "Object" inputs of the various "Cast To" nodes seen below....exactly WHAT am I inputting there? Just a general reference to that particular parent class? Why are those needed?
Thanks in advance!
asked Mar 08 '19 at 05:44 PM in Blueprint Scripting
"Casting" is a programming term used in object oriented programming. A more non-programmer name for "Cast to" could be "Treat as". Before you can understand Casting you need to know what an Object is.
What is an Object?
Every Blueprint/Class needs to be Spawned/Created before you can use it. This is called Instantiation. When a Blueprint is spawned using the "SpawnActorOfClass" node you will notice it has a return value on the right. This is the constructed object reference normally simply referred to as the object.
Why is "Casting" even necessary?
Lets use an existing Class hierarchy in Unreal
The Actor Class has a function called ApplyDamage The Pawn Class has a function called PawnMakeNoise The Character Class has a function called Jump.
Now lets say you have spawned 3 different objects. One Actor, one Pawn, and one Character.
You can call ApplyDamage on all of them because each one "IS A Actor" You can call PawnMakeNoise on the Pawn and the Character because each one "IS A Pawn". You can call Jump on only the Character because it is the only one that "IS A Character"
Now what if the Actor object is actually a RedExplodingBarrel BP derived from Actor and you want to call the Custom Event "Explode" you made which has a GetOverlappingActors with a filter of Pawn because we want to be able to detect anything that derives from Pawn including Pawns. This function only returns Actor references.
Why is it returning Actor references and not Pawn references when I set the filter to Pawns?
That is because the GetOverlappingActors function is made in the Actor class and the Actor class has no idea what a Pawn is because a Actor "IS NOT A Pawn". It can only return its own type or a type it is derived from.
But I want both the Character and the Pawn to call the function "PawnMakeNoise" and the Character should Jump into the air
This is where Casting comes to the rescue. lets say GetOverlappingActors detected both the Pawn and the Character and returned two Actor references. Now you know that they are both Pawns but GetOverlappingActors doesn't know so you CastToPawn and call the PawnMakeNoise function that you wanted. If one of the returned Actors was not a Pawn the cast would fail and nothing would happen. Now you also wanted the Character to jump so you now need to CastToCharacter and if it doesn't fail you can call Jump on the Character. Another scenario would be if you just want to ApplyDamage to the Pawn and the Character then you would not need to Cast at all since ApplyDamage is a function on the Actor class so all you need is a Actor references which GetOverlappingActors already provide. Hope this helps.
answered Mar 08 '19 at 08:15 PM
In general with huds you would want to modify the data inside the hud event graph itself. so inside the hud bp you would get a reference to the character or whatever graph has the variable you are trying to reference. I believe if you make the variables discoverable you can reference them at will on any blueprint without casting to that object character w/e.
The cast to is the object reference and the wildcard is the specific item if that makes sense. Example Cast to third person character =>= wildcard player character. in this example tp character is just a class (because there can be more than 1 tpcharcater in the game if that is a class of ai or whatever) and the specific character is the player character.
in your example before the cast to widget you need to add the widget component before you can cast to it iirc. try adding the widget then cast to it or go into the widget bp and cast to whatever bp you have that has the variable you are trying to modify. Hope this helps
Follow this question
Once you sign in you will be able to subscribe for any updates here