I am doing an endless top down game. Middle segment, a tree trunk will be generated endlessly. But player will cut wood and that endless segment will all position.y + (go down) so i need to generate new segments as other segments go down. Problem is , i need to generate the new segment in a way that it will end up just top of most top segment. So i need to know node size in pixels and position. so i can generate new one accordingly.
Are all of the tree segments are separate nodes? Cause in that case you can just keep track of the top segment via a variable and add it to the top of that.
For calculating what coordinate to place the new segment on, I’d probably recommend just having a hardcoded variable in the segment node so that you can adjust the margins to precisely what you want. Or if you need it to be dynamic (ie randomly sized segments), then you can call get_rect() on whatever node is the sprite of the object.
For example:
var topSegment : TreeSegment
func addSegment(segment : TreeSegment):
#topSegment.top is a constant variable you set to be where the new segment is added relatively to the current top
segment.global_position = topSegment.global_position + topSegment.top
topSegment = segment
add_child(segment)
(These are assuming a lot of things, such as the segments are sibling nodes under one parent tree node and not each segment is not the child of the previous one. The tree segments not being centered and having their position anchored on the top left. And that you want the tree segments to be placed on the top center of the previous one. I encourage you to try fitting the code to your needs yourself!)
To answer the original question though, you can’t get a boundary rect of any Node2d. As while a lot of Node2ds have visuals with bounding boxes, some aren’t such as AudioStreamPlayer2D. Node2d is for any element that can be positioned in space not just anything you can see on screen.