Hi,

I'm having some trouble trying to figure out how to get the bounds of a 2d sprite.

This 2d sprite is meant to be a dialog box and its scene tree currently looks like:

[DialogBoxNPC -> DialogBox -> Sprite root]
- [NinePatchRect bg01]
- [NinePatchRect bg02]
- [NinePatchRect bg03]
- [NinePatchRect bg04]
- [AnimatedSprite portrait]
- [RichTextLabel textBox]

The node type DialogBoxNPC extends DialogBox (which has a similar tree structure, but contains only a single [NinePatchRect bg] and [RichTextLabel textBox]) which extends Sprite as the base class.

Looking it up, it seems it's possible to normally get the size of a sprite by going sprite.texture.get_size() but for me, the texture property is null.

The dialog box also is added to the stage via code. Something like:

var dialogBoxNPCScene:PackedScene = load("res://dialogBoxNPC.tscn");
var dialogBox:DialogBox = dialogBoxNPCScene.instance();
dialogBox.init(<some init stuff>);
viewport.add_child(dialogBox);

I'm doing the size check within _ready():

func _ready():
	print(self.texture.get_size()); # complains texture property is null

The ultimate goal is to get the bounding rectangle and do a check to see if it is fully contained within the viewport rectangle (aka, not off-screen) and handle it if it is/isn't. Something like;

get dialogbox bounds -> convert local to global space -> check against viewport bounds

I could do the math and hardcode the size numbers manually (since I know the size of everything on stage), but it gets a bit convoluted as there are multiple backgrounds (all but one becomes hidden) to handle different orientations which are unknown until placement time. Then all the numbers change if the graphics are edited.

In general, I'm finding that trying to figure out a good method to place dialog boxes so they match with speaking npcs is a surprisingly non-trivial task. I thought it would just be a quick thing. >.>

Try this:

onready var sprite = get_node("Sprite")

func _ready():
	var sz = sprite.get_rect().size
	print("size: ", sz.x, " x ", sz.y)

Hm, it complains that sprite is null.

Trying (which I assume basically does the same thing):

self.get_rect().size
get_node(".").get_rect().size

does give something, but I end up with a rectangle of (0, 0, 1, 1)

2 years later