+1 vote

Hello,

I'm trying to implement a system where the player can have custom animations, and it's supplied to the engine through a folder in the user:// directory as a sprite sheet. I'm trying to make a preview icon next to the player select that only displays the first frame of an animation. The preview icon is a TextureRect.

In order for it to work I need to be able to cut the image programatically, however, three hours of google searching led me to nothing, so I ask: is there a way to do this in GDScript?

Thanks in advance

in Engine by (32 points)

1 Answer

+1 vote

You can do this and it works:
First, change the TextureRect to a sprite. Turn on the region and set it to the size of your Sprite in the sprite sheet.

$Sprite.rect(0,0,20,20)

Call the Sprite, get the "rect" property, and set the first 2 numbers to the position where you want it. Then, the last 2 numbers are the size of the rect.
This is a simple way of doing this.

by (99 points)

I like your answer, and I think this would work for sure. However, because my UI is supposed to be dynamic, like when the screen size and/or resolution changes, the UI is supposed to scale accordingly. I'm currently looking into AtlasTextures to see if I am able to do what I wish to do using that, if you have any thoughts on how that might work.

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.