godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.12k stars 69 forks source link

Revamp Animated Sprite to allow swapping the texture it uses #2302

Open Shadowblitz16 opened 3 years ago

Shadowblitz16 commented 3 years ago

Describe the project you are working on

Spaceship game

Describe the problem or limitation you are having in your project

My ship consists of multiple animated parts which I find animation tree too complicated to use

Describe the feature / enhancement and how it helps to overcome the problem or limitation

I want to be able to easily swap a animated sprite's whole texture out.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

AnimatedSprite.texture = ...

If this enhancement will not be used often, can it be worked around with a few lines of script?

not sure how often it would be used but it certainly would be convenient

Is there a reason why this should be core and not an add-on in the asset library?

The sprite node allows it

KoBeWi commented 3 years ago

The problem is that AnimatedSprite doesn't have its own texture, it uses SpriteFrames, which might use multiple textures. Your best workaround could be either making multiple SpriteFrames resources (you can save them to file) and swap them or change the texture manually for each frame by using set_frame() (should be easy to do in a loop).

Shadowblitz16 commented 3 years ago

I mean why can't it just use reference rectangles or atlas textures for the frames?

this way it can both have frames and be able to swap textures.

KoBeWi commented 3 years ago

This is what it already does, but each frame might reference a different texture, so you can't just swap all of it.

Shadowblitz16 commented 3 years ago

exactly I propose to tie them to a single root texture so its easy to swap out

Zireael07 commented 3 years ago

@Shadowblitz16: That kinda defeats the purpose of the animated sprite, though.

Shadowblitz16 commented 3 years ago

no it doesn't it would do the same thing it would just allow users to swap the texture out and keep the animations.

alot of game engines allow doing this. not allowing it in godot severely limits the engine

KoBeWi commented 3 years ago

So uh

The sprite node allows it

Why don't you use Sprite then? It's kind of clear that the two were made for different purposes.

Shadowblitz16 commented 3 years ago

@KoBeWi because one doesn't support animations without a animation player Also they are both sprites one is just animated it doesn't mean it can't or shouldn't support texture swapping.

djrain commented 10 months ago

change the texture manually for each frame by using set_frame()

Gave this a try to duplicate SpriteFrames for different player skins. But I don't see how to give it the right texture for a spritesheet. Giving it the spritesheet texture results in all frames just showing the whole sheet, and SpriteFrames.get_frame_texture() returns a CompressedTexture2D, which is useless for this.

Something definitely needs to change to make this easier / possible.

KoBeWi commented 10 months ago

SpriteFrames use AtlasTexture internally. Try using get_frame_texture(anim, idx).atlas = new_sprite (although the code gets more complex if you want to display multiple skins at once).

djrain commented 10 months ago

@KoBeWi that's what I tried, it doesn't work because get_frame_texture() returns a CompressedTexture2D, not an AtlasTexture

KoBeWi commented 10 months ago

Well this means that your SpriteFrames is setup incorrectly. How did you create it? I did a quick tests and it returns AtlasTexture.

djrain commented 10 months ago

Ah okay, maybe it was because my SpriteFrames had been imported from Godot 3 (though it was working otherwise). It works with a fresh SpriteFrames. Thanks!

Still, this workaround is quite convoluted, surely there's some room for improvement. For starters, if that will always return an AtlasTexture, that should be specified instead of just Texture2D

KoBeWi commented 10 months ago

It returns AtlasTexture only when you assign the spritesheet from the editor. But you can create an animation using separate textures for frames and they can be of any type.