Using same variable in multiple blueprints

I have a string variable in a blueprint that holds a random number 1-6 which is a dice roll I set up. How do I use the same exact string variable in a different blueprint? I am wanting to do this to do a certain condition depending on the random number but I need the random number in another blueprint. I don’t need help with anything else, just using the variable in 2 blueprints.

Depends

Where is the Logic that rolls the dice? Because you have to send info from there to the BP of your interest

There are plenty ways to get this to work but again - this depends :smiley:

If there’s only one instance of your selected BP exists on the Level (regardless if placed or spawned at runtime) you can use “Get Actor of Class” and select the BP - the output Pin contains this Blueprint and now you can change its variables, run functions, modify components etc.

If there are multiple instances of same BP and those also exist in the Level - “Get All Actors of Class” and you will get an Array of all existing BP’s you selected in the Node

Other possibility is when you f.ex. Spawn an Actor - it also returns blue output pin referencing to what you just spawned - you can right click that blue pin and “Promote to Variable” and from this point you can use that newly created Variable to modify content of it

Variable can be any Object - your Player Pawn, Texture, Any of your Blueprints, Enum and more

So that means you can spawn and set to a variable to use it as a tiny node instead of pulling long lines called Spaghetti

Edit:
To get your Player Pawn - “Get Player Pawn” → Cast to YOUR_PLAYER_PAWN_BP

To get your Player Controller (if you created your own) - “Get Player Controller” → Cast to YOUR_PCONTROLLER

Hi there,
Are you familiar with Blueprint communication techniques? If both blueprints exist in the level during playtime and both contains the same variable, you can send the information to other blueprints using different methods after the dice roll. For instance, save the string in the main blueprint, then get a reference to the second actor (using get actor of class) and send the information.

1 Like

no i am very new to unreal engine

Hey @JFMGamingYT!

If you are just wanting to communicate with two blueprints, check out this non-Epic affiliated tutorial that will cover what methods you need to use:

I hope the above solution works for you!

1 Like

One of my favourite Channels :smile:

You could just store the variable in your game instance and just call it from there from anywhere. It will always be set there until you change it or close the game. If you wish to maintain that variable after closing the game, you could create a save game in your game instance and call it when you open the game for the first time and set the variable again when the game starts and save it when it’s appropriate during gameplay so you can retrieve it later when you reopen the game.