Search code examples
unreal-engine4unreal-blueprint

Unable to increment a variable present in another blueprint in unreal


I'm building a FPS game using built-in template which can shoot bottles.I used on overlap event for the bottle such that when the player projectile overlaps it then it will explode by using spawn actor from class explosion effect after which both bottle and projectile will disappear.But the problem is i need to keep track of number of bottles exploded so that i can assign score to the player.I have created an integer variable Count in third person projectile blueprint and initialized with 0.I was able to access it in Target Bottle blueprint and increment it whenever the player shoots the bottle and explosion occurs.But the problem is when i shoot a bottle the Count variable is incremented to 1 when i shoot another bottle it is still 1 and not becoming 2 and so on.

This is my TargetBottle blueprint

Count variable in Projectile class

Count variable in Projectile BluePrint enter image description here


Solution

  • When you store information such as variables in a blueprint class, these variables are only "alive" and specific to that instance of the object. Imagine every time you spawn a bottle, the variables in that bottle, such as "count" is initialized and specific to that bottle. When a new bottle is created, ITS own variable count is initialized.

    This is one of the pillars of object-oriented programming, where every object, in your case an actor, can keep its own private information about just itself. This is wonderful because not every instance of an actor will keep the same information. It's a little harder to imagine that in your case because from what I can see bottle does not need to hold any information so far. But imagine if you had a blueprint class where it was a character, and it had a variable "Name", then every actor should stored its OWN name, and not be allowed to access names of other actors, unless specifically designed that way.

    If you want to store a variable that is NOT specific to an object, such as a counter like yours, then one way to do it is to create a game instance. You can do this by going to the same tab where you create a blueprint, but search GameInstance at the bottom and select it. Picture of creating a GameInstance class

    Then, you can create and store variables that are global to your game there. The way a GameInstance works is that it is intended to store all the variables that you need to access or change from all other classes (blueprints) of that game during run-time. These variables will reset back to default once you restart your game. However it fits your purpose very well.

    Now just create a variable int in GameInstance, and give it the default value you want, in this case I'm assuming 0.

    Now you can access variables in your GameInstance simply by casting to it, and the object would be "get game instance". Then you can access, modify and whatever else you need with the variables stored in your game instance. Example down below.

    Picture of example of using accessing variables in your GameInstance

    Happy game developing and hope this helped!