Godot Version
v4.2.2.stable.official [15073afe3]
Question
How to get resources created in editor by a tool to carry over into runtime.
Research
I have created a packaged scene (.tscn) where I intentionally left a resource (MeshInstance3D.mesh) empty.
The reason for this is because I plan to create a new mesh on “_ready” in the editor.
This is the code for that creation:
Note: The root node has this script and the child node is a MeshInstance3D
@tool
extends Node
func _ready():
if Engine.is_editor_hint():
if ($MeshInstance3D.mesh == null):
$MeshInstance3D.mesh = BoxMesh.new()
Reproduction
I drag the packaged scene into my currently loaded scene I wish to play, and see that the mesh is created.
I then click play scene, and notice that the mesh is missing.
After closing the game and reopening the scene, I see that the box mesh is still there.
Further questions
My main goal here is to automatically create unique resources when I add another packaged scene to the main scene. The way I am going about that is to create a new shape when the packaged scene is created.
If there is an easier way to do this, I would love to hear it, but regardless, I’d still like to know the answer to my main question.
So you have a root node in the scene you have open in the editor. Then, you drag your packaged scene onto the root node and see that the boxmesh is created. You expect that when you run the game, the boxmesh is still present, but it isn’t. I am confused by what you mean by
the collisionshape is still there
Do you mean the boxmesh? According to Running code in the editor — Godot Engine (stable) documentation in English you need to set the node owner if you want changes to persist to disk. Reading the other information on that page may help you also.
My apologies. I originally wrote this using a collision shape, but change it to use meshes for clarity. You are right it was supposed to be “box mesh” there. It looks like I forgot to change that one line.
I read up on the documentation you sent, and tested some stuff out, but I still am not able to get it to work. I am only creating a resource not a node, so there is nothing to add an owner to.
Here is some more info for clarity:
Here are all my screen shots. (New users can only upload one image, sorry for the inconvenience)
#1
This is the packaged scene, which I’ll call prefab.
In it you can see the two nodes, and how the child MeshInstance3D has no mesh.
#2
Here I drag the prefab to the center of the testScene.
#3
The prefab is created, and the tool is run creating the mesh box resource for the child MeshInstance3D.
#4
Yet when the game is run, the box mesh is missing.
#5
When checking “Editable Children” for the prefab, you can see the box mesh is still there.
#6
However, if I make the prefab local, then the box shows up in game. I’d rather not have to do this each time I want a new prefab, but if it comes to it I will.