AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Automaton or native to disable forward & OT discussion about detecting ground touch (https://forums.alliedmods.net/showthread.php?t=213600)

Astro 04-16-2013 18:07

Automaton or native to disable forward & OT discussion about detecting ground touch
 
Hello, I came across an interesting question that I'm having a hard time trying to answer. Which of the following snippets contains more efficient code? Is there a better way to enable / disable the hook?

Code:
Function() {     if(something)     {         state PreThink:enabled;     } } public client_PreThink(id) <PreThink:enabled> {     if(something)     {         state PreThink:disabled;         // some code     } } public client_PreThink(id) <PreThink:disabled> {} public client_PreThink(id) <> {}

Code:
new forward_PreThink; Function() {     if(!forward_PreThink && something)     {         forward_PreThink = register_forward(FM_PlayerPreThink, "OnPreThink");     } } public OnPreThink(id) {     if(something)     {         unregister_forward(FM_PlayerPreThink, forward_PreThink);         forward_PreThink = 0;         // some code     } }

Code:
new HamHook:hook_Player_PreThink; public plugin_init() {     hook_Player_PreThink = RegisterHam(Ham_Player_PreThink, "player", "OnPlayer_PreThink"); } Function() {     if(something)     {         EnableHamForward(hook_Player_PreThink);     } } public OnPreThink(id) {     if(something)     {         DisableHamForward(hook_Player_PreThink);         // some code     } }

Backstabnoob 04-16-2013 18:10

Re: Automaton or fakemeta?
 
Automaton will always be more or equally efficient. If you have a good reason to use it (such as yours), do it.

ConnorMcLeod 04-17-2013 02:41

Re: Automaton or fakemeta?
 
It could depend on what you want to do.

Also, pay attention to if(!forward_PreThink & something), should be && ;)

Astro 04-17-2013 16:00

Re: Automaton or fakemeta?
 
Thank you for your answers. :) You're right about the &&, I don't know what happend there. :D
Anyway, in which case is fakemeta way more efficient than the first one? I would like to know whether changing state and calling empty client_PreThink(id) <> {} has any negative impact on performance.

ConnorMcLeod 04-17-2013 16:42

Re: Automaton or fakemeta?
 
I'm not familiar with automatons, just feel better to disable such a forward when you don't need, would use Ham_Player_PreThink.
About pure theory, i don't know if someone can answer you.

Can see http://forums.alliedmods.net/showthread.php?p=958497.

Anyway, what are you hooking PreThink for ?

Astro 04-17-2013 17:09

Re: Automaton or fakemeta?
 
I'm mostly interested in automatons as I don't know much about them and I'd like to learn new stuff, so this was a general example but hey, I've found use for this in one of my plugins. I'm blocking any damage dealt to player before they touch ground after spawn (for example maps where players spawn in air and fall down to start with less than 100hp but I don't want to block damage after then).
  1. Spawn
  2. Disable damage
  3. Enable prethink hook
  4. Check for flags like FL_ONGROUND
  5. Disable prethink hook
  6. Enable damage
Edit: Ad Ham_Player_PreThink, I'm actually using this one instead FM, but the question stands still. I'm not much familiar with them either so I'm trying to solve a question about the actual usefulness of the DisableHamForward() and unregister_forward() functions. I believe that the difference would not be significant but I'm just curious. ;)

ConnorMcLeod 04-17-2013 17:48

Re: Automaton or fakemeta?
 
There are often alternative to PreThink hook.

In the example you gave it is not even an alternative, because PreThink shouldn't be hooked for that.
All you have to do is to hook Ham_TakeDamage and to check if last bit param contains bit DMG_FALL.

So before to worry about what it the best between automaton with prethink and disabling prethink forward, you should first ask yourself if there could be better ways to achieve what you want to do.
You can also ask on forum when you have such a question and you don't find an answer by yourself ;)

Astro 04-17-2013 17:56

Re: Automaton or fakemeta?
 
I'm sorry but I don't really see the way how do it your way. If hooked Ham_TakeDamage and the player had fallen from just a few units after spawn Ham_TakeDamage would not be called and if they had fallen from some high place afterwards the damage would be blocked then which in many cases could mess things up. Say I have three maps: first one with spawnpoints high up in the air, second one with trigger_hurt or whatever is it called with spawnpoints nearly on the ground and third one with none of these. I would like do enable damage right after the player touches ground in every of these maps. I don't want to customize my plugin for each map I add to my server.

Edit: I don't mean to be offensive, I'm sorry if it looks like that, my English is terrible.

guipatinador 04-17-2013 18:01

Re: Automaton or fakemeta?
 
Hook Ham_TakeDamage at new round and disable it at start round.

Astro 04-17-2013 18:11

Re: Automaton or fakemeta?
 
I would need to adjust mp_freezetime for each map to fit the time before players reach the ground which is something I am not looking for because I have team of admins that change the maplist quite often but are not allowed to change server CVars. I could even use a set_task for that, I've already considered these options but it's unbearably time-consuming to maintain the individual settings for each map…

Edit: I've added Ham version to the first post. I'm interested in the answer to the detecting player touch ground "event", but also in the pure theory, but I'm not sure whether joaquimandrade reads Scripting Help. :D
Edit2: Following are not solutions to my problem:
  1. Hooking Ham_TakeDamage, disabling and returning HAM_SUPERCEDE after first hit // because in some cases player gets more than one hit before reaching the ground
  2. Hooking Ham_TakeDamage, disabling and returning HAM_SUPERCEDE after DMG_FALL // because in some cases player doesn't fall but gets damage differently
  3. Using set_task or any equivalent // because there would have to be per-map configs which would take much time to test, setup and maintain (as gravity and therefore time to fall can differ)
  4. (Can't remember more, but I've tried more)


All times are GMT -4. The time now is 10:45.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.