Trying to instantiate a simple label but its not working

Godot Version

func meteorHealth(minDMG : int, maxDMG : int): var projDMG : int = randi_range(roundi(minDMG), roundi(maxDMG)) var dmgText = PROJ_DMG.instantiate() dmgText.damage = projDMG dmgText.global_position = global_position add_child(dmgText) meteorHP -= projDMG

Question

This is my script to instantiate a simple label showing damage done, but I cant make it work. I placed a print() on the PROJ_DMG scene to check if it's working, and it prints every time I hit my target. I just cant seem to make the label appear on my screen. I'd appreciate any reply

To format code on the forums, put it between triple backticks:

```gdscript
code code
code code
```

With that, your code looks like:

func meteorHealth(minDMG : int, maxDMG : int):
 	var projDMG : int = randi_range(roundi(minDMG), roundi(maxDMG))
 	var dmgText = PROJ_DMG.instantiate()
 	dmgText.damage = projDMG
 	dmgText.global_position = global_position
 	add_child(dmgText)
 	meteorHP -= projDMG

I’m assuming PROJ_DMG is a scene containing a label? There are several things that could be going wrong here:

  • you set .damage, but something needs to set the label’s .text field or it will be blank and potentially zero sized
  • is the label set to expand to fit its text?
  • setting .global_position to this node’s global_position before calling add_child() will just add it with a local offset of (0.0, 0.0), is that onscreen?
  • do you need to call dmgText.show(), or is it visible by default?

It would be useful here to see the code and node hierarchy for PROJ_DMG.

This is the only thing I have for PROJ_DMG. I added the print() to check if it’s going offscreen, but it’s not. I also put a random text in the label’s inspector. It is also passing the correct damage value from my meteorHealth function. The label is also not hidden.

var damage : int
@onready var label_damage: Label = $labelDamage

func _ready() -> void:
	label_damage.text = str(damage)
	print(self.global_position)
	print(label_damage.text)```

I’m assuming this is going to be a damage number that floats up and fades?

You could probably simplify this:

func meteorHealth(minDMG : int, maxDMG : int):
	var damage: int   = randi_range(roundi(minDMG), roundi(maxDMG))
	var label:  Label = Label.new()

	# By default this will have a position of (0, 0) relative to he parent,
	# you might want to offset it?
	# label.position = Vector2(0.0, 20.0)

	# Attach a script...
	# label.script = preload("res://damage_label.gd")

	label.text = str(damage)
 	add_child(label)

 	meteorHP -= damage

I’d try that, and see if anything shows. I’m wondering if you need an offset, or if you need to change the draw priority; maybe the label is there, but it’s behind its parent?

1 Like

I finally figured it out. I did not set an HP for my target which led to a conditional statement with a queue getting triggered all the time. Thank you for trying to help my stupidity though. lol

1 Like