The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

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

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!

in Engine by (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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.