How do I save an object after changing material in play mode?

I setted up a menu where you can select a material for an object using a seperate level. When I change it, then I switch into a new level with the exact same object, it goes back into the default material. Im not sure how to save the material so I can see the new applied material on another level.

Great! - please tick the answer if you’re happy with it, as it will help others :smiley:

You need to use a save game.

It’s a bit of a pain, just to get this effect, but this is how it works.

A save game is just an empty blueprint. You can put anything in there you want. Most people just put variables in.

In your case, you would have an ID so you know which object you’re talking about ( this is just an integer that you assign ), and a material reference.

When the player changes the material, you write to the save game which object was changed and what the new material was.

When you load the new level, if the same object is in the level, you change the material on begin play.

Here’s a vid about save games:

EDIT: I'm assuming your object is a blueprint. It can remember what material it had last time you played, by storing it in the save game. When the game starts again, or a new level, first thing the object BP does it read what material is had last time the game was running and apply it. Like that... :)

I havent learned about save game yet, thanks so much for the solution, and its just what I needed!

Sorry, but again I’m having trouble… the following screenshots is what I got. But I think what is wrong here is that I don’t have integers applied to the material changes, not sure how to do that ;(

It’s because you’re creating the savegame every time. Which means it will always be empty, because it’s a new save game.

You only ever use the create node ONCE :slight_smile: To create a new save game if there’s none there.

From then on, you read it, modify it, and then write it back.

No no, sorry… :wink:

Begin play:

When you want to read:

When you want to write:

So, if you have a create node in the middle of your code, it has to be wrong…

I think i did it right.

So I believe now the saving is working, however, I just need to load the material that was changed. I already set up a loading thing, but not sure what to do now.

Oh, thanks. But before I try that out, I just wanna make sure that what I’ve done on my save object is correct and I didnt miss something even though its hardly anything in it. So if that is looking good, then when I write the save game, how can it save the material?

Oh, I actually use only one mesh and 3 materials to change it to.

The video you sent that i watched, he created 2 variables in total, one in the save object, and one in the UMG/Widget BP, im also using the UMG, so I guess I need both? If so, which one was the array integer?

Yes, that’s fine. So you have an int which represents which material is applied to something in your game.

When you load that int from the save game, you can then look it up in your array of materials and apply the correct material :slight_smile:

EDIT: But wait… If you have, like, 3 meshes you’ll be putting materials on, you need 3 places to save.

You can either have 3 variables, like mat1, mat2 and mat3.

Or you can have an array called something like MyMaterials. It’s just an array of ints, and you put the ints representing the material into that array in the save game.

Do you get it?..

Ok, then one int is good.

No. You only need to make other variables in the UMG if you actually need that.

My game has A LOT in the save game. Nothing copied into the UMG.

If you can already make materials change, you can write to the save game when you do that.

If you’re changing the materials from the UMG, then it might be logical to do it there, but it really doesn’t matter.

If you get stuck, come back here, and I’ll show you how…

Alright, thanks!

Tried again a couple of times, couldn’t work it out. The following screenshots are all I have as ill try to start over starting with these nodes. So I have no variables and nothing related to the saving stuff. What do I do? All I have is my save object setup, and my materials changed in-game. So hopefully you can see what I got, and maybe this time it will be easier to work it out.

So if you could please tell me what to do from here, that would be awesome! So after I finish this, I could study it myself :slight_smile:

Alright, looks good but 2 things. 1, Whats in the “Set Color” function, and 2. Where the “My Base BP” variable come from?

SetColor is a custom event, you make it yourself in the BaseBP.

To get the MyBaseBP variable, right click on the pin on the right of GetActorOfClass and choose ‘promote to variable’

In the widget:

In the BaseBP, you need an array of materials:

and this code:

and in the level BP:

Thank you sooo much, it’s perfectly working! I strongly appreciate your time and effort to help me with this thing that I couldn’t find anywhere!!!