How to make exported variable distinct

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

Hi guys.

I exported a variable called items in one of my scene that should keep track of an array of items in the game.

However, when I instanced the same scene, they all shared the same array. I would expect them to be unique for each instance.

My question is, how to make the say exported variable unique? Thanks in advanced.

:bust_in_silhouette: Reply From: timothybrentwood

In the Scene panel of your editor: click the node that the script for items is attached to. Then go to the Inspector panel and click the little screw driver with the wrench icon near the top and press the Make Sub-Resources Unique option.

This works, but it’s very ugly. Exported arrays are always shared between all instances of the same node in godot. A better way would be to make your items child nodes of your parent node. This way you won’t run into any issues when ever you want to change the code of your scene.

See the docs for more information on exporting arrays. Unfortunately, it’s only mentioned in a comment in the example code. But I think this comment from Reddit summarizes it pretty well.

Exporting arrays works but with an important caveat: While regular
arrays are created local to every class instance, exported arrays are
shared between all instances. This means that editing them in one
instance will cause them to change in all other instances.

MagnusS | 2021-05-05 13:11