Can I share one variable among all my blueprints?

I have a INT variable that I would like to have increased whenever something is destroyed a certain way. I also want the same variable to be use as a currency for the shop widget. Any help on this is appreciated.

Yep! Not hard at all. Just create the variable in your player character blueprint, your player controller, your game instance etc wherever you want to actually hold that information and “reference” it when something is killed or when you want to use it as currency to spend in the shop. So basically the logic is this:

  1. Create the variable say in Player Character
  2. When Player kills an enemy from the enemy blueprint, you would cast to Player character, or have a reference to player character saved from a previous cast
  3. From the cast, drag off the object pin and “get” the integer variable you created
  4. Perform whatever operations you want on the variable
  5. “Set” the variable to the new value
  6. Now the variable has changed by whatever amount you chose and is “stored” on the Player Character
  7. In the shop widget, once again, cast to the player character or get a reference
  8. Pull off the object pin from the cast or directly off the reference and “get” the integer variable
  9. If something was purchased, write your script logic out for however much it costs and subtract from the integer variable
  10. “Set” the integer variable once again

Now you have manipulated the same variable from 2 different places. Killing enemies increases the value, going to the shop and purchasing things decreases the value.

Hope this helps.

There are many ways to skin a cat, and Nebula Games’ way is a perfect solution to this and will definitely give you the result you want. I would just offer another solution, as it’s the way I prefer to work and that is using interfaces.

If you create and store the variable you want to use somewhere, we’ll use game mode for this example. Then create an interface class that has 2 functions in it, I created a simple add currency and remove currency function, and then add that interface to your class that has the variable stored (game mode). You can then just do a simple interface call from any actor you want to alter that variable, say a coin that you pick up.

I used game mode as an example, one because it is a place I like to store game wide variables, but also because you can get a reference to it without having to cast and I like to avoid as much hard referencing as possible. This means the actor that is updating the variable basically doesn’t care what is done with the information, it’s just telling any class that’s listening for the update that it has happened, and they can do with the information what they will.

As I said, the results will be the same, but…

[“The More You Know!”][3]

Just started getting into interfaces myself, that’s a pretty elegant way to use them!

Thank you so much! This helped me finish my game and work on the next one that I want to make.