Cast to another actor -> What exactly does Object refer to

The cast node borns from object oriented languages. As you probably know you can create a class who contain some data and from that class you can create a child class that inherit all the previous data and can implement or override other data.

For example: you can create a weapon class that contain damage, weight and cosmetic data. From weapon class you can create child classes such “sword”, “mace” and “flail” wich will contain the same variables and methods of weapon class. You can add new variables and methods as you can override existing ones for each child class. For example let’s suppose we add the “swing” event to flail class.

Now, if I need to store a generic weapon reference I need a weapon reference variable (ref variable are light blue in BP) and I can store it even if it’s a child of it (sword, mace, flail from the example) BUT obviously I can ONLY call weapon_class variables and methods until I cast it to the matched child class. To close the circle: if I’ve stored my flail into my weapon variable and I want to call the swing event I need to cast it to flail to invoke the method.

Back to your example: you are using the cast node properly for the game mode but the second cast you did isn’t correct, you have to previously create an actor (that is a child class of object) from your BP_Ground class and then use the reference to that object to call the Set Text function.

Hey, I have been trying to learn UE4 the last few days.

One thing I can’t get my head around however is how exactly to “cast to” other actors, irrelevant of the actors type.

I have seen many examples that use very specific blueprints like your player or gamemode, but none of that really help me in my situation. In my current project I am trying to cast to an actor I am using as ground for my level. On this actor is a TextRender component, whose text I want to update from the blueprint I am currently in.

Could someone explain to me how to determine what object I need to use, preferably with my current example in mind?

If the script you’re showing is in the Level Blueprint and you placed the Ground Actor in the level, you do not need to cast at all. You can drag & drop the reference of the Ground Actor from the Level into the Graph and access its components - the Text Render you mention.

Casting is needed when you’re unsure what class you’re dealing with. For example, you’re doing some casting to the custom BP Game Mode - a custom Game Mode you created and set up yourself, you extended a generic one by creating a child. By using Get Game Mode, you’re telling the engine you’re about to access a GameMode, by casting you’re telling the engine which particular version of the GameMode you need - the one you made.

The Blueprint I am in is just another actor.

So what do I need to do if I want to access something that isn’t a gamemode? And why?

I honestly don’t get anything after “To close the circle”.
Why would I need to castto when the swing event was already created in the flail class?
Why and how do I have to create a Childactor of my Actor Bluepring and how do I then reference that?

Sorry to bother you with my inexperience, but right now this entire function just seems completely alien to me and everything I try to learn about it only seems to make that worse.

The Blueprint I am in is just another

Where is that another actor? Placed manually in the Level Blueprint or spawned dynamically via the Spawn Actor From Class node?

So what do I need to do if I want to
access something that isn’t a
gamemode? And why?

Generally speaking when you create an object, you’re supposed to store a reference. Spawn Actor From Class node has a Return Value pin which you can right click and promote to variable - this automates the process of creating a reference.

Since the Game Mode is a globally accessible class (Get GameModeCast To), available from anywhere and anytime, it’s not a bad idea to spawn objects and keep their references there.

Isn’t a problem, I’m in lunch break
“Why would I need to cast to when the swing event was already created in the flail class?”

because that what we have is a Weapon reference, not a Flail reference. Even if you create a Flail object you can reference it to the parent class. Since Flail inherit from Weapon you can store a Flail object reference into a Weapon reference variable (object oriented languages rules, more in deep is related to memory addresses). But since we are using a Weapon ref we can’t use the Swing method because that method isn’t in the weapon class but in the Flail class. The cast node is used when you have a reference to something but you need to get data from a specific class wich inherit from it.

“Why and how do I have to create a Childactor of my Actor Bluepring and how do I then reference that?”

I can more easly answer using an example:

The Game Mode is a class used to define the rules of your game. Obviously YOU have to define the rules of your own game, so you have to program your own game mode. Since the game mode is REQUIRED in every map/level, you have to create your custom game mode. If you have different levels with different rules (deathmatch, capture, the flag etc) you need to create multiple class wich inherit from Game Mode class and set the game modes into your levels (only 1 for level). Also, if I make a game where the character can dual wield/akimbo I could need to store both weapons references. Since i can’t know first wich weapons my character will have (if there’s multiple weapons in the game) I can’t have a Sword/Mace/Flail/Club/Estoc/BastardSword/Scythe etc object references into my character BP. I will, instead, set a 2 Weapon reference variables where I can store my weapons data. I can invoke every method and get any variable contained into weapon class. If you don’t have any child class specific method or variable you don’t even need to cast.
How to reference an object in the next reply since I’m running out of chars

The object refers directly to the blueprint component you wish to make changes too. For example: If I have a character with a custom event and I want to activate that from another blueprint, I have to cast to the blueprint, and then make a reference to that blueprint and plug that into the object. Here is an example of how I get it to work:

From my player blueprint I cast to the cowBP and call a custom event. I have a reference to the cow plugged into object


My reference is to the CowBP (simply type the name of the bp you wish to cast to, and select object reference as a single variable). Make sure it is a public variable. (check “instance editable”)

Now close the blueprint and go to the setting of the character/object blueprint you used to cast from on the right side of the screen. Under the default tab, select the eyedropper icon and use it to click on the object you have used as your reference inside of your game world.

Now save your world and compile and save the blueprint you have the reference in. It should now work.
I hope this helps :slight_smile:

Should you be casting from a level blueprint, let me know, I will explain how to do that. It is slightly different.

To get references there’s tens of ways but before you need to know how class and object works:

When you define a class you have a “mold” that you can use to create objects. Every object you create from a class will be stored into your memory. A reference is a pointer to the object. When you spawn an actor (or construct an object, but spawning is generally better) you can set 3 properties:

the transform: where the actor have to spawn
the collision handling: define how/if the actor have to spawn in case of overlap with other actors
the owner: the player controller who own this actor; more useful in multiplayer games but you can still use it even in single player for store the ref of an actor you want to “own” your just spawned actor.

(these properties are expose into the actor class)

also you can set expose on spawn your own variables (check your variables properties) to appear into the spawn actor from class node.

If you are checking for an actor who already exist in the level but you aren’t programming the level blueprint you can use an Interface (not the graphic one lol) or a Dispatcher to get its reference, but since I consider Interface and Dispatcher as intermediate/advanced function I can’t really explain them in this place. BTW a more simple way is the “Get all actor of class node” who return an array that contains all the references to objects of a defined class that are already spawned in the level, from it you can get an index or all (via loop) to obtain the reference of your objects.

It seems that your cow reference does not need a cast at all in this instance (see note) or is this just an example for the OP?

Thanks, I will remember that for future projects

This is the solution, not advice for a future project. It’s both, actually. That’s until you need to start using Event Dispatchers and Interfaces because things will get way more complicated.

OOP is at the very core of blueprint programming (and pretty much any other programming language that matters these days). Worth a read, even of you’re not a classic programmer.

Back to the topic at hand:

You must have created the another actor somewhere, and this is where the reference to your missing casting object is - the one from the very topic.

Good luck!

It worked, thanks alot ^^

If there is one thing I have learned from you, then it’s that I still have ALOT to learn about developement … then again, I guess that is always the case, even for more experienced people ^^
Anyways, I will definitely remember the GetAllActorsOfClass node, thanks for your help

This may be of help to you. I explain how and why you use casting at a beginner level.