|
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
|

01-24-2020
, 11:49
Re: Optimize Code
|
#8
|
Quote:
Originally Posted by Napoleon_be
Untested. Could probably be done more efficient using Ham_Killed etc.
PHP Code:
/* Plugin generated by AMXX-Studio */
#include <amxmodx> #include <csstats> #include <fakemeta_util>
#define PLUGIN "ShowSpecInfo" #define VERSION "1.0" #define AUTHOR "NapoleoN#"
#define TASKID 546873
public plugin_init() { register_plugin(PLUGIN, VERSION, AUTHOR); }
public client_putinserver(id) { set_task(1.0, "ShowSpecInfo", id + TASKID, _, _, "b"); }
public client_disconnect(id) { if(task_exists(id + TASKID)) remove_task(id + TASKID) }
public ShowSpecInfo(id) { id -= TASKID if(!is_user_alive(id)) { new iPlayers[32], iNum, iSpecPlayer, szName[33], iRank, iMaxRank, iStats[8], iBodyHits[8]; get_players(iPlayers, iNum, "ch"); iMaxRank = get_statsnum(); for(new i; i < iNum; i++) { iSpecPlayer = pev(iPlayers[i], pev_iuser2); if(is_user_alive(iSpecPlayer)) { get_user_name(iSpecPlayer, szName, charsmax(szName)); iRank = get_user_stats(iSpecPlayer, iStats, iBodyHits); } } set_hudmessage(180, 180, 180, -1.0, 0.0, 0, 6.0, 1.1, 0.0,0.0, -1); show_hudmessage(id, "Spectating [%s] | [Rank: %i / %i]", szName, iRank, iMaxRank); } }
|
Why is that get_players loop in there? iRank and szName will contain the values for the last player that's being spectated, not the one the current player(with id "id") is currently spectating. There's no need to loop.
This is enough(basically what op had before):
PHP Code:
iSpecPlayer = pev(id, pev_iuser2); get_user_name(iSpecPlayer, szName, charsmax(szName)); iRank = get_user_stats(iSpecPlayer, iStats, iBodyHits);
set_hudmessage(180, 180, 180, -1.0, 0.0, 0, 6.0, 1.1, 0.0,0.0, -1); show_hudmessage(id, "Spectating [%s] | [Rank: %i / %i]", szName, iRank, iMaxRank);
The only thing that's worth "optimizing" is to create only one task in plugin_init instead of creating a task for each player. Then inside the global task you would use get_players as you tried to, get all dead players(use flag b, don't check with is_user_alive), retrieve iuser2 and show the hud.
__________________
Last edited by HamletEagle; 01-24-2020 at 11:51.
|
|