Swapping childs of a hboxcontainer

Godot Version

4.2

Question

func _start_animation_right():
    animation_in_progress = true
    var hbox = $MarginContainer2/HBoxContainer
    var last_child = worlds[worlds.size()-1]
    hbox.move_child(last_child, 0)
    
    selected_level = last_child.name
    print("selected level: ", last_child.name) #Level selection is working

    var last_world = worlds.pop_back()
    worlds.insert(0, last_world)

    animation_in_progress = false

My script right above is modifying the position of control nodes consiting of labels and icons inside an hboxcontainer to swap their positions. However instead of moving correctly they only jiggle (?) a little bit

Illustration:

how world 4 can be behind world 1 if it’s in a hbox

1 Like

I have absolutely no idea, but it’s not even bothering me since he will be hidden.
Here’s whole code if it can help

1 Like


just fyi, this is fresh from download, with the random branch. possibly forgot to push new thing
if i open anyway:
image

1 Like

Just made a new push but i’m not exactly sure why it’s doing this, if it’s not working i’ll try to figure it out

yes, but this is about the main menu:


notice what’s wrong here?

the move child definitely work, but your texturerect wont automatically moved to the right position because it’s a set position from the center of the parent World2 control node

Oh. I get it the labels, they are not positioned on the nodes, or with an offset.

the label of “world 2” also is a set position from inspector dock to -254:

if you dont mind about no animation, then it’s possible to make the hbox with control nodes works as intended

is this what you wanted it to be looked like?

1 Like

Exactly! Thank to you i managed to make it work, i reverted the script to scroll to left and it working fine!

1 Like

i will just drop the scene here in case you wanted to see how it managed to be looking like that, but this will need you to reload the project by replacing it thru notepad or any 3rd party text editor. remember to save/backup your mainmenu scene before replacing this

mainmenu scene
[gd_scene load_steps=8 format=3 uid="uid://cedft3vy5hg68"]

[ext_resource type="Script" path="res://scripts/mainmenu.gd" id="1_qj2ne"]
[ext_resource type="FontFile" uid="uid://dbjl1e6kdxpl6" path="res://assets/fonts/PixelOperator8.ttf" id="2_h05dh"]
[ext_resource type="Texture2D" uid="uid://cyfxuch6qa87f" path="res://icon.svg" id="3_tlb1w"]

[sub_resource type="Theme" id="Theme_45hd1"]

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_2rwsq"]
bg_color = Color(0.4, 0.627451, 0.796078, 1)
border_width_left = 2
border_width_top = 2
border_width_right = 2
border_width_bottom = 2
border_color = Color(0, 0, 0, 1)
border_blend = true
expand_margin_left = 10.0
expand_margin_top = 10.0
expand_margin_right = 10.0
expand_margin_bottom = 10.0
shadow_color = Color(1, 1, 1, 0.6)
shadow_size = 2
anti_aliasing = false

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_8c08p"]
bg_color = Color(0.152941, 0.152941, 0.152941, 1)
expand_margin_left = 5.0
expand_margin_top = 5.0
expand_margin_right = 5.0
expand_margin_bottom = 5.0

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_llkcd"]
bg_color = Color(0.121569, 0.121569, 0.121569, 0.305882)
expand_margin_top = 10.0

[node name="mainmenu" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_qj2ne")

[node name="MarginContainer" type="MarginContainer" parent="."]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -107.0
offset_top = 76.0
offset_right = 107.0
offset_bottom = 179.0
grow_horizontal = 2
grow_vertical = 2

[node name="Panel" type="Panel" parent="MarginContainer"]
layout_mode = 2
theme = SubResource("Theme_45hd1")
theme_override_styles/panel = SubResource("StyleBoxFlat_2rwsq")

[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer/Panel"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -46.0
offset_top = -40.0
offset_right = 46.0
offset_bottom = 40.0
grow_horizontal = 2
grow_vertical = 2

[node name="Play" type="Button" parent="MarginContainer/Panel/VBoxContainer"]
layout_mode = 2
theme_override_fonts/font = ExtResource("2_h05dh")
text = "Play"

[node name="Options" type="Button" parent="MarginContainer/Panel/VBoxContainer"]
layout_mode = 2
theme_override_fonts/font = ExtResource("2_h05dh")
text = "Options"

[node name="Exit" type="Button" parent="MarginContainer/Panel/VBoxContainer"]
layout_mode = 2
theme_override_fonts/font = ExtResource("2_h05dh")
text = "Exit"

[node name="MarginContainer2" type="MarginContainer" parent="."]
layout_mode = 1
anchors_preset = 5
anchor_left = 0.5
anchor_right = 0.5
offset_left = -539.0
offset_top = 30.0
offset_right = -6.0
offset_bottom = 203.0
grow_horizontal = 2
scale = Vector2(2, 2)

[node name="HBoxContainer" type="HBoxContainer" parent="MarginContainer2"]
layout_mode = 2
size_flags_horizontal = 4

[node name="World1" type="Control" parent="MarginContainer2/HBoxContainer"]
y_sort_enabled = true
custom_minimum_size = Vector2(150, 0)
layout_mode = 2

[node name="Label2" type="Label" parent="MarginContainer2/HBoxContainer/World1"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -69.0
offset_top = -86.5
offset_right = 69.0
offset_bottom = -62.5
grow_horizontal = 2
grow_vertical = 2
theme_override_fonts/font = ExtResource("2_h05dh")
theme_override_font_sizes/font_size = 24
theme_override_styles/normal = SubResource("StyleBoxFlat_8c08p")
text = "World 1"

[node name="Label1" type="TextureRect" parent="MarginContainer2/HBoxContainer/World1"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -32.0
offset_top = -32.0
offset_right = 32.0
offset_bottom = 32.0
grow_horizontal = 2
grow_vertical = 2
texture = ExtResource("3_tlb1w")
expand_mode = 1
stretch_mode = 5

[node name="World4" type="Control" parent="MarginContainer2/HBoxContainer"]
custom_minimum_size = Vector2(150, 0)
layout_mode = 2

[node name="Level4" type="Label" parent="MarginContainer2/HBoxContainer/World4"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -69.0
offset_top = -86.5
offset_right = 69.0
offset_bottom = -62.5
grow_horizontal = 2
grow_vertical = 2
theme_override_fonts/font = ExtResource("2_h05dh")
theme_override_font_sizes/font_size = 24
text = "World 4"

[node name="Level5" type="TextureRect" parent="MarginContainer2/HBoxContainer/World4"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -32.0
offset_top = -32.0
offset_right = 32.0
offset_bottom = 32.0
grow_horizontal = 2
grow_vertical = 2
texture = ExtResource("3_tlb1w")
expand_mode = 1
stretch_mode = 5

[node name="World2" type="Control" parent="MarginContainer2/HBoxContainer"]
custom_minimum_size = Vector2(150, 0)
layout_mode = 2

[node name="Label" type="Label" parent="MarginContainer2/HBoxContainer/World2"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -69.0
offset_top = -86.5
offset_right = 69.0
offset_bottom = -62.5
grow_horizontal = 2
grow_vertical = 2
theme_override_fonts/font = ExtResource("2_h05dh")
theme_override_font_sizes/font_size = 24
text = "World 2"

[node name="TextureRect" type="TextureRect" parent="MarginContainer2/HBoxContainer/World2"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -32.0
offset_top = -32.0
offset_right = 32.0
offset_bottom = 32.0
grow_horizontal = 2
grow_vertical = 2
texture = ExtResource("3_tlb1w")
expand_mode = 1
stretch_mode = 5

[node name="World3" type="Control" parent="MarginContainer2/HBoxContainer"]
custom_minimum_size = Vector2(150, 0)
layout_mode = 2

[node name="Label" type="Label" parent="MarginContainer2/HBoxContainer/World3"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -69.0
offset_top = -86.5
offset_right = 69.0
offset_bottom = -62.5
grow_horizontal = 2
grow_vertical = 2
theme_override_fonts/font = ExtResource("2_h05dh")
theme_override_font_sizes/font_size = 24
text = "World 3"

[node name="TextureRect" type="TextureRect" parent="MarginContainer2/HBoxContainer/World3"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -32.0
offset_top = -32.0
offset_right = 32.0
offset_bottom = 32.0
grow_horizontal = 2
grow_vertical = 2
texture = ExtResource("3_tlb1w")
expand_mode = 1
stretch_mode = 5

[node name="Panel" type="Panel" parent="MarginContainer2"]
layout_mode = 2
size_flags_vertical = 0
theme_override_styles/panel = SubResource("StyleBoxFlat_llkcd")

[connection signal="pressed" from="MarginContainer/Panel/VBoxContainer/Play" to="." method="_on_play_pressed"]
[connection signal="pressed" from="MarginContainer/Panel/VBoxContainer/Options" to="." method="_on_options_pressed"]
[connection signal="pressed" from="MarginContainer/Panel/VBoxContainer/Exit" to="." method="_on_exit_pressed"]
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.