Skip to content

SpriteAnimation: introduce imageRange#80

Open
eulerscheZahl wants to merge 2 commits intoCodinGame:masterfrom
eulerscheZahl:animation-imageRange
Open

SpriteAnimation: introduce imageRange#80
eulerscheZahl wants to merge 2 commits intoCodinGame:masterfrom
eulerscheZahl:animation-imageRange

Conversation

@eulerscheZahl
Copy link
Copy Markdown
Contributor

@eulerscheZahl eulerscheZahl commented Apr 13, 2026

SpriteAnimation.setImages can be quite expensive, as it serializes the whole image sequence. When the animation sequences changes to something else and then changes back (because a character plays an attack animation, changes moving direction, ...), the whole sequence gets serialized again.
There is the AnimationModule as an alternative, but it's not as easy to use.

Here is an example from Code Keeper - not even quoting a full frame:
12 0 I c5o12,c5o13,c5o14,c5o15,c5o16,c5o17,c5o18,c5o19,c5o20,c5o21,c5o22,c5o23;1210 0 I c4i21,c4i22,c4i23,c4i24,c4i25,c4i26,c4i27,c4i28,c4i29,c4i30,c4i31,c4i32,c4i33,c4i34,c4i35,c4i36,c4i37,c4i38,c4i39,c4i40,c4i41;1213 0 I c4i21,c4i22,c4i23,c4i24,c4i25,c4i26,c4i27,c4i28,c4i29,c4i30,c4i31,c4i32,c4i33,c4i34,c4i35,c4i36,c4i37,c4i38,c4i39,c4i40,c4i41;

While image names don't have to be in sequence, they are in most scenarios. So it's possible to compress them by just storing the prefix, the first and last suffix.
E.g. I c5o12,c5o13,c5o14,c5o15,c5o16,c5o17,c5o18,c5o19,c5o20,c5o21,c5o22,c5o23 would become IR c5o|12|23 with this commit.

It checks, if image names are in sequence. If they are, the new imageRange property is used to serialize and deserialize. Otherwise the old images is still used as a fallback solution.
From a user perspective nothing changes, this all happens behind the scenes.

Comment on lines +174 to +175
if (compressed == images) set("images", Stream.of(images).collect(Collectors.joining(",")), null);
else set("imageRange", compressed[0], null);
Copy link
Copy Markdown
Contributor

@CGjupoulton CGjupoulton Apr 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a spriteAnimation's images changes from a set to a range or vice versa, the other variable won't be cleared, I fear it would create problems client side

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

easiest scenario is if the compressed version also goes inside the images param. maybe with a special prefix

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or just detect the presence of the pipe character

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, images and imageRange shouldn't co-exist like this. I'll think about a solution after work. I was a bit scared about checking for the pipe in images because the image name might contain that symbol, breaking things in a different way.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setting the stale param to "" might be enough

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This indeed did the trick.

  • I verified that it was broken by replacing one animation by just a subset if the images
  • Changed the code to set an empty string as you suggested. It works fine now
    Example of serialization including reset of other parameter: 1139 0.001 d 333 I w481,w483,w491 IR ;1148 0.001 I w681,w683,w691 rs 6;1059 0.001 d 1000 I IR w2|21|36. You can also see that the new entity 1148 doesn't reset anything as the value remains unchanged.

I pushed the fix: fa3f2ac

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants