Can't access RigidBody2D's children from getter/setter

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Twk Amz
:warning: Old Version Published before Godot 3 was released.

Hi, I’m trying to make a RigidBody2D which I can resize from the editor. I exported a variable called size, and defined a getter and a setter for it. When I change the size variable(as rigidbodies cannot be scaled), I want to scale all the children of my rigidbody. The thing is, inside the setter I cannot access the children of the node.
This is what happens:


extends RigidBody2D

export var SIZE = Vector2(64, 64) setget setSize, getSize; # my exported var
func _ready():
    pass
func setSize(newSize):
    print(get_child_count()) # outputs 0
    #[Lots of crazy resizing here]

func getSize():
    return SIZE;

If i try to access the children nodes from another method, it’s ok, but in the setter it’s impossible. Any ideas why?

Thanks!

EDIT: Ok, I found that this is happening because the setter is being executed before the children nodes are loaded. If I use live editing and change the scene after running the games, it works(because all nodes are loaded). How can I “wait” for the children to be loaded before executing the setter? Or is there a better way to do it?

:bust_in_silhouette: Reply From: puppetmaster-

Do you have tool on top off your script?

I have had also problem with function called to fast. (before child nodes are loaded)

tool

extends Node2D

export var map_number = 5 setget set_map

func set_map(new_value):
	map_number = new_value
	if get_node("level") != null:
		var mapscene = load("res://scenes/maps/map"+str(map_number)+".scn")
		if mapscene != null:
			if(get_node("level/map")):
				get_node("level/map").free()
		var map = mapscene.instance()
		get_node("level").add_child(map)

So I check first if child node exist and it works!

Better to use has_node(...) instead of get_node(...) != null :smiley:

Bojidar Marinov | 2016-03-31 15:25

I added tool to the top of the script but it keeps happening, it doesn’t detect the children of the node. Why is this?

Twk Amz | 2016-03-31 16:54