How to use Bitfields?

Godot Version

4.2

Question

Hi,

I’m trying to use the method

Font.get_multiline_string_size

to calculate the width of my Textbox before I draw it . The method has a bitfield parameter,

LineBreakField

TextServer — Godot Engine (stable) documentation in English, which I need to set to correctly calculate the size.

As far as I understand, you can set multiple of these flags for some desired effect.
Like add them together or use bitwise operations, which will basically produce the same effect:

var flags = TextServer.BREAK_MANDATORY ^ TextServer.BREAK_WORD_BOUND ^ TextServer.BREAK_TRIM_EDGE_SPACES

However, Godot will always complain: Integer used when an enum value is expected. If this is intended cast the integer to the enum type. and the official documentation does not give much info on this. I couldn’t figure out how it’s supposed to be used.

Do I always have to cast it or is there an official way to add these flags together and if I do have to cast it, how do I do it?

I think you will need to cast it as LineBreakFlag, maybe this?

var flags:TextServer.LineBreakFlag = var flags = TextServer.BREAK_MANDATORY ^ TextServer.BREAK_WORD_BOUND ^ TextServer.BREAK_TRIM_EDGE_SPACES

No. I only get additional complaints from Godot.
Cannot assign 19 as Enum “TextServer.LineBreakFlag”: no enum member has matching value.

Try just LineBreakFlag

That doesn’t compile. The others compile at least. I’m just asking, because I want to know how to deal with bitfields, because the Editor is complaining. I guess it’s jsut a warning and i can ignore it.

Cannot reproduce your issue. Here’s what I did:

func _ready() -> void:
	# the flags you've posted about, no changes
	var flags = TextServer.BREAK_MANDATORY ^ TextServer.BREAK_WORD_BOUND ^ TextServer.BREAK_TRIM_EDGE_SPACES

	# get a font resource 
	var font = $Label.get("theme_override_fonts/font")

	# default values except for 'flags', no warnings
	print(font.get_multiline_string_size("Test", 0, -1, 16, -1, flags))

Can you share a full example of what you’re doing?

1 Like

It’s not an issue per se. The code works, but I get warnings from the Godot IDE, which makes me think I’m not using it correctly. I’m doing basically the same thing or so I think

class_name ComicTextBox extends Label

func _init(t: String):		
    var font_size = 18
    autowrap_mode = TextServer.AUTOWRAP_WORD
    add_theme_stylebox_override("normal", ComicStyleBox.new())
    add_theme_font_override("font", load("res://font/CRIMFBRG.TTF"))
    add_theme_color_override("font_color", Color.BLACK)
    add_theme_font_size_override("font_size", font_size)
    add_theme_constant_override("line_spacing", -1)
    text = t

    var flags = TextServer.BREAK_MANDATORY ^ TextServer.BREAK_WORD_BOUND ^ TextServer.BREAK_TRIM_EDGE_SPACES)
    var string_size = get_theme_font(font).get_multiline_string_size(t, 0, 405, font_size, -1, flags)

    custom_minimum_size.x = string_size[0] + 58
    position.y = 76

I could just tell the Godot IDE to ignore the warning and be done with it. Just wanted to know, if I’m maybe overlooking something.

Took me a while, but: The warning is about the alignment argument, not about brk_flags!

So instead of this:

var string_size = get_theme_font(font).get_multiline_string_size(
    t,
    0, # <-- int
    405,
    font_size,
    -1,
    flags
)

you would to do this:

var string_size = get_theme_font(font).get_multiline_string_size(
    t,
    HORIZONTAL_ALIGNMENT_LEFT, # <-- enum
    405,
    font_size,
    -1,
    flags
)
2 Likes

oh wow… ty