English When does CS2D free images?

10 replies
Goto Page
To the start Previous 1 Next To the start
14.04.21 12:27:49 am
Up
Mami Tomoe
User
Offline Off
I know that images don't get freed on cs2d lua hook endround and that they get freed on cs2d lua hook startround.
Does that mean the images still technically "exist" on cs2d lua hook startround_prespawn?

If so, is that consistent? Can I free the images on cs2d lua hook startround_prespawn? Or is it better to let the game handle that and to just set the variable to
nil
?

What is the order of execution?
When do images get automatically freed by CS2D?
Why is there no cs2d lua hook post_endround (once the round completely ended)?
fish
14.04.21 12:38:20 am
Up
SQ
Moderator
Offline Off
It is better if you let CS2D free it whenever it can.
This way you are avoiding additional network traffic.
14.04.21 12:42:44 am
Up
Mami Tomoe
User
Offline Off
I mean, of course!
But I just noticed that one of my scripts frees images on cs2d lua hook startround_prespawn, and I haven't noticed any issues.

I've changes it now to only free the image if the function wasn't called on a round start, I just wanted to know if I missed anything or if this really does work just fine either way.
fish
14.04.21 12:49:33 am
Up
DC
Admin
Online On
Yes, all dynamic objects, including NPCs, buildings and images, are removed when a new round starts.

No, images do NOT exist anymore in cs2d lua hook startround_prespawn and therefore freeing does not make any sense. It doesn't do anything at all. It would not even cause extra taffic because CS2D would silently fail to find the images you want to remove. It's cleaner to simply not try to free images at that point but it doesn't cause any problem either. Only setting the variables to nil is better of course.

Execution order when a new round starts:
• auto team balance
• adjust scores (depending on end of previous round)
• free all dynamic objects
• reset the map (clear items, reset entities, clear particles, run trigger start, spawn items/npcs/buildings)
• run cs2d lua hook startround_prespawn
• clear old hostages & spawn new hostages
• spawn players (and run cs2d lua hook spawn for each right after spawning)
• run cs2d lua hook startround
• reset/start bot AI
• optionally trigger an automatic map change if conditions are met

And no, that order isn't perfect. It just grew like that and it worked so that's why there is that order.

I guess there's no "post_endround" or something like that because there was no high demand for that yet. I would have no idea what you want to use that for. Everything what happens after cs2d lua hook endround (gameplay wise) should be ignored because it's basically just an artificial delay so people can see how the round ended.
edited 1×, last 14.04.21 12:56:18 am
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
14.04.21 12:55:02 am
Up
Mami Tomoe
User
Offline Off
Thank you!
You should document that order somewhere on CS2D.com.
Perhaps on the cheat sheet!

I've also noticed bad usage of image IDs can lead to game breaking bugs, like being able to move a Turret's image but its physical body stays behind, similar stuff can happen to NPCs as well!

Of course all of that won't happen unless someone messed up the image handling...
fish
14.04.21 12:58:36 am
Up
DC
Admin
Online On
Yes, I'm aware of this. Since all these objects share the same basic data type the image commands can be used to also modify npcs and buildings - leading to weird and possibly game breaking results. It would be quite easy to block that by checking the type but I simply never added such a check.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
16.04.21 11:07:02 am
Up
Mami Tomoe
User
Offline Off
@user DC: Actually, I've had quite a few issues when trying to create images on the cs2d lua hook startround_prespawn hook.

The images won't show up for some reason, perhaps the hook gets called before the images get freed? Do you mind verifying?
For now I'll stick to cs2d lua hook startround.

EDIT:
I think that actually, players won't receive a packet about the image being set when using that hook, the image is there when I reconnect, this is really odd.
fish
16.04.21 08:29:59 pm
Up
DC
Admin
Online On
cs2d lua hook startround_prespawn is in general a horrible choice for spawning/changing ANY kind of stuff because most of the actual spawning done by the game happens afterwards. That's why there is that big red text in the description of the hook:
Quote:
This hook can lead to problems with certain commands due to its early execution (especially when playing online). This is because many things will actually be initialized by the game AFTER calling this hook. Try to execute problematic commands later (in another hook or delayed with a timer) in case they do not work properly. Especially commands which change players/hostages/NPCs/items/entities might be affected.


So yes, please use cs2d lua hook startround for creating/changing things. It's much safer and it makes no visible difference because all 3 spawn hooks are executed at the same frame / mainloop iteration. Actually you should always prefer cs2d lua hook startround. cs2d lua hook startround_prespawn should only be used for things which can't be done in cs2d lua hook startround. You will probably save yourself a lot of trouble with that little rule of thumb.

You are also totally right that this is related to networking and how stuff is synchronized and sent etc. I don't want to investigate this but I assume that clients receive and process your prespawn image spawn messages BEFORE they receive the message which starts the new round. Therefore the images are probably spawned but directly destroyed again due to the round restart on client side. So basically the order of execution is a bit different for clients. This is a special case because a new round / spawning triggers 3 different hooks but it's actually just one network message.

I never thought that much about this problem before but now it's very clear to me. I should probably update the documentation a bit
edited 4×, last 16.04.21 08:48:25 pm
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
17.04.21 10:22:38 pm
Up
Mami Tomoe
User
Offline Off
@user DC: I decided to only use cs2d lua hook startround_prespawn hook for things that relate to Lua only.

Anything that requires calling a CS2D function or sets anything in CS2D, will use the cs2d lua hook startround hook.

That seems like a better approach.
I used to think that the cs2d lua hook startround_prespawn hook was made for initialising everything, before the player spawn, but apparently it's even more strict than that and it should only be used for pure Lua execution that does not depend on CS2D (such as resetting variables, initialising tables, ETC.).

Thank you!
fish
18.04.21 04:39:56 pm
Up
Pagyra
User
Offline Off
This is probably the reason why network errors occur in my thread cs2d Template Mod.
18.04.21 05:12:49 pm
Up
Gaios
Security Supporter
Offline Off
@user Pagyra: You better work with network packets instead of CS2D API.
To the start Previous 1 Next To the start