Forum

> > CS2D > Scripts > When does CS2D free images?
Forums overviewCS2D overview Scripts overviewLog in to reply

English When does CS2D free images?

18 replies
To the start Previous 1 Next To the start

old When does CS2D free images?

Mami Tomoe
User Off Offline

Quote
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)?

old Re: When does CS2D free images?

SQ
Moderator Off Offline

Quote
It is better if you let CS2D free it whenever it can.
This way you are avoiding additional network traffic.

old Re: When does CS2D free images?

Mami Tomoe
User Off Offline

Quote
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.

old Re: When does CS2D free images?

DC
Admin Off Offline

Quote
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

old Re: When does CS2D free images?

Mami Tomoe
User Off Offline

Quote
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...

old Re: When does CS2D free images?

DC
Admin Off Offline

Quote
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.

old Re: When does CS2D free images?

Mami Tomoe
User Off Offline

Quote
@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.

old Re: When does CS2D free images?

DC
Admin Off Offline

Quote
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

old Re: When does CS2D free images?

Mami Tomoe
User Off Offline

Quote
@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!

old Re: When does CS2D free images?

Mami Tomoe
User Off Offline

Quote
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.

old Re: When does CS2D free images?

DC
Admin Off Offline

Quote
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.

old Re: When does CS2D free images?

Mami Tomoe
User Off Offline

Quote
@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.

old Re: When does CS2D free images?

DC
Admin Off Offline

Quote
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...

old Re: When does CS2D free images?

Mami Tomoe
User Off Offline

Quote
Perphaps on the next CS2D version, add a boolean check on that initialising check as such:

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.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview