#Trying to generate animations dynamically in code from sprite sheets.

28 messages · Page 1 of 1 (latest)

amber patrol
#

I'm loosing my mind. I've wasted several hours now trying to get this working, tried extensively googling my problem, tried asking chatgpt; and i'm getting nowhere.

I have a generic 2d npc that i'm trying to code so that you can essentially skin it to be any unique npc using sprite sheets, so that I don't have to set up each individual npc's animations, and save myself time in the future.

I'm using RPG maker style 3x4 cell sprite sheets where each row is a different animation, and the columns are the frames.

I want to be able to have an exposed variable in the inspector tab where I can just drag and drop the sprite sheet for unique instances of this npc when I add them to a room/level scene, and have the npc's script generate it's animations on the fly.

I'm very new to scripting in godot, and it's been really difficult, but I really want to move away from game maker studio 2 (the engine I was using before)

When I try googling anything along the lines of "godot 4 animation script" or "godot 4 animation code" or the like, I don't find anything relevant, and all the posts seem to just say to manually set up the animations in the animations tab of an animated sprite2d node; which is the whole thing i'm trying to avoid doing.

Surely there has to be some way to import a sprite sheet through an exposed variable in the inspector tab, and then use scripting to dynamically generate animations by carving up the sprite sheet, right? Surely, right?

I feel like it should be as easy as dividing the width of the sprite sheet by 3, the height by 4, and then iterating through a dictionary with the names of the 4 animation directions (down, up, left, right) and then iterating through and assigning them frames cropped from the sprite sheet. It sounds simple, but i'm really struggling to figure it out. If someone could help me you'd be a lifesaver.
I don't want to have to manually set up 100's of npc animations.

gloomy void
#

all of which look to me like they match what you're trying to do?

worn temple
#

If you swap out the texture property of a sprite2d you only have to make the animations once

#

But you'll need to use an animationplayer as well

gloomy void
#

you might want a little extra information (like: how does Godot know you have a 4x3 sheet and not a 1x12 sheet? You have to tell it that, which isn't quite a one-stop shop). You might want to make a little resource to hold that so that you can encode your special semantics ("always 4x3! Each row means left/right/up/down!"), but I think it's annoying and you shouldn't do this last step (unless: you're really annoyed without it 😁)

worn temple
#

Since sprite2d doesn't animate itself

#

I think a lot of tutorials use AnimatedSprite2D since it's easier to explain than AnimationPlayer, but I think AnimationPlayer is better for your usecase

amber patrol
worn temple
#

For animation player, you only need to set up the animations once

#

then you can swap out the texture file

#

and the animations will still be there

amber patrol
#

Would that only work if the sprite sheet stays the same size?

worn temple
#

No, sprite2d uses number of rows and columns to determine what part of the spritesheet to show

#

so the size doesn't matter

#

though if you only key the frame number, then you can also change the number of rows and columns

amber patrol
#

How would I swap out the sprite sheet? Do I have to duplicate the npc scene for every sprite sheet, or can I use the same character 2d scene and just change the sprites on individual instances?

worn temple
#

You can change the sprite2d's texture property on each instance

#

So you don't have to make a different .tscn file for every spritesheet

amber patrol
#

How do I go about that?
I've only figured out how to add frames using the grid selector thing, so I don't even know where to drop the sprite sheet to change it while keeping the animations the same.

worn temple
#

the grid selector thing is for AnimatedSprite2D

#

I meant that you use Sprite2D and AnimationPlayer

amber patrol
#

Ah, gotcha.

I'll give that a look later then.

Hopefully it's at least closer to what I'm looking for.

worn temple
#

all you need to do is change this one property

#

which can easily be done through code as well

#

since it's just the texture property

amber patrol
#

Got it, that sounds a lot better than all the things I've tried that haven't worked 👍