+2 votes

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
...
cardn[cardcount].set_scale(s)
print(cardn[cardcount].get_scale())

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?

asked Feb 7 in Engine by bluemage (51 points)
edited Feb 8 by bluemage

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

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.

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

    #cardn[cardcount].set_expand(true)
    cardn[cardcount].set_scale(s)
    print(cardn[cardcount].get_scale())
    cardn[cardcount].set_custom_minimum_size(s)
    #cardn[cardcount].set_stretch_mode(5)
    cardn[cardcount].minimum_size_changed()
    cardn[cardcount].rect_scale = s
    #cardn[cardcount].apply_scale(s)
    #cardn[cardcount].set_size(s)
    #cardn[cardcount].scale = .5

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

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. getscale returns that the rectscale value is changing but my images don't shrink.

    cardn.append(TextureRect.new())
    cardn[cardcount].set_name("card" + String(cardcount))
    cardn[cardcount].texture = actual[showcard] 

    #cardn[cardcount].set_stretch_mode(3)
    #cardn[cardcount].set_expand(true)
    cardn[cardcount].set_scale(s)
    print(cardn[cardcount].get_scale())

    #cardn[cardcount].set_custom_minimum_size(s)
    #cardn[cardcount].minimum_size_changed()
    #cardn[cardcount].rect_scale = s
    #cardn[cardcount].apply_scale(s)
    #cardn[cardcount].set_size(s)
    #cardn[cardcount].scale = .5

    print(cardn[cardcount].name)

    get_node("HBoxContainer").add_child(cardn[cardcount])

3 Answers

+1 vote

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)
answered Feb 7 by jgodfrey (5,496 points)

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

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...

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.

also I don't have access to the inspector I'm trying to do this with code....

+1 vote

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 = ImageTexture.new()
    itex.create_from_image(image)
    return itex
answered Feb 8 by bluemage (51 points)
+2 votes

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].set_stretch_mode(5)
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.

answered Feb 8 by njamster (8,788 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.