0 votes

Hello there!
I am writing a function that sets an UI element's font size dynamically according to it's rect_size. My problem is, that the font size of all the elements get overwritten when setting the next one's.
Here is my code:

func bestFitGroup(group,maxSize):
  for node in get_tree().get_nodes_in_group(group):
      bestFitNode(node.get_path(),maxSize)
  for node in get_tree().get_nodes_in_group(group):
      print(node.get_font(str(node),"").size) #for some reason always returns the last node's font size for every node           

func bestFitNode(nodePath,maxSize):
    var node = get_node(nodePath)
    var font = node.get_font(str(node),"")
    var rect = node.get_size()
    font.size = clamp((rect.x + rect.y) / 2,0,maxSize)
    print(font.size) #prints the proper size
    get_node(nodePath).add_font_override(str(node),font)

When bestFitNode runs, its sets the font size of the button's font properly, but when it runs for the second time, the first button's font size also gets overwritten, even tho it should have no effect on that.
For example:
The bestFitGroup gets called for a group that contains 2 buttons

  • The first button's font size becomes 60
  • The second button's font size becomes 30
  • The first buttons front size also becomes 30 for some sorcerous reason

I am new to GDScript, what am I missing here? After the function runs, the var font should not exsist anymore, just it's value passed to add_font_override() function.
Thank you for reading!

asked Apr 12 in Engine by tralk331 (12 points)

Maybe there's something in the theme of the font which makes the font size of the first button become 30 instead of 60? This scenario makes me think that the fonts share a size property somewhere.

Please log in or register to answer this question.

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.