Why aren't changes made by a tool being applied when I run the game?

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

Hello! So I made a tool to add Area2Ds to certain objects whenever I check a box. The Area2Ds are added in a container node labelled “Hitboxes”. When I run the script in editor I can see the blue collision shapes applied right where I need them but when I run the game, they’re gone (no collision, no shape when I have the debug show shapes on). Additionally, when in editor, there are no children under “Hitboxes” but when I toggle it’s visibility, all the added Area2Ds go away, so they’re definitely being added as children of “Hitboxes”.

My quick fix is just to run the script in the _ready() function as well so they’re always applied at runtime, but this adds a few seconds onto loading and kind of defeats the purpose of it being a tool. Does anyone know why the changes my tool is making are applied in editor but gone at runtime?

Hey, can you show us the complete script? Because some keywords don’t work very well with tools and the issue might be coming from there and btw are you using godot’s built in editor or an external one for your scripts(because external editors normally have problems with tool mode)?

MikeSundaysGameDev | 2021-04-10 18:09

How about saving the scene before running the game to confirm that the changes are saved?

backendcoder | 2021-04-14 09:08

I’m using Godot’s built in editor for all my development. Here’s the relevant part of my code!

   func add_area_at_relative_position(position: Vector2, cell):
     var area = load("Assets/resources/spikes_surface_area2d.tscn").instance()
	 area.global_position = map_to_world(cell) + self.global_position
	 area.global_position += position

This is called inside “for cell in get_used_cells()”. The variable _hitboxes is the container node I was talking about in my question. The scene “spikes_surface_area2d” is just a small area2D with a collision shape. This code does exactly as intended in editor but only works in game if I manually run the tool in the ready function.

arbo | 2021-04-14 17:12

Saving shouldn’t be the issue. I believe Godot autosaves when you run the scene (but do correct me if I’m wrong) and if it doesn’t, I save compulsively after any change.

arbo | 2021-04-14 17:14

:bust_in_silhouette: Reply From: Lopy

add_child does not modify the owner field of the Node, which is used when saving Nodes.

Try adding a area.owner = area.get_parent().owner after calling add_child.

If it still does not work, try manually saving the scene in your Tool.