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!