text
stringlengths
0
692
public client_connect(id)
{
g_iSpeakFlags[id] = SPEAK_NORMAL;
}
public fwd_FM_Voice_SetClientListening(receiver, sender, bool:bListen)
{
if(!is_user_connected(receiver) || !is_user_connected(sender))
{
return FMRES_IGNORED;
}
if(g_iSpeakFlags[sender] == SPEAK_NORMAL && g_iSpeakFlags[receiver] != SPEAK_LISTENALL)
{
return FMRES_IGNORED;
}
new iSpeakValue = 0;
if(g_iSpeakFlags[sender] == SPEAK_ALL
|| g_iSpeakFlags[receiver] == SPEAK_LISTENALL
|| g_iSpeakFlags[sender] == SPEAK_TEAM && get_pdata_int(sender, 114) == get_pdata_int(receiver, 114))
{
iSpeakValue = 1;
}
engfunc(EngFunc_SetClientListening, receiver, sender, iSpeakValue);
return FMRES_SUPERCEDE;
}
set_task can sometimes be an extremely useful function, but other times it is really overkill. What few people know is that it is a very expensive operation, the CPU has to literally check every cycle if it is time for the set_task to be executed. Usually, there are ways around a set_task.
set_task also has the restriction of being unable to be called more than every 0.1 seconds. This means that if you pass 0.01 in parameter 1, it will be clamped at 0.1.
But what can we do to avoid it? Sometimes you really have to use a set_task, such as here:
Code:
#include <amxmodx>
#include <amxmisc>
#include <engine>
public plugin_init()
{
register_plugin("ROFL","1.0","Hawk552")
register_event("ResetHUD","EventResetHUD","be")
}
public EventResetHUD(id)
set_task(1.0,"DelayedResetHUD",id)
public DelayedResetHUD(id)
DispatchSpawn(id)
But why? There is no real way to escape delaying the function once without doing something more CPU intensive or unreliable. We could create an ent and make it think in 1 second (which will be covered later), but that not only adds an unnecissary spawn for such a small thing, it also is more unreliable (i.e. the server could crash due to too many ents, or it could be failed to be spawned and the function simply has to end there).
But there are other times where escaping a set_task is not only easier, but less resource consuming.
One of the commonly known ways of doing this is the forward client_PreThink / FM_PlayerPreThink. This forward is called every frame on a client before rendering physics. In this forward, you would do things that generally must be constantly updated, like velocity or changing rendering.
Here is an example:
Code:
#include <amxmodx>
#include <amxmisc>
#include <engine>
public plugin_init()
register_plugin("ROFL","1.0","Hawk552")
public client_PreThink(id)
{
static Float:Velocity[3]
entity_get_vector(id,EV_VEC_velocity,Velocity)
Velocity[2] = -floatabs(Velocity[2])
entity_set_vector(id,EV_VEC_velocity,Velocity)
}
Why is a set_task unsuitable here? For two reasons: 1) the half-life engine will allow you to have timers anyway (to a varying degree), and 2) it will look very glitchy since if the client has 60 FPS, the server is only checking every 10 frames if their velocity is what it should be.
One of the less practiced ways of avoiding set_tasks is to spawn an entity, set its nextthink to whatever you want, and register_think/FM_Think it. But when is this necessary? Pretty much whenever you use the "b" flag in set_task, it's better to use this method. It's also preferred when you have already spawned an entity (such as an NPC) and want to make it say, disappear, in x seconds.
Here is an example:
Engine:
Code:
#include <amxmodx>
#include <amxmisc>
#include <engine>
new g_Classname[] = "roflmao"
public plugin_init()
{
register_plugin("ROFL","1.0","Hawk552")
new Ent = create_entity("info_target")
entity_set_string(Ent,EV_SZ_classname,g_Classname)
entity_set_float(Ent,EV_FL_nextthink,20.0)