Change mutilple materials for multiple static mesh on Key press

Hi all guys,
I have some pretty simple stuff to do, but I’m stuck with it.Let’s explain:
I have five static meshes and ten materials, now, I need to assign five of this ten materials to five meshes on a keypress, and the other five materials to the same meshes if I press another key.

Any idea how to handle that?
Thank you all, guys :wink:

Hi, first of you would need a reference to each of your static meshes. Then when the key is pressed you can use “Set Material” to switch the material.

Thank you. Where can I reference each static meshes? In Level Blueprint?

Depends on what you’re trying to do =)

You can reference them in the level blueprint, or you can reference them from any actor you put into the level or you can use “GetAllActorsOfClass” or “GetAllActorsOfClasswithTag” during runtime.

So yeah, you can do it in the level blueprint.

Ok, I did some tests and now seems working. This is what I did in the level blueprint. Do you think is correct? Any suggestions? Thank you

PS. It works, but I think it’s not the best way to do that :frowning:

I think I would need more information on what you’re trying to do =)

So you’ve got 5 (different?, same?) static meshes placed in your level and you’ve got 10 different materials. Then when a specific key gets pressed you want to change the material of each of the 5 static meshes to one of the 10 materials (a random one of those 10?, or always a specific one?, or do you choose the new material based on the static mesh?, or…)

Then if you press another key you want again 5 of those 10 materials to be applied to the static meshes. Does this need to be a different one than before?

And do you choose from all 10 materials or do you have two sets of 5 materials and on one keypress choose only from one set of 5 materials and on the other keypress from the other 5? If so do you always have the same 5 materials per set, or do you have some randomness in there?

Ok, sorry @chrudimer for being too superficial and imprecise in the explanation. My bad. I will try to do a better explanation now (sry for the bad English :o)

So, yes, I have 5 different static meshes in my level and I’ve got 10 different materials (2 sets of 5 materials each) I need to apply them to the same 5 static meshes (so, in other words, every static meshes could have 2 materials, one of them if I press the Key “O” and the other one when I press the Key “V”).

Actually, this is my setup: when I press one of these 2 keys, a sequencer in an Actor Blueprint is played, then a string variable is stored and finally, the same 5 static meshes we are talking about are animated with simple translations and rotations.
Now in the level blueprint, I check the value of the string and If it is equal to “OA_Materials” I set the first group of five materials, while, if it is equal to “V_Materials” I set the second group of 5 materials in the same way.

Since the flow in the level blueprint is linked to an Event Tick, the last stuff I did, was check if the string value has changed or not, in order to block the flow of material assignation.

Nope, nothing is random, and nothing changes. Eg: “mesh1” can always have only “A” or “B” materials, “mesh2” can always have only “C” or “D” materials, “mesh3” can always have only “E” or “F” materials, and so on…

Ok, then two options I can think of

(1) Inside the actor where you have the static mesh, create a new variable for each of the two materials and mark it “instance editable”. Then create two events/functions in this actor, one that you would call if you press the Key “O” and another one when you press the Key “V”. So inside the first event/function you would apply the first material to the static mesh, in the second event/function the second material.
Then since you marked the two variables “instance editable” you can set them for each actor that you’ve put into the level separately. Then when you press the key “O” or “V” you would get all actors of this class and call the appropriate event/function.

And if you have directly dragged the static mesh assets into the level, then they will be “static mesh actors”, and you could create a new actor that inherits from “static mesh actor” and add the above functionality to it.

(2) Create a map from static mesh to material. Then when you press the key, iterate over the actors with the static mesh and apply the material from the map. So you could create one map for the materials when you press key “O” and another for key “V”.
[HR][/HR]
So I would try to build it in a way, where I would be able to loop through all actors, instead of duplicating the logic per static mesh/actor.