Fitting a texture into a dinamically sized area

Godot Version

4.4

Question

Hello, I am basically completely new to GoDot and game making in general. I have my source image I want to use as a texture for my sprite2D node, but the source image is way too big. I want to define rectangle area in which the texture should fit (without clipping it, I need the whole image to be visible). I know I could use scale to achieve this but it feels like a very brute-force approach. I would like to experiment with different sizes for the area, so the scale would need to be dynamically changing, I could write a script calculating what the scale should be based on the rectangle if I integrate its size as variables, but then it will be slightly off because of approximation errors. I noticed for control nodes you can use stretch_mode to help with this. Is there a similar solution for 2D nodes, and why is stretch_mode specific to control nodes?

what do you need this for?
A sprite2D is a node that can be moved by a Node2D or CharacterBody2D.
scaling is the way to do it.
if you need to fit it to certain constraints, you have to calculate it manually.

BUT, if you don’t need this to be animated, or if you are willing to animate it yourself, you can use a Node2D and draw the sprite manually by overriding the _draw method:

@export var image : Texture2D
@export var rect : Rect2
@export var region : Rect2

func _draw() -> void:
	draw_texture_rect(image, rect, region)

but you can also just use a control if this thing doesn’t need to move, like if it’s a final fantasy or pokemon style combat.

no.

control nodes are designed to fit within each other and accommodate for different resolutions. Node2Ds are designed to move around independently of each other.

1 Like

Thank you for the detailed explanation! I have some ideas for a card game I’d like to try and want to experiment with the ratios and sizes of the cards. Ideally they should take up the same space relative to the whole screen no matter the resolution but I guess there isn’t just a simple toggle for that haha. I’ll just work in 1080p and figure out different resolutions later down the line, just wanted to make sure I’m not missing something obvious and complicating the process needlessly. And again, thank you for your input!