How set sprite texture size

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mefihl
:warning: Old Version Published before Godot 3 was released.

How set texture size on sprite node, after dynamic load? After texture load size node is resize to texture size.

var image = ("res://assets/images.png")
get_node("sprite_node").set_texture(image)
:bust_in_silhouette: Reply From: dc1a0

The easiest way to do this is to use sprite scale like so:

var image = (‘res://assets/images.png’)
var scale = Vector2((percent of width), (percent of height))
var this_sprite = get_node(“sprite_node”)
this_sprite.set_texture(image)
this_sprite.set_scale(scale)

If anyone wanted to set size in pixels, then there is how you do it:

var is = get_node("sprite").get_texture().get_size() #image size
var th = 50 #target height
var tw = 100 #target width
var scale = Vector2((is.x/(is.x/tw))/50, (is.y/(is.y/th))/50)

Somehow while those /50, should be logically /100, they shouldn’t. For me, it made the sprites twice as small, so when setting /50 it was perfect. Might be different for you.

Soaku | 2017-05-10 18:39

Just in case someone else is also looking for it in 3.x, its changed to:

var s = Vector2()

s.x = x_scale
s.y = y_scale
$sprite.set_texture(your_texture)
$sprite.scale = s

stragemque | 2018-04-12 11:50

:bust_in_silhouette: Reply From: boy_hax

var size_to=Vector2(720,1080)
var size=texture.get_size()
var scale_vactor=size_to/size
scale=scale_vactor

You are right, I like it most, but you have some syntax error (at least in Godot 3.4).
Here is the correction

	var sizeto=Vector2(80,80)
	var size=texture.get_size()
	var scale_factor=sizeto/size
	scale=scale_factor

Losatu | 2022-03-29 19:16