+1 vote

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?

in Engine by (271 points)

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.

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.

1 Answer

+2 votes

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)
by (5,245 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.