Array suddenly gets smaller?

Hey everyone! I have a very weird problem.

I have a Boolean Array containing 12 indexes. I have a flagging System which basically saves events that happened either true or false. So when I am initializing all I do is load the game data, get the save game data and set variables.

When I am initializing, which has a for each loop, looping through a different array and based on their values, I set the boolean at the same index to their values. When I do that, I have all 12 indexes. But now it gets weird.

I never changed anything from the array itself. There are no removes, no clears, nothing. But when I am in my LevelBP creating a debug key which prints me the length of the boolean array, it suddenly isn’t 12 anymore but 8. And for the love of god I have no idea what’s happening xD

Anyone else who encountered a similar or even the same issue with arrays suddenly becoming smaller in length?

I appreciate any help I can get

Answering some eventual questions beforehand:

  • No indexes have ever been removed
  • The Array has never been cleared
  • I deleted the Save Game Files already and remade them
  • The array has a fixed index count of 12 (13 elements). It never gets expanded or decremented
  • The array is never bigger or smaller at any given time unless I make it bigger inside my SaveGame File, increasing the amount of indexes/entries

with things like this we really need to see the code

1 Like

I honestly don’t even know what code I should show here xD.

I have 2 Arrays inside my SaveGame Object.
1 DataAsset Array
1 Boolean Array

Both have the same amount of indexes. Why? Because every time I restart either the Editor or my packaged game, the Data Assets always revert back to default and after days of searching for a solution to this, I came up with my own solution which is creating a bool array with the same amount of indexes as the Data Asset array. When I am using my Init Function (when pressing either new game or continue), all I am doing is looping through my Data Assets, get the boolean array at the same index and set if this event has happened or not. See image below.

Set Event Has happened does nothing but finding the Item to Find inside the DataAsset Array.
I get the SaveGame Data, get the Data Asset array, use the ‘Find’ Node, then with the returned index I use the Get function, set the boolean Value of the data asset to either true or false and then I set the array element. And I do the same thing with the boolean array on the same index.

I don’t think I have to show the rest because it is literally nothing more but a debug key, printing me the Size of the BooleanEvents Array.

In the first image, it prints me all 12 indexes but the debug key only prints 8. I already tried deleting the SaveGame Object multiple times but to no avail. Because if I already have a Game File and I press New Game, it will Delete the Game in Slot. And it actually deletes it too I even checked if it has been deleted locally.

Side Note: In both images, we are currently looking at code inside the Game Instance

2 things to check, is the Find successful? (not -1)
and did you Resize the arrays? if not SetArrayElement could fail since you dont have SizetoFit on

Find is successful and no, as already mentioned in my initial post, the array never gets resized, cleared, nothing. The array should always stay the size I set inside my SaveGame Object

EDIT:
Find is successful, however the way I found out about it being smaller is, that even after setting the flag, it still did something it wasn’t supposed to do if the flag has been set. In short: Find is successful, setting value fails because of the array size suddenly being smaller. But when I initialize my game, it has all 12 indexes (It’s what happens in the first picture in my post above)

Answering some eventual questions beforehand:

  • No indexes have ever been removed
  • The Array has never been cleared
  • I deleted the Save Game Files already and remade them
  • The array has a fixed index count of 12 (13 elements). It never gets expanded or decremented
  • The array is never bigger or smaller at any given time unless I make it bigger inside my SaveGame File, increasing the amount of indexes/entries

what are you looping over initially? i cant follow the logic from the screenshot.

it seems you’re finding->getting->setting the same variable on the events, which is redundant.

if you just want the index just loop over the events array. or you could event use a Map or struct to bundle the data together.

otherwise i’d say just tick SizeToFit

1 Like

its also worth verifying your DataAssets save correctly, (as in from disk not to the save game object) i swear i tested it once and it does but people tell me you cant save DataAssets as they are object references. I havent had a chance to retest it myself

Data Assets save correctly. However, every time I restart my Editor OR the packaged game, the Data Assets Values revert back to default. Also, I am very certain Data Assets CAN be saved. But I tried literally everything. As long as I don’t restart editor, the Data Assets save correctly and everything works as it should. But as soon as the Editor closes or the packaged game exits and restarts anew, everything is back to default.

I am looping over my DataAssetArray.
The Data Asset has nothing but a bool in there, setting true or false based on an event happening or not.

Ever since I had this issue with my Data Asset values reverting to default, I got ‘creative’ and just created a boolean array the same size as the Data Asset

This is how the Data Asset Array looks like:

It has a fixed amount of Events set as default values inside the SaveGame Object.

This is how the Boolean Array looks like:

badum, tz, as you expected I suppose xD

What I am basically doing is, ever since my boolean array saves properly, no matter if the Editor is closed or packaged game is closed and reopened, I save the corresponding index for both my DataAsset array and my Boolean array. (If first index in Data Assets is the ‘hasIntroTutorialReceived’ and I set this to true, I also set the first index of my boolean array to true. If e.g. my 5th index is ‘hasUnlockedABC’ and I set this to true, I also set the 5th index of my boolean array to true).

And once I initialize my game (when pressing New Game or Continue), I loop through all my Data Asset Events (which, at this point, has been reset to default meaning all values are false again even if I saved some as true) and in order to get them back to the actual values, I take my boolean array and set the values of my events at index a to the same value my boolean array has at index a.

I hope I explained it properly for you to understand what I am doing and why I am doing it.

To your initial question: Yes, I am saving it correctly as far as my understanding of data assets go. Otherwise I’d really appreciate you telling me how to correctly save Data Assets with some code or even settings example. I mean even in the Project Settings under ‘Game - Asset Manager’ I added my Data Asset in the hopes it stops resetting. As I said I did so much research to fix this problem. But people speak of a bug from Epic’s side which hasn’t been fixed and only arises ever since version 5.2 or something like that (don’t take the version callout for granted because I am not 100% sure anymore what people said). And ever since that, I started implementing the way I am going on about it right now.

I’ve been trying around again and out of whatsoever reason, it seems to work now and the debug key gives me the correct array size. I have no darn idea what just happened or what I did. Tracking back my steps, I didn’t even do anything really. I just deleted the save file over and over again until it worked.

If people encounter the same or similar issues, always check:

  • If everything necessary is compiled
  • If you override the values somewhere
  • if you actually resize the array (add or remove indexes somewhere in your code, etc.)
  • Delete the save file not just via ‘Delete Game in Slot’ node but also locally (ProjectDir/Saved/SaveGames)

@Auran131
I really appreciate your help though but please let me know about your insight of saving data assets properly because maybe I can actually throw away this double logic.

yes i’ve had this bug too so i understand.

i feel like DataAssets may be the wrong tool here though. DataAssets are effectively global singletons, if you change them anywhere it effects everywhere. ie if you save it in one game, start a new game it will have the old save values. (unless you overwrite it again) assuming all you have is basically and ID(Asset) and bool you’d want to use a struct. Change the ID to a GameplayTag or FName. again you could use a map which will give faster lookups. and the struct will allow you to add other data, ie maybe you want some UI Text associated with the Quest.

1 Like

Thank you very much for your Info! I know about that and if a new playthrough is being started, all the values will be reset, including the Data Assets. Data Asset is pretty handy for my use case to be honest meaning I probably won’t be switching at this point. I am way too deep into it already and it’s very comfortable using it the way designed.

1 Like