I've read somewhere a tutorial somewhere in here that the set_task function should try to be avoided if possible. So I've taken Gohan as an example here. I've modified his loop function to use a different method that doesn't require the set_task function. Basically, my question is: Is this method better than the set_task method? By better I mean, more efficient and requires less memory for the server. They basically do the same thing in game, but still.. I want to know so I can help better my server.
Code:
// GOHAN! - from Dragon Ball Z, Goku and Chi Chi's first son.
/* CVARS - copy and paste to shconfig.cfg
//Gohan
gohan_level 10
gohan_health 150 //default 150
gohan_gravity 0.40 //default 0.40 = low gravity
gohan_speed 800 //How fast he is with all weapons
gohan_healpoints 10 //The # of HP healed per second
gohan_healmax 400 //Max # HP gohan can heal to
gohan_healcooldown 1 //Gohan heals x number of hp per this number in seconds
*/
/*
* v1.2 - vittu - 6/19/05
* - Minor code clean up.
*
* v1.1 - vittu - 3/13/05
* - recoded from ArtofDrowning07 cleaned up code
* - new cvar from ArtofDrowning07, gohan_healmax, you
* can choose how much goten will heal to now
*/
#include <amxmod>
#include <superheromod>
// GLOBAL VARIABLES
new gHeroName[]="Gohan"
new bool:ghasGohanPowers[SH_MAXSLOTS+1]
new gHealPoints, gHealAmount
//----------------------------------------------------------------------------------------------
public plugin_init()
{
// Plugin Info
register_plugin("SUPERHERO Gohan", "1.2", "sharky")
// DO NOT EDIT THIS FILE TO CHANGE CVARS, USE THE SHCONFIG.CFG
register_cvar("gohan_level", "10")
register_cvar("gohan_health", "150")
register_cvar("gohan_gravity", "0.40")
register_cvar("gohan_speed", "800")
register_cvar("gohan_healpoints", "10")
register_cvar("gohan_healmax", "400")
register_cvar("gohan_healcooldown", "1")
// FIRE THE EVENT TO CREATE THIS SUPERHERO!
shCreateHero(gHeroName, "Super Power-Up", "Start with more HP, gain even more each second. Also, has Ultra Speed and Low Gravity!", false, "gohan_level")
// REGISTER EVENTS THIS HERO WILL RESPOND TO! (AND SERVER COMMANDS)
// INIT
register_srvcmd("gohan_init", "gohan_init")
shRegHeroInit(gHeroName, "gohan_init")
// Instead of the original heal loop that uses set_task
// we use FM_PlayerPreThink
register_forward(FM_PlayerPreThink, "gohan_think")
// Let Server know about Gohan's Variables
shSetMaxHealth(gHeroName, "gohan_health")
shSetMinGravity(gHeroName, "gohan_gravity")
shSetMaxSpeed(gHeroName, "gohan_speed", "[0]")
//Set Varibles when plugin loads
gHealPoints = get_cvar_num("gohan_healpoints")
gHealAmount = get_cvar_num("gohan_healmax")
}
//----------------------------------------------------------------------------------------------
public gohan_init()
{
// First Argument is an id
new temp[6]
read_argv(1,temp,5)
new id = str_to_num(temp)
// 2nd Argument is 0 or 1 depending on whether the id has the hero
read_argv(2,temp,5)
new hasPowers = str_to_num(temp)
//This gets run if they had the power but don't anymore
if ( !hasPowers && ghasGohanPowers[id] && is_user_alive(id) ) {
shRemHealthPower(id)
shRemGravityPower(id)
shRemSpeedPower(id)
}
//Sets this variable to the current status
ghasGohanPowers[id] = (hasPowers != 0)
}
//----------------------------------------------------------------------------------------------
//Instead of using set_task
public sh_client_spawn(id)
{
if ( ghasGohanPowers[id] ) {
gPlayerInCooldown[id] = false
}
}
public gohan_think(id)
{
if ( !shModActive() || !hasRoundStarted() ) return
if ( ghasGohanPowers[id] && is_user_alive(id) ) {
new Float:healcooldown = get_cvar_float("gohan_healcooldown")
if (!gPlayerInCooldown[id])
{
shAddHPs(id, gHealPoints, gHealAmount)
if ( healcooldown > 0.0 ) sh_set_cooldown(id, healcooldown)
}
}
}
//----------------------------------------------------------------------------------------------
__________________