How to make All Instnace Scenes Unique

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By vonflyhighace2
:warning: Old Version Published before Godot 3 was released.

I have objects that I need to spawn but some are duplicates that have to be unique. Is this Possible to do with packed scenes?

That’s a vague question, could you clarify what specifically you’re trying to do.

Anything instanced should be unique. Their properties can be identical, but they are allocated into different pieces of memory and possess their own state.

avencherus | 2016-10-26 03:04

Sorry for the late reply. What I meant was that instance scenes share resources. Even though the are allocated in memory differently if the engine sees that you are accessing the same resource it defaults to referencing the resource rather than copy it. so all instances of the same scene even though unique as a node, share properties. Change material on one node changes it on all nodes of the same instanced scene basically. I Want to default to coping a node upon instance instead.

vonflyhighace2 | 2016-10-27 19:13

:bust_in_silhouette: Reply From: avencherus

I see, yeah that’s a good question. There are two ways to do that.

If you’re doing it through the editor, when you right click on the property where the resource is assigned, there is an option called “Make Unique”.

If you would prefer to do this in script, resources have a method called .duplicate(), which acts just like .instance() for scenes, and .new() for objects. But be mindful some script allocated and instanced resources have to be unloaded on exit, using .free()

So watch out for that.

A example of code might look like this for a FixedMaterial as a resource.

var my_resource = preload("res://my_shader.tres").duplicate()

# Some change...
my_resource.set_parameter(FixedMaterial.PARAM_DIFFUSE, Color(0,1,1,1))

my_mesh.set_material_override(my_resource)

Is possible to duplicate a Resource with duplicate(true), that way you can duplicate PackedScene resource and sub-resources.

What I don’t know is how deep the sub resource duplication goes.

eons | 2016-10-28 01:19