Implementing A Flyweight System in Blueprints
The Problem: How to Properly Implement?
The first task was to figure out what types of tools were at my disposal to make a flyweight system in Unreal using blueprints. I was very lost when starting out, so I started with how I would make such a pattern in C++. This would include making a custom static structure that would be shared between all instances of the tower class. Furthermore, at each child, I could derive a custom flyweight object for each type. First, I looked into whether Blueprints supported static variables and unfortunately, I didn't find a satisfying answer there.
My next idea was to find a blueprint that would persist throughout the entire level and simply jam all those tower values in there. This definitely wasn't an "elegant" solution, and it had additional potential problems as well. Each tower would need to be added to this central blueprint and custom functions to retrieve their specific data would need to be added. Default values would have to be set on this arbitrary blueprint, causing potential headaches down the line. Making quick iterations/tweaks to values might be simple enough if one remembered where to go for the tower in question. Overall, I just didn't like this solution and new there had to be a better way.
The Solution: Introducing Primary Data Assets & Data Assets
A primary data asset (aka: PDA) can be used to store the default values for any given type of tower. This is like the "blueprint" for towers. A data asset (aka: DA) is an instantiation of said primary data asset. Each tower directly references the singular data asset. The nice thing about this, is that other systems can directly tap into the data assets to provide buffs or debuffs across all towers! To support this feature, I had to go back and replace all references to old values with function calls to get the actual value. For most towers, this is simply calling a reference to the data asset value. However, this system will make it easy to add temporary buffs to towers.
The structure of my PDA system is as follows:
Comments
Post a Comment