Forum

> > CS2D > Scripts > CS2D Values & Spritesheet Perfomance
Forums overviewCS2D overview Scripts overviewLog in to reply

English CS2D Values & Spritesheet Perfomance

3 replies
To the start Previous 1 Next To the start

old CS2D Values & Spritesheet Perfomance

Bowlinghead
User Off Offline

Quote
Hello usS,
I have a few questions regarding the usage of LUA commands.

∗ Caching solutions:
It is possible to get the image x& y position from this command cs2d lua cmd imageparam and the player list from cs2d lua cmd player .
People discussed here about caching information. When is it a good idea and when is it a bad idea?

Here are 2 code examples that demonstrate what I mean:
Warning: They are missing some details, like stopping the burning effect when a player dies for example.
Burn Effect Uncached >
Burn Effect Cached >
When should I cache information and what information should I cache?
I can safely say that no additional scripts will be used. So the playerhp will always be accurate because I will always use
sethealth(...)
instead of
parse("sethealth ...")
. I can assure that there are no other hit hooks that return 1 as well.
I ask specifically for timed hooks like always and ms100.

But I could imagine unlimited use-cases regarding caching information (about image positions, rotation in a custom projectile system for example)

∗ Spritesheets:
It is possible to use parts of a spritesheet image.
I have read about them in OpenGL - you should always use them so that the GPU only loads in all graphics once into VRAM (and then the GPU is really fast).
Is that also true for CS2D, when I want to show multiple different images like in bottom of the screen here:
Is the file transfer download speed faster because there are less files in the servertransfer.lst?
Is it a good habit to always use spritesheet images?

Thanks in advance

old Re: CS2D Values & Spritesheet Perfomance

Mami Tomoe
User Off Offline

Quote
Regarding caching, I believe that you shouldn't optimise something unless you really have to.

I can only see big servers ever having to consider this as an option, mainly because of the pros vs cons of the concept.

Cons:
* Takes a long time to implement.
* Might worsen the performance and thus be a fail.
* Requires more hooks, more functions and overall makes the code more complex.
* Will most likely cause problems in the logic of your script that you'd spend months chasing down to eradicate.

Pros:
* A (potential) slight improvement in server tick-rate.

Due to the above, I would consider this concept to be a band aid solution or a proof of concept more than something you would do in production.
At least to something that changes periodically (like player stats that change in many wonderful and unexpected ways).
Also, by the time you actually need to cache player stats to make your server playable, one could say that you're looking for the problem in the wrong place.

So when should you cache information like this?
1. If you have literally no other choice and are willing to go down that rabbit hole.
2. If you just want to experiment and prove me wrong.
3. If you just want to learn more about Lua by taking on difficult and confusing challenges.

Regarding your sprite sheets question, it is quite unclear what your intentions are.
It is not possible to use a part of a frame in a sprite sheet, you have a sheet made of sprites and each sprite has to be the same size as any other sprite on the sheet.
That's how CS2D sprite sheets have and always will work.
If you want to show more than 1 sprite at a time using the same image object, then you can't do that.
And you shouldn't! Because that would make life quite very complicated for managing the images.
CS2D is not that flexible and that's what makes it so fun.
Work around it by either making one whole image for your HUD (which is what I assume you wanted to display) or multiple images on the screen if it has to be dynamic.

Your last question is quite confusing, the amount of files in the transfer list doesn't directly indicate the length of time it would take to download them as there are quite a lot of things to take into consideration.
Some of them are:
1. Player base download speed.
2. Server base upload speed.
3. Server upload speed limit (server config).
4. Player upload speed limit (player config).
5. Size of each file.
6. Player is using HDD/SSD? And their respective speed.
7. Server is using HDD/SSD? And their respective speed.
8. Files skipped by player config.
9. Missing files on server side.
10. Files the player already has.
And so, so much more...

So to say that less files in transfer list equals to a shorter download time is a bold and unclear claim.
Just make sure that no file exceeds 250KB (default max download size for new clients), and that the total size of files does not exceed 2MB.
This should give a mostly comfortable experience for new joiners.
Plus I would add that having the smaller files higher on the list could make the players feel like the process is a lot faster than it is and once they reach the bigger files it's a bit late to back down, some mind tricks if you're desperate, I'm good with those.

"Is it a good idea to always use sprite sheets?"
Tell me, is it a good idea to always drive a car? To always take the stairs? To always charge your phone before bedtime?
Okay, maybe yes for that last one, but the point is that you should only do things when they're appropriate and make logical sense.
Why would you use a sprite sheet to display single image that will never change?
Sprite sheets are made for animations or images that stay in the same place but change periodically, so it's more efficient to use a sprite sheet as opposed to freeing and creating them again.

old Re: CS2D Values & Spritesheet Perfomance

MikuAuahDark
User Off Offline

Quote
Without reading your whole code, the general rule-of-thumb is to minimize CS2D-related Lua API calls and hooks. Looking how simple the uncached version is, it's better to go with uncached version.

As for the spritesheet, without knowing how CS2D renders the image, it's safe to assume spritesheet improves the performance slightly. user DC have to confirm this one though. Also spritesheet improves file transfer slightly because less file to be transferred.

old Re: CS2D Values & Spritesheet Perfomance

DC
Admin Off Offline

Quote
- Caching -
I agree with the others here. Don't cache unless there is a good reason to do so. When you consider using caching you should first answer this question:
• Does your script cause performance issues?
If yes, go on with this thought. If no, you can probably get away with a simple solution without caching - unless you are a perfectionist and want to get the best possible performance.

The next questions you should ask yourself are:

• 1) what do you want to cache (how much is it / how complex is the data)
If your data is complex/huge it might be too difficult/cause much overhead to store/access it.

• 2) how often do you need to use this data
If you don't use the data (nearly) every frame (e.g. cs2d lua hook always or cs2d lua hook move or cs2d lua hook attack) it's PROBABLY not worth thinking about caching.
Also if you just need to use the same data often inside a loop it's often much easier to just cache it once in a local variable before starting the loop.

• 3) how often does the data change
If the data changes super frequently, e.g. player positions, it's most likely not worth caching it. Especially if you would introduce new hooks and frequent function calls just to cache the data.

These are just general rules of thumb. Of course there can be extreme cases. Like you mentioned: Having a lot of dynamic Lua images. If you often do a check against all their positions it might make a lot of sense to cache their positions even if you don't do these checks that often. It all comes down to CS2D Lua API calls / frame. If you can significantly reduce them with caching it might be worth it. Also keep in mind that most Lua CS2D API calls are NOT horribly slow. They are just a bit slower than doing everything in Lua directly.

In your example, you should definitely go with the uncached version. It's much simpler and less error prone. cs2d lua hook ms100 is called just 10 times per second. That's nothing. Caching won't have a significant impact here. Clearly not worth introducing complexity here (complexity in code is always bad).

- Sprite Sheets -
I actually don't know if BlitzMax (CS2D's "engine") does proper draw call batching. However, the effect of this would only become apparent anyway if you were to render a lot of images. Therfefore in your example (3 images) it doesn't matter at all. It should affect file download and handling speeds a bit because handling few large files is more efficient than handling many small ones. So yes, even without better rendering performance, using sprite sheets is good practice. But: If it is too painful using sheets I wouldn't bother doing it. Impact shouldn't be huge unless we're talking about A LOT of files.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview