Hello,
I made some DataAsstes for Pickup Items. My inventroy has an array of dataassets.
If I wand to save the character, will it work or do I need an extra class for the inventory with the
same attributes like the data asset and copy the data to this class?
You’ve just discovered the problem with data assets. You can’t save them.
Unfortunately, you have to convert to structs and put them in the save game… :-/
Ok, as i thought. But strange that they don’t have a nice solution for this kind of data.
Datatables are also crap.
You can save them, just tested it.
I’d love to be wrong, can you show that?
I can only save a reference…
I think structures are supposed to be it, but they’re very unwieldy… ( in blueprint ).
Yeah, that’s what I mean. If you’re trying to save modifications to that (like changing a variable in the data asset), it’s not going to work (or at least not the way it’s supposed to).
Here’s what I’m doing.
Edit: If you change a variable in the data asset through blueprint, it will modify the asset itself (the editor asset). Don’t know what will happen in a packaged game, but it shows it just saves the reference.
Ok, wait. I was talking about objects ( yet another way of doing this ), but I assume data asset is similar…
Yes, and that’s the problem. What’s the point of having this great way of managing data/object/assets, if you can’t save it?..
I think they intended data assets for static data (items, weapons, enemy stats, etc.), then use structs for runtime data (health, modifications, etc.).
Ok but i wouldnt use structs, the cool thing about data assets are that you can derive.
I have a base data asset and then different kinds of items based on the base asset.
Or do i have to make one big merged struct to save data?
No, you can just save it directly. Remember, it’s just saving a reference to the data asset, not the data inside it.
Ok, maybe we missunderstood. I have a character inventory.
I have PickupItems in the world, there i can assing DataAssets, maybe a box of bullets with 20 bullets.
Now i store the data asset in the inventory. Now maybe i reload the weapon and 10 bullets left.
If I save the reference to the asset, will mean i have the initial value of 20.
So i seems i need to copy the data to save it.
It should work if i do the same class structure and save it.
i have
class LOSTSIGNAL_API UItemDataAssetBase : public UDataAsset
class LOSTSIGNAL_API UWeapon : public UItemDataAssetBase
I would make new classes
UItembase : UObject
UWeaponItem : UItembase
with the same properties like the data asset and store an array of Itembase instead of UItemDataAssetBase.
Yeah, that’s the case. DataAssets are just editor assets that store data; they are just assets, not runtime “containers” like structs are. If you need to save ammo, etc., you need a struct.
Ok, but really a struct or does it also work with classes.
Ok isnt an Actor, my base class is UObject in Blueprint i would create an instance of this class and
copy the data. The serializer hopefully grab all public members and store them.
I’ll try this but have to restructure a bit, will take a while
For level save it saves the reference of the actors on loading the Level i destroy all actors in the list, this works fine.
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:
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.
Hey, I know it’s a bit old topic.
But if I create a DataAsset that stores in it a struct, would that struct be able to change during runtime and be saveable?
if you mean change a struct in a dataasset yes you can but be aware this will affect all references to the dataasset
if you mean save a data asset in a struct then also yes you can