0 votes

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?

Godot version 3.2.3
in Engine by (25 points)

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)?

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

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()
     _hitboxes.add_child(area)
     area.global_position = map_to_world(cell) + self.global_position
     area.global_position += position

This is called inside "for cell in getusedcells()". The variable hitboxes is the container node I was talking about in my question. The scene "spikessurface_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.

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.

1 Answer

+1 vote

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.

by (2,482 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.