Map variable not saving correctly?

I’m trying to set up a system where I save the salient details of every NPC in a streaming level before it unloads, so that the next time that I open the level, all of the NPCs are where they were, have the same HP, etc.

I have a variable set up in the Game Instance that is basically a map with a Name (the level) keyed to a struct. That struct contains only one variable, another map with another Name (the NPC ID) keyed to another struct. That struct, in turn, contains all of the details I want to save about the NPC.

Inside of the Door class, I have some logic that is fired when you interact with the door.

What I’m trying to do is collect all that data and save it to the lower-level map. As far as I can tell, this should work.

In order to test it, I am going through a door that has an NPC next to it, then returning through the door. If it worked, there would be two copies of the NPC: the one that is placed there in the editor and the one that is spawned upon loading the level from the variable in the Game Instance. There is only one, however.

I had this set up differently before where I was using the Make Struct node, but when I was doing it that way, I ended up with an additional copy of the NPC every time I came back through the door.

Any ideas?

image
This is returning a copy of the element in the Map, so when you modify it, it doesn’t modify the one in the Map.

To fix that, you need to update the element in the Map and not just a copy. For arrays there is a “FIND by ref” node which helps, but it doesn’t exist for maps apparently. So you have to modify the copy of the element, then insert it back into the main map, like so :

1 Like

Shouldn’t the Add node and the Set node be reversed ?

No, if you don’t store the result of FIND in a temp var immediately, it’s gonna FIND multiple times and return a different copy each time, so you would modify the first copy and then ADD or SET a second copy (unmodified), which wouldn’t do anything.

This should also work and allows you to expose only the data you want to modify:

(post deleted by author)

(post deleted by author)

It works!

Now I just need to figure out why my widget disappears when the first level unloads.

Edit: actually, wait, it’s making more than one copy… I thought that when you Add to a map, if the key is already in the map, that it will overwrite the existing value for that key. This seems to be adding a separate instance of the same key.