How to store hit actor info in an array to then save that info in a save game?

I’m currently trying to keep a bunch of trees from respawning when reloading the game.

I’m starting to familiarize myself with save systems, but I can’t seem to understand how I’d save info on what tree has been cut, to then decide that on begin play/on save loaded, these trees will no longer be spawned.

So far what I understand is that I can get the hit info on the actor by creating a variable named “Hit actor Name Actor” from the “hit actor” pin in hit result.

Step 1

Then I add that information to an array variable called “Hit Actor Name Array” (this a array has not been set anywhere):

Step 2

And thats where I’m unsure of what to do next. To save it in my save file, I added an array variable named “Destroyed Tree Actor 2” to my save game and I try to store the “Hit Actor Name Array” in that “Destroyed Tree Actor 2” variable:

and make a load custom event, but I’m not quite sure how to make this one:

step 4

I also have no idea where I would call the custom event “Add Info to Array” in the second photo.

All I’m trying to do is get the information from what tree was hit → store that information in an array → make it so the trees from the array will be destroyed on loading the game again.

Any help is appreciated! The best help would be if anyone could tell me what I have done wrong and where to put these nodes and how to complete the “Load Tree Info” custom event. Pictures explaining it too will be very appreciated!

I can add more information here if needed.

Thank you in advance.

If you write a function like this

Then you can destroy actors and put the references in the save game

and later, re-destroy them like this

Thank you so much for the help! That solved the issue for me, but it added a bunch of errors at the end of runtime so I won’t put this as solved just yet.

The error that popped up is referenced back to the tree blueprint I put this code in and the “destroy actor node” that is connected to the “for each loop” node.

The message is:

I tried to put “collect garbage” behind the destroy actor nodes, but it didnt remove the “pending kill error”.

EDIT: Would also like to add that I have around 700+ trees. I only cut down 2 to test that this worked.

To be clear, you only need to call ‘save actor’ if you intend to destroy it. Also, if it takes many hits to break a tree, only the first hit is relevant for the save. I think that’s what it might be.

So instead of saving it when its hit, I would call it when for example saving the game through a save menu? Sorry if I misunderstand! Just want to make sure I get it right :slight_smile:

The system works as so that it spawns in foliage actors → on overlap with character it destroys the blueprint in the image and spawns a new one in that same place, and the new actor can be interacted with. This actor though doesnt spawn when reloading the game, just to add some more info on it.

I thought you were trying to remember which trees had been destroyed. So when you run again, they are still destroyed?

When you destroy a tree, call ‘save actor’.

Next time you run the level, you can load the save game and find out which actors had been marked as destroyed. In your case, I guess you then replace them with a broken tree?

So, don’t necessarily always use the destroy node.

Yes I do need to remember what tree has been destroyed so I don’t respawn those trees after I load up the game again :slight_smile:

However I do also need to use “destroy actor” to get rid of the actor so it doesn’t block for the new actor that spawns. So yeah I do replace the first actor by spawning a new one on its location. So not using the destroy node seems kind of impossible in this setting :open_mouth:

EDIT: Everything seems to work fine, its just that the error message pops up and I’m worried about packing a game with that constantly there

You’re basically destroying the actors many times after the first time. That’s why you’re getting that error.

You only need to destroy twice. Once when you replace with the choppable tree, and when you get rid of them on the level re-load.

If you don’t even re-spawn them on reload, then you don’t need to destroy there…

I went over everything again and now I feel like an idiot :'D I figured it out. Thank you so much for your patience! I’m still quite new to making games so I really appreciate the help!

The issue was that I built the code for destroying the trees, in the actual tree actor itself when I should have made this in the level blueprint instead. I now have no errors and everything works perfectly! :slight_smile:

1 Like

Do try packaging it, I’m not sure if this ‘actor reference’ method works in a packaged build.

1 Like

I will try that right away!

I packaged it and no error came up. It also works in a package version!

Again, thank you a million times! :smiley: Been driving my head in for the last few weeks!

1 Like

I have a follow up question regarding this that just came up:

I got everything to work like I mentioned, but I’ve been messing about today on how to combine everything to one save file. In this instance I gave it its own save file name compared to what I normally use to make sure that I got it working.

The problem seems to be that since I’m saving every time that I interact with a tree, it overwrites the other saves, so if I reload the save, it now only saved the trees and nothing else that should be saved.

I’m not sure how to reference the saved array either, over to the gamemode where I have stored all my “save game” and “load game” events, so that I can save the array with trees manually together with every other save I have, by using a menu.

The ideal thing would be to not save the variable directly by using “save to slot”, but instead save it together with everything else in the gamemode blueprint

This is my save game 101

Notice how, every time you want to save something, you have to read the save game first.

This is so that you write it back out with the stuff it already had in it :slight_smile:

Regarding your other question: yes, a centralized approach to saving is a good idea. Do the save game stuff in a central place like the game instance, or game mode, then you don’t have to worry about organizing it anywhere else.

The game mode ( or instance ), can have custom events ready, one to give you the SG variable, one to save it etc. Also, if you do that with interfaces, you don’t even have to cast :slight_smile:

That cleared things up more! Thank you :smiley: Adding a local variable in the gamemode and adding the hit actors to that array was the way to go!

Also turned out I had yet another thing overwriting this save, so it didn’t work even with the local variable at first. I removed all of the other saving events to test only this and then it worked.

Thank you again a million times for your patience and your help! :slight_smile:

1 Like