0 votes

I am programmatically adding a label to the screen, which drifts up then fades out. The problem I am having is if I add multiple instances they seem to be using the same rect_position and overlap on top of each other:

FadlingLabel.gd:

extends Label
class_name FadingLabel


var action_start = null

const LABEL_RISE_SPEED = 15
const ACTION_TIME = 5
const FADE_DELAY = 3


func _process(delta):
    if action_start != null:
        action_start = action_start + delta
        rect_position = rect_position - Vector2(0, LABEL_RISE_SPEED * delta)
        if (ACTION_TIME - action_start < FADE_DELAY):
            modulate.a = 1.0 - ((action_start-FADE_DELAY) / (ACTION_TIME-FADE_DELAY))
        if (action_start >= ACTION_TIME):
            queue_free()

func start_fade(string, pos):
    set_text(string)
    rect_position = pos
    action_start = 0

And using like so:

var energy_tick = FadingLabel.new()
add_child(energy_tick)
energy_tick.start_fade(ENERGY_USED_FORMAT_STRING % amount, Vector2(200, 200))
Godot version 3.2.2
in Engine by (134 points)

Sadly this is the correct behavior by default.

If you want auto positioning use V or H BoxContainers as the one that adds the label child or change the rect_position for each instance. One way would be to get the last child then offset from that.

rect_position = get_children()[-1].rect_position + Vector2(xOffset, yOffset)

change the rect_position for each instance

How do I do that? I thought by changing it in the script it would reference itself and not anything else

Your code errors when I replace rect_position = pos with it; "Expected 0 params for get_children()

Made a mistype it's supposed to be get_children()[-1] not get_children(-1). My bad

1 Answer

0 votes
Best answer

I solved it, it was because the scene I was instancing them in was a MarginContainer, so it must've been triggering layout placement, but when I changed the root node to a CanvasLayer they didn't jump around anymore

by (134 points)

Just for curiosities sake do you have screenshots of the differences?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.