Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nodes added as children in tool scripts are not visible in the editor #40385

Closed
Shadowblitz16 opened this issue Jul 14, 2020 · 7 comments
Closed

Comments

@Shadowblitz16
Copy link

Godot version:
3.2.2

OS/device including version:
Windows 10 Pro Education Nvidia geforce gtx 650 gles2

Issue description:
I have this script..

extends Control
tool



onready var _icon     = $Ship/Icon
onready var _def_bars = $Ship/DefBars
onready var _spd_bars = $Ship/SpdBars
onready var _pwr_bars = $Ship/PwrBars

export(Resource) var ship_resource setget _set_ship, _get_ship
export(Color   ) var def_color = Color(0x73eff7ff)
export(Color   ) var spd_color = Color(0xa7f070ff)
export(Color   ) var pwr_color = Color(0xef7d57ff)

func _set_ship(value):
	ship_resource = value
	update_controls(value)
	
func _get_ship():
	return ship_resource; 
	
func update_controls(value):
	#print("test")
	
	#del stuff
	_icon.texture = null	
	for i in _def_bars.get_children():	
		i.queue_free();
		
	for i in _spd_bars.get_children():
		i.queue_free();
		
	for i in _pwr_bars.get_children():
		i.queue_free();
		
	# add stuff
	if (value==null) || !(value is Ship): return
	
	_icon.texture=value.icon
	for i in range(value.def):
		var c = ColorRect.new()
		c.color = def_color
		c.rect_min_size.x = 2
		_def_bars.add_child(c)
		c.set_owner(_def_bars)
		print(_def_bars.get_child_count())
		
	for i in range(value.spd):
		var c = ColorRect.new()
		c.color = spd_color
		_spd_bars.add_child(c);
		c.set_owner(_spd_bars)
		
	for i in range(value.pwr):
		var c = ColorRect.new()
		c.color = pwr_color
		_pwr_bars.add_child(c);
		c.set_owner(_pwr_bars)

attached to this object..
image

but as you can see the ColorRect are not appearing in the hboxcontainer even though they are there and the script is a tool script

Steps to reproduce:

  • just add object to the scene tree in a tool script. they do not show up in the scene tree.

Minimal reproduction project:
Not Needed

@Zireael07
Copy link
Contributor

That is normal. You haven't set the owner variable, so they do not show up in the scene tree.

You should try to set the scene root as owner, using set_owner() and get_tree().get_root().

@Shadowblitz16
Copy link
Author

Shadowblitz16 commented Jul 15, 2020

@Zireael07
while I personally think set_owner should be removed and add_child should take care of that, that's not the issue because I clearly set the owner.
c.set_owner(_pwr_bars)
please reread my code

@volzhs
Copy link
Contributor

volzhs commented Jul 15, 2020

owner should be the currently open scene.

@Shadowblitz16
Copy link
Author

@volzhs ok oh that resolves this issue.
thankyou

@Telokis
Copy link

Telokis commented Jul 3, 2023

There is very little information on how to solve this problem. I know this issue is very old but I still have the same problem today while trying to spawn new nodes from a tool script.
My code is basically doing the following:

@tool
extends Node2D

@export var points : Array[Node2D]

func _process(delta):
	for index in range(points.size()):

		# If the user wants to add a new point, we automatically create it for convenience
		if points[index] == null:
			print("Spawning node")
			var node = Node2D.new()
			points[index] = node
			
			# The following never worked.
			# node.set_owner(get_tree().get_root())
			# node.owner = get_tree().get_root()
			# node.owner = get_owner()
			# node.owner = self
			# node.owner = get_tree().get_edited_scene_root()

			add_child(node)

Sometimes I get the error

scene/main/node.cpp:1645 - Condition "!owner_valid" is true.

And sometimes I don't get any error but the node doesn't show in the scene inspector.

@AThousandShips
Copy link
Member

AThousandShips commented Jul 3, 2023

You can't set owner before adding the node to the parent, owner must be an ancestor, see documentation

The error could be improved to say this though

Edit: PR for error open #79000

@Radivarig
Copy link

Radivarig commented Aug 19, 2024

	skeleton = Skeleton3D.new()
	add_child(skeleton)
	# skeleton.owner = self # doesn't work
        skeleton.owner = self.get_parent() # works
        # NOTE: set owner AFTER add_child

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants