Nodes added from script share properties

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By beaverusiv

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:

extends Label
class_name FadingLabel

var action_start = null

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):

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

And using like so:

var energy_tick =
energy_tick.start_fade(ENERGY_USED_FORMAT_STRING % amount, Vector2(200, 200))

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)

Wakatta | 2021-04-03 21:03

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()

beaverusiv | 2021-04-03 21:12

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

Wakatta | 2021-04-04 20:45

:bust_in_silhouette: Reply From: beaverusiv

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

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

Wakatta | 2021-04-04 20:39

beaverusiv | 2021-04-04 20:57