English When does CS2D free images?

18 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)?
Does this script come with insurance?
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.
Does this script come with insurance?
14.04.21 12:49:33 am
Up
DC
Admin
Offline Off
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...
Does this script come with insurance?
14.04.21 12:58:36 am
Up
DC
Admin
Offline Off
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.
Does this script come with insurance?
16.04.21 08:29:59 pm
Up
DC
Admin
Offline Off
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!
Does this script come with insurance?
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.
18.05.21 11:55:50 am
Up
Mami Tomoe
User
Offline Off
user DC has written:
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


I don't know where exactly to ask this, but is there a check to prevent the round from ending during that "initialising" phase?

Because just now, I think that I had a case where the round ended while it was initialising, and perhaps (probably) skipped the start round hooks.

Here's the video: Download

In that case, a player was auto team balanced, and a player died, the round ended twice in a row, before it fully started, under the same MVP and team winner.

That's really odd, I've never seen that happen, I don't even have simple cs2d cmd killplayer calls, so that's really strange.

I suppose the player used cs2d cmd kill and the game did it while the round was initialising?
I really have no idea.
Does this script come with insurance?
18.05.21 04:03:08 pm
Up
mrc
User
Offline Off
I saw the problem there, easy to fix, its lua sided
CS2DBR @ Comunidade Brasileira de CS2D | https://www.cs2dbr.com
18.05.21 05:38:19 pm
Up
Mami Tomoe
User
Offline Off
@user mrc: What do you mean?

I have three hooks:

cs2d lua hook endround - Ends all special events/rounds.
cs2d lua hook startround_prespawn - Resets all variables to default.
cs2d lua hook startround - Starts a new event/round.

Then how come were there two days on the same round?

I suspect some of the hooks may have been skipped.
Does this script come with insurance?
18.05.21 07:11:35 pm
Up
mrc
User
Offline Off
hard to tell without the script
CS2DBR @ Comunidade Brasileira de CS2D | https://www.cs2dbr.com
18.05.21 07:12:48 pm
Up
DC
Admin
Offline Off
Really hard to tell what happened there. Do you have the server logs?

The full sequence I posted (which you just quoted) is basically atomic in the sense that it all happens in one frame/mainloop-iteration and can't be interrupted. At least it shouldn't be possible to interrupt it. Maybe if you do some really weird things in one of the hooks you might be able to break it somehow.

There is a delay of a few seconds between cs2d lua hook endround and starting a new round though (where the outcome is displayed). Normally in that delay no new roundstart should be triggered but maybe it happened anyway for some reason. In that case it's possible that cs2d lua hook endround was triggered multiple times but all the roundstart stuff (the quoted list) was just executed once.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
18.05.21 07:40:44 pm
Up
Mami Tomoe
User
Offline Off
@user DC: I do have the server logs, but I don't recall them containing anything valuable.

Server logs doesn't countain rounds ending / starting messages and there weren't any errors.
Does this script come with insurance?
18.05.21 08:51:27 pm
Up
DC
Admin
Offline Off
Oh okay. I guess I can't help then.
I would need a step by step instruction to reproduce it. And possibly the script as well or a sample script which shows the issue.

Don't get me wrong. I'm not asking for it because I really do NOT want to look into that issue that's just what I would need to do so.

If it doesn't happen often I recommend to just ignore it...
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
18.05.21 09:10:43 pm
Up
Mami Tomoe
User
Offline Off
Perphaps on the next CS2D version, add a boolean check on that initialising check as such:

Code:
1
2
3
IS_STARTING = true
Run all startround initialising...
IS_STARTING = false


And then, when the game is trying to end, due to any reason, just check if
IS_STARTING
is not set to true in order to proceed.

How's that? It should be quite easy to add, and prevent weird things like this from happening, at least I'd hope.

Also, this is quite a big script, reproducing it, is less than likely.
Does this script come with insurance?
To the start Previous 1 Next To the start