How to save a Material at runtime?

Hey guys and girls.

Is it possible to save a Material that is assigned to a mesh in game?

For better understanding:

I have a cube in Level1 that will be generated with a blueprint. So basically the cube is e.g. “red” and now in my MainMenu are some displayd Materials (blue, green,…) to chose from, select it and it should apply this Material to the cube. The problem is if I jump in my Level1 the cube hasn’t changed.

I tried out “DMI” and “setMaterial” and think “setMaterial” will work if i find a way to save the new Material that was assigned to the cube.

I would really appreciate any help or suggestions.

Hi Priimee,

Would you be able to post a screen shoot or two of your current setup? SetMaterial should work fine but its hard to trouble shoot without seeing your current setup.

Thanks for your reply at first.

Maybe these Screenshots can help you to understand me.

I also have a SaveGame but don’t know how to save the new assigned Material in there.

Hope you guys can help me and others with that.

Yes this helps a lot!

Is Button_104 suppose to change the material to a specific color? Are there going to be multiple buttons that can change the materials? For example a button to change it to blue, a button to change it to red, and a button to change it green like in your first post.

(Also, unrelated but your tunnel material looks pretty cool)

Hi Aryln , Basically if i need to set a Material to a Static Mesh i will be using a create Dynamic Material Instance for the Static mesh and set the Material there .

if The DynamicInstance is not working for you from Desgin Blueprint . Which should work basically .

Try This : In the construction Script of you BP_Box Chaos : create the DynamicInstance of the material and promote the material to a variable called MeshMat and set its properties as InstanceEditable and Expose on Spawn .
Fetch the Selected Material From UI : Spawn BP_BoxChaos using SpawnActorFromClass . you will see the the material variable MeshMat as input in SpawnActorFromClass Node , which you can provide the Selected Material From UI .

Thanks for your support guys, appreciate it :slight_smile:

Arlyn: Yes there will be at first about 10 buttons that all change the Tunnel Material to a different Material, so if i create a new Material i can easy add the button for it.

OoKushoO: I tried out the DMI, but i want to change the complete Material not some instances of it.

Also the problem could be that i switch levels ingame and also have a save game file.

The Expose on Spawn could probably work if it also saves if you quit, but how can i set the DMI in my MainMenu?

For the separate material route, you can make an array of material references in your MainMenu BP. When you press a button, set an integer to the index of the material you want and ‘get a copy’ from the array. That would get a copy of the material you could use on the tunnel mesh. You can also save the index integer in your save game and use that to set the material when you load it.

I’m not at a computer right now so if this doesn’t make sense I can try to make a mock up of it in UE4.

Thanks mate that is finally at this game part a step forward, but i probably need some more detailed explanation, until then i will try my best to get it to work, so i can give u more details.

Here we go, this should help. Happy to explain more if something isn’t clear :slight_smile:

Thanks a lot man,

but the print string node won’t get triggered and also the material won’t change.

I think the problem might be with the new Level that spaws wenn you press play ingame, or the SaveGame file.

Or do i missed something else?

Really appreciate your help man :slight_smile:

Ok i placed my Blueprint Tunnel in my MainMenu Level and the Buttons both print 0 and the tunnel Material also won’t change.

Also the BlueprintTunnel can’t be placed in my MainMenu Level, that causes a lot of errors.

Did you tried it with different Levels and a SaveGame?

It would mean so much to me if someone could help me with this.

Try using the same setup on an Actor BP with a static mesh component and see if you can get the basic function working to change materials on it. Once that works, then we can tackle getting the tunnel BP, different levels, and save game to work.

Best to tackle things in small chunks rather than all at once.

Yeah good guess, i will try it out tomorrow, thanks man.

Ok I made a new ActorBP with a static mesh component, placed it one time in my Level1 and one time in my MainMenuLevel, i change the Material from my MainMenu, it applies to the ActorBP in

my MainMenu, but not in my Level1, also if i quit it resets to the original Material.

Good, so now we know the change material function works! Lets tackle applying the material when levels change.

The current problem is that when you switch between the main menu level and level 1, the **default **state of the level is loaded for the actors. The widget in the main menu level won’t be able to directly change the material on an actor in level 1.

We need to store the material selection (either a material reference or the index that we selected) in an actor that will be persistent between the two levels. I think the best place to do this would be in a Game Instance Blueprint.

Here is a Answer Hub thread that is similar to what we’re trying to do: https://answers.unrealengine.com/questions/548875/pass-variable-between-main-menu-and-levels.html

Once we get the selection variable stored in the game instance, we can use the Event Begin Play in the ActorBP (which will be triggered when Level 1 is loaded) and get the saved material selection in the game instance and change mesh material to it.

Thanks mate all that helped me a lot!

Ok finally i got it to work.

Is it also possible to save the changed Material? Like if you close the game it loads the latest Material?

I tried it with my SaveGame, but it didn’t work.

Great, now we’ll get the save game part working.

Can you post some screenshots of your current SaveGame setup? We’ll want to make sure this works in your current framework.

Hope you can help me with this :slight_smile:

I also made a new Level(Design) and when you select your Material from there it applies to my MainMenu and my GameLevels, but it shouldn’t affect the SaveGame setup which I made in my CharacterBP.

I really appreciate your help mate, if you want to play my game you just have to tell me how i can send it to you :slight_smile:

Our goal here is that we need to save the material selection when we save the game and set the material when the game loads. I’m not sure doing a save/load from the character is the best place from an organizational/hierarchy standpoint but we should be able to make it work in a quick and dirty way. If you’re not adding too much more to the game then it shouldn’t matter.

First, lets save the material selection. You’ll need to add a reference variable to the material that matches the one used in the Game Instance to the Save Game Class. Then, when Save the Game is called, you can grab the material reference variable from the Game Instance and set it in the Save Game Object.

Now lets load the material. To do this, you’ll want to get a reference variable for the material from the Save Game Object and set the Save Game Instance variable to it. Once that is done, we’ll need to call an event to set the material of the tunnel. I’m not sure if Begin Play will work here so lets trouble shoot that next if it doesn’t work.

Let me know if this is confusing

Also, I would love to play the game! Please do send it to me.