Get Sprite Dimensions

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By djmick

Hi, this should be simple, but I can’t figure it out. How do I get the dimensions of a sprite, and how do I set the dimensions of a sprite? In other engines, you would do something like sprite.getWidth(), but I have no idea in Godot. Thanks.

:bust_in_silhouette: Reply From: AlexTheRegent

sprite.texture.get_width() and sprite.texture.get_height()
As far as I know, you can’t resize sprite quickly. Instead you need to rescale it. If image has width of 100px, and you want to scale it to 150px, then you need to divide 150 by 100 and it will give you scale you need to set sprite.scale.x = 150.0 / 100.0 (note that you need at least one float in division to get float result (int/int=int, i.e. 150/100 = 1)).

One thing to note about getting the dimensions of a sprite or texture. The nodes themselves have a scale and the texture has its own scale or better yet size. Every node2D will have a property called scale in its Transform. And a sprite node will have both node2D scale and the sprites texture size. It’s important consider that if you change the scale of your sprite node2D then the values you get from sprite.texture.get_width() will be of the original texture and not the scaled version. To fix this simply multiply your width or height by the value that you set the scale of the node2D.

Azukala | 2023-06-09 17:03