Godot Version
v4.7.1.stable.official [a13da4feb] - CachyOS KDE Plasma (Wayland) x86_64
Question
Hi,
Is it possible to set draw index for text?
We need a text to be above some sprites.
Please provide GDScript code sample if you can.
Thank you.
Control nodes such as Label can have their z_index set. If you are using RenderingServer you can use canvas_item_set_z_index
Hi,
I can’t figure out how to do the above.
Please look at below source - it is how we draw text:
func DrawText(index, text, x, y, horizontalJustification, fontToUse, fontSize, scaleX, scaleY, rotations, red, green, blue, alpha, outlineRed, outlineGreen, outlineBlue):
if ( index > (TextCurrentIndex-1) ):
Texts.TextImage.append(RichTextLabel.new())
add_child(Texts.TextImage[index])
TextIsUsed[index] = true
var newTextDrawingOffsetY = 0
fontSize+=10
y-=24
var xValue = x
if horizontalJustification == 0:
Texts.TextImage[index].text = text
Texts.TextImage[index].set_use_bbcode(false)
elif horizontalJustification == 1:
Texts.TextImage[index].text = "[center]"+text+"[/center]"
Texts.TextImage[index].set_use_bbcode(true)
elif horizontalJustification == 2:
Texts.TextImage[index].text = "[right]"+text+"[/right]"
Texts.TextImage[index].set_use_bbcode(true)
elif horizontalJustification == 4:
Texts.TextImage[index].text = text
Texts.TextImage[index].set_use_bbcode(false)
var textWidth = Texts.TextImage[index].get_theme_font("normal_font").get_string_size(Texts.TextImage[index].text).x
xValue = x - (textWidth/2)
Texts.TextImage[index].clip_contents = false
Texts.TextImage[index].add_theme_font_override("normal_font", FontTTF[fontToUse])
Texts.TextImage[index].add_theme_font_size_override("normal_font_size", fontSize)
Texts.TextImage[index].add_theme_color_override("default_color", Color(red, green, blue, alpha))
Texts.TextImage[index].add_theme_constant_override("outline_size", 20.0)
Texts.TextImage[index].add_theme_color_override("font_outline_color", Color(outlineRed, outlineGreen, outlineBlue, alpha))
var textHeight = Texts.TextImage[index].get_theme_font("normal_font").get_string_size(Texts.TextImage[index].text).y
Texts.TextImage[index].global_position.x = xValue
Texts.TextImage[index].global_position.y = (y - newTextDrawingOffsetY)
Texts.TextImage[index].set_size(Vector2(VisualsCore.ScreenWidth, VisualsCore.ScreenHeight), false)
Texts.TextImage[index].pivot_offset = Vector2((VisualsCore.ScreenWidth / 2.0), (textHeight / 2.0))
Texts.TextImage[index].scale = Vector2(scaleX, scaleY)
Texts.TextImage[index].rotation = rotations
Texts.TextImage[index].canvas_item_set_z_index = 1 # ERROR HERE?
Texts.TextIndex.append(index)
Texts.TextScreenX.append(x)
Texts.TextScreenY.append(y)
Texts.TextHorizontalJustification.append(horizontalJustification)
Texts.TextSize.append(fontSize)
Texts.TextScaleX.append(scaleX)
Texts.TextScaleY.append(scaleY)
Texts.TextRotation.append(rotations)
Texts.TextColorRed.append(red)
Texts.TextColorGreen.append(green)
Texts.TextColorBlue.append(blue)
Texts.TextColorAlpha.append(alpha)
Texts.TextOutlineRed.append(outlineRed)
Texts.TextOutlineGreen.append(outlineGreen)
Texts.TextOutlineBlue.append(outlineBlue)
if ( index > (TextCurrentIndex-1) ):
TextCurrentIndex+=1
return(TextCurrentIndex-1)
It appears your TextImage[index] is a RichTextLabel, which is of Control type so it would use .z_index