Blueprints Communication : Making Blueprints to share variables with each other.
As a beginner in Blueprint scripting, working with blueprints sometimes becomes very frustrating in certain situations when we try to access a variable defined in some other Blueprint. When we talk about Blueprints Communication, we need to follow certain steps to do it easily and effectively. Let us first understand the goal of this post that we will be going to achieve eventually in certain steps.
Blueprints Communication for sharing variables
Lets say we have created two Blueprints in our project. There is a variable that we have defined in our first Blueprint. Later there is an event happening in second Blueprint which is changing the value of that variable in our first Blueprint. To do this, we can not get direct access to make changes in variable. We will create object of the Blueprints and then use it to get/set variables present in each other. We will be using Character Blueprint as a medium for communication between our two Blueprints. We will also be learning about common mistakes that you can make as a beginner . Lets start it and follow these steps :
- First of all create a Blueprint of Actor type. (name used here is BP_One)
- Create a new variable inside Event Graph of the above Blueprint. (variable created here is Variable_In_BP_One). Now first of all, we want to access this variable from our Character Blueprint e.g in ThirdPersonCharacter or FirstPersonCharacter Blueprint.
- In Character Blueprint Event Graph, we will try to Cast to BP_One Blueprint to get access to variable created in second step.
As you can see that Casting to BP_One gives an error because the object is undetermined. So we need to create an object of BP_One that will be used as target for SET node.
- Create a new variable in Character Blueprint and set its type to be : BP_One Object.
So now we will be having a variable of BP_One Object type that we can use to Set the “Variable In BP_One”
Now most of the people make mistake here as they think that now using this object they can get / set variables or execute functions present in BP_One’s Blueprint. But when you compile and run this in editor, the value will not be set and following error pop up on closing editor. Make sure you have added the newly created Blueprint into the Level of your project.
This error means that you have added the object variable for BP_One but it is having no value. Before using this object , its value should be set. Otherwise this object remains invalid.
- Now we will set this object’s value in our BP_One Blueprint. First of all in Event BeginPlay node, Cast to your Character blueprint. Right click on screen and search for “Get Player Character”, select this node and link it as input to Object in Casting node. Now we have reference to our Character Blueprint. Using this reference we will now set the value of BP_One’s object present in Character Blueprint. For the value we will take reference to self. It will become clear with following screenshot.
Now when you will compile and run, the same error will be shown. The problem now is that the ThirdPersonCharacter Blueprints’ Event BeginPlay is executing first before the value of BP_One Object is set in BP_One Blueprint. As this example was for testing purpose, so we can add a delay to avoid this error now.
Now we are well aware of how the blueprints communication taking place. Lets take it to one step further and set this variable in another Blueprint using the same object.
- Create a new Blueprint eg. BP_Two.
In this blueprint, we will perform same step as in step 5 for casting to ThirdPersonCharacter or any other character that you are using. Using character reference we will search for BP_One_Object. Using this Object we will get/set any variable or execute any function present in our BP_One Blueprint.
These are the steps that you can follow to do blueprints communication in Unreal Engine 4 and avoid some most common mistakes that a beginner can do.
PS : If you keep getting the error as in step-4, please make sure that you have added the Blueprints into your level.
Leave any suggestions or doubts in comments. I will be more than happy to hear from you.
For version 4.9 and later, check this post .
Happy Game Making !! 🙂