Cast to another actor -> What exactly does Object refer to
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?
asked Aug 01 '18 at 09:38 AM in Blueprint Scripting
Asato of Vinheim
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.
answered Aug 01 '18 at 10:25 AM
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 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 :)
answered Aug 01 '18 at 11:39 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here