Data Asset and Save Game

Ok, I just dropped in this thread cuz I was like “yeah, you can save (references to) data assets,” but unaware you meant saving variables inside of them. I haven’t messed with saving outside blueprint savegames, so I haven’t touched anything dealing with saving in C++.


But with the DataAsset stuff… (If this is hard to follow, sorry, I’m sleepy.)

There was a misconception that DataAssets are for storing (runtime) gameplay data; they’re not, they’re just for storing asset data (e.g. storing the stats of a weapon, but not the current ammo count of it).

If you want to store ammo in your inventory, you need to use an array of stucts that stores the DataAsset and the ammo:
image

In this example, I store the item and an ammo counter. The ammo counter is called “AmmoUsed” which is how much ammo has been used from this item. The item itself stores the max ammo, so to get the amount of ammo left, you do Item.MaxAmmo - AmmoUsed.

The item in this example can be anything: an ammo box, a magazine, or a weapon; it doesn’t really matter. All we’re saying here is that “this is the item, and this is how much ammo has been used.”

It’s also important to note that “Item” here is not the instance of the item, but its “type” (i.e. what it is and information about it) (e.g. {ammo box, 100 rounds}, {magazine, 30 rounds}, etc.). The struct in the inventory array is the instance. So if you add two of these to an array with the same item, you would have two instances of the same item, but with separate ammo counts.


So, in your case with pickups, your data asset would store things like:

  • Mesh
  • Max Ammo
  • Particle Effect
  • Pickup & Drop Sound

The pickup actor would only store the struct above (DataAsset & AmmoUsed). In the pickup’s BeginPlay, you would set the mesh & particle effect. When you pick it up, you would play the pickup sound. When you add it to your inventory, you add it using the struct above like this. When you use the item, you add to “AmmoUsed” (which subtracts from the ammo count). When you drop the item, you would play the drop sound, spawn a pickup actor, and set its struct like this; the pickup’s BeginPlay will handle setting the mesh & the particle.

6 Likes