Forum

> > CS2D > Scripts > multiple hooks of the same type
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch multiple hooks of the same type

5 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt multiple hooks of the same type

En-Kay
User Off Offline

Zitieren
Greetings, everyone!
So i was messing around with some scripts and found myself using few die hooks in a script. So my question is:
Should i put everything together under one die hook or is it acceptable to use multiple hooks? Does it have any impact on performance? It doesn't need to be just a die hook, it can be spawn hook for example. Is it better to have as few as possible?
Thanks and have a nice day!

alt Re: multiple hooks of the same type

Mami Tomoe
User Off Offline

Zitieren
I'd recommend using one central hook for everything that has a return value, such as the die, say, kill, spawn, ETC. hooks.

But for return-less hooks it's fine to use many (though it might cause confusion) such as the second, minute, ms100, ETC. hooks.

There is a performance gain from using less hooks because you will be calling less functions but it's not enough to impact the game play under normal circumstances.

alt Re: multiple hooks of the same type

Masea
Super User Off Offline

Zitieren
In all honesty, using fewer hooks is a good habit that everyone should have eventually. I'd even go one step further and separate the codes that perform different tasks into various functions and call them within the hook function. So, I would only see a few functions being called by the hook functions at the end of the day. We all know the fact that having fewer hooks increase the performance in theory. I personally use this way just for the sake of the cleanliness of my code.

alt Re: multiple hooks of the same type

En-Kay
User Off Offline

Zitieren
Agreed. Although pc hardware is getting better and better to the point you might not even notice any difference in performance of codes, i do believe it is a good habit to write them as efficiently as possible. When they start getting bigger and bigger and consist of numerous lines of codes, every bit of performance boost helps.

alt Re: multiple hooks of the same type

DC
Admin Off Offline

Zitieren
Zitat
Should i put everything together under one die hook or is it acceptable to use multiple hooks?

It is acceptable to use multiple hooks. Otherwise I would not have allowed to attach multiple functions to the same hook. Using the same hook multiple times within the same self contained script is bad practice though.

Zitat
Does it have any impact on performance?

Yes, using multiple hooks (aka adding multiple functions to the same hook) is slightly slower than just having one single function. Calls between CS2D and Lua (e.g. invoking a Lua function via a CS2D hook) come with a small overhead. So doing more of them is always more expensive than just doing one.
The impact however is most likely NOT noticeable in practice (for most cases)

Zitat
Is it better to have as few as possible?

Because of the above: Yes, try to have as few hooks as possible. It's good practice to aim for best performance. If it leads to horrible code however you should reconsider it.

In general I would say it's best practice to use each hook only once for each "Lua module" you write (for each self contained script). This allows everyone to easily combine multiple different scripts without re-writing any code.
Of course you probably still have to re-write things though if you're working with return values in hook. That can't be fully prevented. In that case it can be a valid solution to merge everything into one hook like user Mami Tomoe suggested. Or you can work with hook priorities in a few cases.
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht