Forum

> > CS2D > Scripts > Spritesheets ? How to create an animation ?
Forums overviewCS2D overview Scripts overviewLog in to reply

English Spritesheets ? How to create an animation ?

19 replies
To the start Previous 1 Next To the start

old Spritesheets ? How to create an animation ?

ign_ot
User Off Offline

Quote
Please tell me how "Commands/Lua" works - in particular
tween_animate
&
image:path <spritesheet:PATH:FRAMEW:FRAMEH[:MODE]>
&
imageparam:framecount

If I understand correctly, then if I add only 1 image with 10 frames of animation to the game, it will cause fewer delays and additional problems than if I added 10 images with 1 frame.

I mean:
1 spritesheet * max number of players = 32 images

10 sprites * max number of players = 320 images

So please explain to me how to add images correctly and activate their animation using the example of several animation options. I want to create additional monsters for the cooperative survival mode (they will animate to move, attack, die)
Unfortunately, I have not found a single topic/script with a ready-made solution to my question, and the help lines are very limited and incomprehensible. And even tibia / minecraft/... others do not use animations and only create/rotate/move images.

old Re: Spritesheets ? How to create an animation ?

Gaios
Reviewer Off Offline

Quote
The TPS of a CS2D server is typically slowed down by excessive calls to the CS2D API through dedicated Lua functions. Using a spritesheet to create an image requires only one call. You can then utilize cs2d lua cmd tween_animate to ensure that the animation of the image loops indefinitely based on the settings defined by this function, e.g., Loop Forward at a frequency of 0.3s per frame.

This works in a way where the frames will continue to change automatically without your further interaction. However, if you want to control or modify it manually, you can use functions like cs2d lua cmd tween_frame or cs2d lua cmd imageframe to set a specific frame as needed. Keep in mind, though, that every API function call introduces a slight delay to the server's performance. While it would require a large number of calls within a single tick to noticeably impact the server, poorly optimized mods like Tibia (CS2D Edition) can already make such issues apparent.

That said, using a spritesheet is far more efficient than creating each image individually. It minimizes API calls and improves overall server performance significantly.

In summary:
Creating an image as a spritesheet and using tween_animate involves only 2 API calls: one to create the image and one to start the animation. Once these calls are completed, the server will not experience any additional load from the animation, as it runs automatically without further interaction.

~Translated by ChatGPT

old Thank you

ign_ot
User Off Offline

Quote
Thanks, can I ask you to give me some code samples - as I know you have done something similar with animations in Metin 2d

old Re: Spritesheets ? How to create an animation ?

Gaios
Reviewer Off Offline

Quote
This is an old script (below) I wrote a long time ago. However, it won't work as-is without the rest of the project files (which I don't want to share since the implementation is poorly done anyway). Still, this script provides a basic overview of how such a system works.

A key optimization you can apply here is caching the positions of images instead of querying their positions from the API on every iteration. You can achieve this by leveraging the timing consistency between the loop and the tween functions. For example, if mobs are iterated every 0.1 seconds, and the movement time (cs2d lua cmd tween_move) is also 0.1 seconds, you can directly set the position to the target position without repeatedly fetching it through the API. This approach reduces the server's load and ensures smoother performance. Essentially, instead of relying on the API to tell you where the image currently is, you predict and "cache" the destination position because you already know where the tween will take it in the given time interval.

Additionally, the cs2d lua cmd imagehitzone function can also be optimized. Instead of keeping it active for all mobs at all times, you can set it only for mobs that are currently visible. When a mob moves out of the visible area, you can release the memory associated with the hitzone by setting its parameter to
0
. This approach further reduces resource usage, especially in scenarios where there are many mobs or objects in the game.
imagehitzone has written
Defining a big number of hit zones can have a bad impact on the game performance (when shooting)!

Mob Engine >

Btw.
If you're interested, you’re welcome to join a project focused on building a modern framework for CS2D. The goal is to enhance efficiency through caching and object-oriented principles. The framework is being written in TypeScript, which can easily be transpiled to Lua 5.1 or LuaJIT.

This is an exciting opportunity to work on something that will significantly simplify and improve mod creation for CS2D. If you'd like to contribute or learn more, feel free to contact me on Discord. Your input could play a key role in shaping this project!

The CS2D community is pretty much dead, and there aren’t many players left. Still, I really like the idea of creating a CS2D server where players fight custom mobs together in a co-op mode.

It’s a fun idea because it doesn’t need many players to work, unlike other game modes. Even with just a few people, the gameplay could feel like a busy server with 60 players. This could make the game more exciting and bring something new to the small community.

~Translated by ChatGPT

old Re: Spritesheets ? How to create an animation ?

Gaios
Reviewer Off Offline

Quote
@user ign_ot: Don't try to implement Custom Weapons as it won't work good as excepted. Better to replace GFX and SFX of some weapons and pretend it's another. Also you can modify firerate of them.
edited 1×, last 05.12.24 01:16:32 pm

old Re: Spritesheets ? How to create an animation ?

Mora
User Off Offline

Quote
You also rather to imagealpha() and imagepos() all your images of mobs which will be spawned again on the same location and turn them into queue mode. And your spawn function must check for available mobs in the queue for this spawn area.

Images is stored into ur cs2d memory still after freeimage. There is not much sense to create new image after killing mob if you will spawn that mob again later.

old Re: Spritesheets ? How to create an animation ?

Gaios
Reviewer Off Offline

Quote
This approach isn't really necessary anymore. It could be useful if images were being created very frequently, like for custom bullets. However, CS2D has significantly improved its image management system since some time ago.

It's actually better to completely remove images using cs2d lua cmd freeimage (but keep the mob instance) and create them only when needed (e.g., when the player can see them). This is because when a new player joins the server, they have to download all existing content. For example, if 300 mobs were already created (even if not all are active), the player would still need to load them all.

So managing images dynamically, based on visibility or necessity, is a better approach now.

old Heeheehee

ign_ot
User Off Offline

Quote
I think it makes sense to turn this discussion into a collection of tips or even blueprints ("You have to do it this way!)
- To get performance,
- To achieve lag-free performance,
- To achieve server stability
- ... .

Unfortunately I didn't get any answers from Pagyra, judging by his work (especially mods) I think he's one of the few who tried to turn CS2D into any other genres/games.
As I understand you both have worked closely with him before? Can you get in contact with him?
His mods QA2D (source Lua code unavailable) and BattleinAir (we modified this Lua code and name a bit ) are quite popular in China, behind 金盾工程, in networked internet bars and 网吧, so we would like to chat with him.
edited 1×, last 08.12.24 01:56:45 am

old Re: Spritesheets ? How to create an animation ?

Mora
User Off Offline

Quote
I will try to reach him in telegram/whatsapp/Viber later when I'm free. If I'm succeed I send him link to current post.
/edit: I found him but don't know if he must have time for You, just have to wait now, I sent link to ur post

Gaios: images still stored in memory after freeimage() the only way is to restart dedicated/cs2d game
edited 1×, last 08.12.24 07:00:00 am

old Re: Spritesheets ? How to create an animation ?

Mora
User Off Offline

Quote
Well, he has no time atm at all cuz of job. There is no known time till he answer you something but in case if u just wanna source code of something - there is no way(and I support him, nobody would like to give a thing that you worked alone just for that free).

old Re: Spritesheets ? How to create an animation ?

Gaios
Reviewer Off Offline

Quote
user Mora has written
@user Gaios: images still stored in memory after freeimage() the only way is to restart dedicated/cs2d game

You don't understand bro.

@user ign_ot: Take this deobfuscated QA2D script: https://pastebin.com/jfy3H3P7
Mostly local variables are still obfuscated but you get the idea... Also I don't like the idea of posting obfuscated mods here (as it was kinda easy to decompile)
edited 1×, last 08.12.24 01:03:58 pm

old Re: Spritesheets ? How to create an animation ?

Mora
User Off Offline

Quote
I'm not enough experienced to develop things and I'm talking about Pagyra work so you rather to create thread where you can ask related stuff and contact those who interested.
So I'm not interested

old Re: Spritesheets ? How to create an animation ?

Gaios
Reviewer Off Offline

Quote
@user ign_ot: I used ChatGPT 4o to deobfuscate the local variable names within the
qa_attack
function, and here are the results (may contain problems):
More >

old Re: Spritesheets ? How to create an animation ?

ign_ot
User Off Offline

Quote
@user Gaios: Thanks for the tip, that's a great way to do it, since I assume you were also interested in analyzing the functions of Pagyra script.
As with the BattleinAir (air combat) mod, in the QA2D mod Pagyra has come up with some interesting implementation ideas to bypass the standard CS2D functions and has written code that is little affected by network latency.

old Re: Spritesheets ? How to create an animation ?

dxvrxv
User Off Offline

Quote
i can decompile QA2D.lua if you want but idk if the author allows it
i just saw Gaios did it with luadec so i think its ok.
this decompiled script is just like original script
pastebin.com/VnuYLhsM
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview