Hi, dear godot master
I am trying to do Making Buttons with animations in godot 4.0.2
and what confuse me it's:
node global position and size alter after awhile, it makes:
1. If I dont await 0.01second.finished in _ready() virtual method,
selector will appear in wrong position at first
2. if I dont set pivot_offset in script, no matter what's the value in inspector, it turns to 0,0
it's a bug or what I doing wrong?
extends Control
@export var bg:ButtonGroup
@onready var icon: Sprite2D = $Icon
@onready var v_box_container: VBoxContainer = $VBoxContainer
@onready var buttons := bg.get_buttons()
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
await create_tween().tween_interval(0.1).finished
for i in buttons:
i.mouse_entered.connect(i.grab_focus)
i.focus_entered.connect(movetobutton.bind(i))
i.focus_exited.connect(moveaway.bind(i))
i.pressed.connect(clickbutton.bind(i))
i.pivot_offset = i.size/2
buttons[0].grab_focus()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func movetobutton(button:Button):
var tween = create_tween()
tween.tween_property(icon,"position",button.global_position,0.2).from_current()
tween.tween_property(button,"scale",Vector2.ONE * 1.1,0.15).from_current()
func moveaway(button:Button):
var tween = create_tween()
tween.tween_property(button,"scale",Vector2.ONE,0.15).from_current()
func clickbutton(button:Button):
var t = create_tween().set_parallel(true).set_trans(Tween.TRANS_ELASTIC)
t.tween_property(button,"scale",Vector2.ZERO,0.3).from_current()
t.tween_property(button,"rotation",deg_to_rad(-40),0.3).from(deg_to_rad(40))
await t.finished
t = create_tween().set_parallel(true).set_trans(Tween.TRANS_ELASTIC)
t.tween_property(button,"scale",Vector2.ONE,0.3).from_current()
t.tween_property(button,"rotation",deg_to_rad(0),0.3).from_current()
The code you shared seems like the await shouldn't change anything in this script specifically. Do you have other UI elements with scripts in your project? Or is this the only code running? If this is the only code running, in an isolated scene could you please show us the scene tree? Without more information we may not be able to help you figure out the cause of the issue.
Okay it makes sense, so the problem is that you have a parent box container node (VBoxContainer) that is supposed to control the child buttons, but you're kind of bypassing it by trying to modify a property of the button nodes that the container doesn't automatically account for.
When you change the children of a container node, the container will generally need to receive some information causing it to update. It appears changing the pivot doesn't trigger the container update, but re-assigning the position or size of one of the buttons instead of creating a tween should trigger the container update.