How do I scale a TextureRect in code?

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

So I solved one of my problems using a TextureRect but it doesn’t seem to scale at all.

var s = Vector2()
s.x = 0.5
s.y = 0.5

This seems to output the .5 .5 vector2 I have but the image doesn’t scale at all. I’m trying to shrink it to about half the image size. What am I doing wrong?

have you tried changing the Stretch Mode property of the TextureRect?

Eric Ellingson | 2020-02-07 21:04

yes I can’t seem to figure it out. I tried going through them all with no luck but there may have been other factors so I’m not sure how to do it right if that’s the solution.

bluemage | 2020-02-07 23:05

I’ve tried various forms of this code to no avail. when I turn on expand the images disapear.

	cardn[cardcount].rect_scale = s
	#cardn[cardcount].scale = .5

setting scale in the GUI of godot works but I’m trying to do this with code…

bluemage | 2020-02-07 23:06

Here’s my full thought process. I’ve deleted Hboxcontainer and readded it to the scene so its in defaults. strech mode alone doesn’t seem to have any effect 0 -7 it all displays the pictures but not scaled down and when on 3 or 4 with expand it writes over the same spot and negates hboxcontainer. the other strech mode options with expand make the images disapear. get_scale returns that the rect_scale value is changing but my images don’t shrink.

	cardn[cardcount].set_name("card" + String(cardcount))
	cardn[cardcount].texture = actual[showcard] 
	#cardn[cardcount].rect_scale = s
	#cardn[cardcount].scale = .5



bluemage | 2020-02-08 01:22

:bust_in_silhouette: Reply From: jgodfrey

You probably want to set the rect_scale property. So, assuming cardn is an array of TextureRect, something like:

cardn[cardcount].rect_scale = Vector2(2, 2)

I’ve tried this it doesn’t seem to shrink my images when a variable definted as Vector2 is .5 .5

bluemage | 2020-02-07 23:11

It definitely works here. It might have to do with how it’s anchored, or how the Expand and Stretch Mode properties are defined in the inspector…

jgodfrey | 2020-02-07 23:30

Its a child of an hboxcontainer. setting strech mode to 3 or 4 makes the image show with expand on but it writes over itself and doesn’t arrange in the hboxcontainer and doesn’t scale.

bluemage | 2020-02-08 00:59

also I don’t have access to the inspector I’m trying to do this with code…

bluemage | 2020-02-08 01:40

:bust_in_silhouette: Reply From: bluemage

I was able to get it to do what I want with this function. Not sure why I can’t do it with scaling though.

func get_resized_texture(t: Texture, width: int = 0, height: int = 0):
	var image = t.get_data()
	if width > 0 && height > 0:
		image.resize(width, height)
	var itex =
	return itex

Tnx! Realy working!

TimurKady | 2020-12-05 18:58

:bust_in_silhouette: Reply From: njamster

I just commented on this over here , but for better visibility will post it here as well:

var s = Vector2(2.0, 2.0)
cardn[cardcount].expand = true
cardn[cardcount].rect_min_size = cardn[cardcount].rect_size * s

This will only work of course, if rect_size is set to the size of the texture. Which will happen automatically when you add a texture in the inspector, but has to be done manually if you’re doing that from within GDscript. I assume this won’t be an issue, given that your designing a card game and I expect all cards to have a uniform size.