How to modify material for a scene/object instance?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By chuckeles

In my game, I have a car scene (object) which then gets instantiated during gameplay many times. Each car has a separate state for its breaks, and when the car is breaking, I want its tail lights to change their emission strength.

How should this be done properly in Godot?

The way I’m currently doing this in the game is that, in a script for the tail lights, I call material = material.duplicate() to make a unique material for the car. Then in the process function, I change the material’s emission as necessary using material.emission_strength. This feels like it isn’t the proper way to go, and I expect it to not be performant to duplicate the material each time a new car is spawned. How can this be improved?

Have you tried assigning a unique Material to the original object, and then tried changing the emission_strength on the instantiated object via a Material override?

Ertain | 2022-04-04 23:38

How do you do that? I don’t see any material override option in the editor.

chuckeles | 2022-04-10 17:55

Okay, actually now I see there’s a material_override property which I can set. But it looks like it needs another material, which means that I still need to duplicate the existing car material anyway.

Actually, now that I’m thinking about it, I can have 2 premade materials - one for regular tail lights and one for breaking tail lights. Then I can set the material_override to the breaking material while breaking, and to null when not…

chuckeles | 2022-04-10 18:01

Yep that worked!

chuckeles | 2022-04-10 19:02

:bust_in_silhouette: Reply From: Inces

Yeah, there is an editor function for that.
Get into your car scene, → material → and choose “resource” property tab there → set “local_to_the_scene” to true. The same can be done in code by “make_local” method.

I don’t see any make_local function in code, nor any “local to the scene” option in the editor. I’m using Godot 3.4: Search — Godot Engine (stable) documentation in English

chuckeles | 2022-04-10 17:51

Resource — Godot Engine (stable) documentation in English
It is almost 1st sentence there.
or it was local to the scene in code and make local in editor :). In editor every resource has
Resource tab, it is a bit hard to see. If You click on it , it will show additional options, like resource path and make local.

Inces | 2022-04-10 18:06

Oh I see! I was looking in the wrong place – I was searching the properties of the mesh node, whereas this is a property of the resource. This will suit my needs exactly, thanks!

chuckeles | 2022-04-12 07:22