Skip to content

Instantly share code, notes, and snippets.

@toger5
Last active October 22, 2018 12:13
Show Gist options
  • Save toger5/dfd7100414d627869c847435d1b77e30 to your computer and use it in GitHub Desktop.
Save toger5/dfd7100414d627869c847435d1b77e30 to your computer and use it in GitHub Desktop.
Godot container. Examin updating.

Godot Container Events (sort)

There is an event called '"on_sort_children"' for each container object. This event is supposed to be used to get notified, when resorting is needed (in the sourcecode of godot this is used whenever the container itself is resized for example -> than the children need to get repositioned as well)

The event system in godot works by first calling the builtin functionality though. This means when the children need to be resorted, (new children added and the container needs to draw. NOT IMMEDIATLY AFTER ADDING EACH CHILD. FOR BETTER PERFORMANCE. BECAUSE 10 CHILDS CAN BE ADDED AND IT SORTS ONLY ONCE) the builtin funciton gets called first. This function actually transforms the children to the intended locations and sizes. After that the gd script signal connected functions get called. Right after the position actually got updated! Exacly where you need it :)

I added a script where i tested it. have a look, and check the output.

extends GridContainer
var process_counter = 0
func _ready():
connect("resized", self, "on_resize") # never gets called so this event is useless regardin the issue...
connect("sort_children", self, "on_sort_children")
for i in range(7):
var b = Button.new()
add_child(b)
print("_ready/for_loop b: " + str(b.rect_position))
print_button_positions("_ready/for_loop")
print_button_positions("_ready")
func _process(delta):
print("process " + str(process_counter))
process_counter += 1
func on_resize():
print_button_positions("on_resize")
func on_sort_children():
print_button_positions("on_sort_children")
func _input(event):
if event is InputEventMouseButton:
print_button_positions("_input")
func print_button_sizes(from_function):
print("-----Button sizes in Function: " + from_function + " -----")
for b in get_children():
print(b.rect_size)
func print_button_positions(from_function):
print("-----Button positions in Function: " + from_function + " -----")
for b in get_children():
print(b.rect_position)
** Debug Process Started **
OpenGL ES 3.0 Renderer: AMD PITCAIRN (DRM 2.50.0 / 4.15.0-36-generic, LLVM 6.0.0)
_ready/for_loop b: (0, 0)
-----Button positions in Function: _ready/for_loop -----
(0, 0)
_ready/for_loop b: (0, 0)
-----Button positions in Function: _ready/for_loop -----
(0, 0)
(0, 0)
_ready/for_loop b: (0, 0)
-----Button positions in Function: _ready/for_loop -----
(0, 0)
(0, 0)
(0, 0)
_ready/for_loop b: (0, 0)
-----Button positions in Function: _ready/for_loop -----
(0, 0)
(0, 0)
(0, 0)
(0, 0)
_ready/for_loop b: (0, 0)
-----Button positions in Function: _ready/for_loop -----
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
_ready/for_loop b: (0, 0)
-----Button positions in Function: _ready/for_loop -----
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
_ready/for_loop b: (0, 0)
-----Button positions in Function: _ready/for_loop -----
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
-----Button positions in Function: _ready -----
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
-----Button positions in Function: on_sort_children -----
(0, 0)
(16, 0)
(32, 0)
(0, 24)
(16, 24)
(32, 24)
(0, 48)
-----Button positions in Function: on_sort_children -----
(0, 0)
(16, 0)
(32, 0)
(0, 24)
(16, 24)
(32, 24)
(0, 48)
-----Button positions in Function: on_sort_children -----
(0, 0)
(16, 0)
(32, 0)
(0, 24)
(16, 24)
(32, 24)
(0, 48)
process 0
process 1
process 2
process 3
process 4
process 5
process 6
process 7
process 8
process 9
process 10
process 11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment