Godot Version
4.5.1 stable Windows 11
Question
So I have this code:
print("status icons: ", status_icons)
print("status icons size: ", status_icons.size())
status_icon_container.size.x = (clamp(status_icons.size(), 0, 7) * 9.0)
status_icon_container.position.x = -status_icon_container.size.x / 2.0
print("size x: ", status_icon_container.size.x)
print("position x: ", status_icon_container.position.x)
It’s in a draw_status_effects function that is called everytime an enemy gains or loses a status effect, which deletes all the TextureRect children of status_icon_container (GridContainer), clears the status_icons array that contains them, then for each status effect the enemy is currently experiencing, it creates a TextureRect, adds it as a child of status_icon_container, and appends it to the status_icons array, then this code is run. However, sometimes it prints out this:
status icons: [@TextureRect@29726:<TextureRect#2133541895536>]
status icons size: 1
size x: 26.0
position x: -13.0
And sometimes it prints out this:
status icons: [@TextureRect@29725:<TextureRect#2133441232139>]
status icons size: 1
size x: 17.0
position x: -8.5
In theory, if status_icons has only one item in it, and its size returns 1, then status_icon_container.size.x, which is set to 9 * 1, should return 9. But it sometimes returns 17, and sometimes 26. I have no idea where these numbers came from. I thought it was a problem with controls needing to set custom_minimum_size instead of size, but that wasn’t it. I also don’t see any operations that add to one of these properties instead of directly setting them everytime the function is run.
What is happening here? Any help is appreciated.
My guess here is that your container size is being automatically overridden by its parent or a minimum size setting elsewhere. It did look rather perplexing but I can’t see anything wrong with your maths. But container sizes are rather complex in how they get set. So I would take a look at your container in your tree structure and try to see what is overriding it.
The funny numbers you are getting back is probably due to whatever window size you happened to have at the time, also affecting container sizing.
That would be my best guess, otherwise yes, that is very odd indeed.
EDIT: Just to be clear:
(clamp(status_icons.size(), 0, 7) * 9.0)
is fine. But your container might not be applying it due to container constraints from the parent.
2nd Edit:
I just saw that you mentioned this, but containers are still overridden by their parents settings. Not just minimum size is applied.
The status_icon_container is not a child of any container, it’s a child of an Area2D (which is the enemy).
Ok, aread2D won’t affect it. But the container is still constrained by the size of what’s in it. If your icon is 26 wide, then you cannot set the container to size 7. So check whats inside the container.
There is definitely nothing wrong with your maths, so it must be that the value is invalid for your container because it is being overwritten.
Anyway, I hope that helps. An Intriguing puzzle though.
The only things inside the container are these TextureRects that I add with code:
var icon : TextureRect = TextureRect.new()
icon.size = Vector2(8,8)
icon.set_anchors_preset(Control.PRESET_BOTTOM_LEFT)
match status_effect :
### setting texture based on type of status effect
status_icon_container.add_child(icon)
status_icons.append(icon)
And the textures being applied to these TextureRects are all exactly 8x8 pixels.
Oh, perhaps the texture is giving it a size that is overiding the size your are trying to give it. Try setting the icon explicitly to ignore texture size. If its not that then I don’t know and my best guess was wrong.
PS it seems to be marked as experimental in the docs. I didn’t know that.
Experimental: Using EXPAND_FIT_WIDTH, EXPAND_FIT_WIDTH_PROPORTIONAL, EXPAND_FIT_HEIGHT, or EXPAND_FIT_HEIGHT_PROPORTIONAL may result in unstable behavior in some Container controls. This behavior may be re-evaluated and changed in the future.
1 Like
Hey, I am glad you fixed it. Was it this?
icon.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
1 Like
It was indeed, thank you for the solution!
1 Like