How to Build a Component with Assets That's Reusable Across Projects?

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

I’d like to create a reusable component that has images and scripts. For example, a dice component that would have six PNG images, one for each die face. There would be a script with a handful of functions, nothing huge. I could create a project for this component, build the functionality, then copy over the scene and assets to a new project.

The problem is that the TSCN file points to the images relative to the project root, which breaks when the component is being used in a different project structure. For example, the TSCN file for the component might point to res://images/die1.png, which might not resolve correctly when used in another project. I could fix the broken references each time, but I’d prefer not to.

Is there a best practice for building components that contain assets like images, scripts, etc?

:bust_in_silhouette: Reply From: JG

For each component, I create a unique folder and put everything in there, the scene file and whatever images you need.

Once I am finished creating the component, I remove all paths from the TSCN file. This can be done quickly with a search and replace in your favorite editor.

For example, path=“res://components/dice/1.png” becomes path=“1.png”

search for “res://components/dice/” and replace it with nothing.

When you move the component to a new project, drag and drop the folder and Godot should automatically update the paths for you.

Great - thank you!

curtking | 2023-03-01 18:12