Attaching Custom Data to a Dynamically Created Object

Godot Version

4.6

Question

Hello! This is my first question here and really happy this forum exists, anyways to the point:

I am demo-ing a type of city builder and I spawn buildings dynamically, they MeshInstance3D packed into a StaticBody3D. My question being, I create some of these meshes procedurally and I would like to embed custom data into them, specifically a custom resource I built that contains vertex arrays and other important data that should be saved but I don’t really want to save them on one Node, rather my idea was to embed this data into the MeshInstances I create and when I find them through raycast to fetch that data and edit it afterwards (this is all a part of me changing these meshes procedurally in certain ways).

So the question being, how do you think should something like this be achieved, I did see something about attaching scripts to these objects but I am not 100% sure if that is the proper way of doing this.

Thanks in advance!

You can use MetaData. It can be added to any node at the bottom of the Inspector or through code.

2 Likes

Make a class extending Resource, then use metadata to create and store your resource on that node.

2 Likes

Thanks so much for the insight! :grin:

1 Like

Thanks so much for explaining this :smile: