Update player statistics in the textdraw
#1

Update player statistics in this way is fine? I understand that I can use 'OnPlayerUpdate' or use a timer in 'OnGameModeInit' but "I BELIEVE" that there is some recommended and best way to do this.

PHP Code:
public OnPlayerSpawn(playerid)
{
    
KillTimer(TIMER[playerid]);
    
TIMER[playerid] = SetTimerEx("UpdateHud"1000true"i"playerid);
    return 
1;
}
////////////////
UpdateHud(playerid)
{
    new 
str[64];
    
format(strsizeof str"$%d"PLAYER_MONEY[playerid]); //MONEY
    
PlayerTextDrawSetString(playeridTD_HUD[playerid][0], str);
    
    
format(strsizeof str"- %d -"PLAYER_LEVEL[playerid]); //LEVEL
    
PlayerTextDrawSetString(playeridTD_HUD[playerid][1], str);
    
    
format(strsizeof str"- %.3f -"PLAYER_EXP[playerid]); //EXP
    
PlayerTextDrawSetString(playeridTD_HUD[playerid][2], str);
    
    
format(strsizeof str"- %s -"PLAYER_CLAN[playerid]); //CLAN
    
PlayerTextDrawSetString(playeridTD_HUD[playerid][3], str);
    return 
1;

Reply
#2

No it is not fine not at all! You only update when something changes! No timers, no OPU!

Furthermore this not okay either.

Code:
    KillTimer(TIMER[playerid]); 
    TIMER[playerid] = SetTimerEx("UpdateHud", 1000, true, "i", playerid);
Why would you kill a timer just to start it again?
Reply
#3

Then how would I do it correctly? This is what I have right now in my game mode.

PHP Code:
public OnPlayerSpawn(playerid)
{
    
ShowHud(playerid)
    return 
1;
}
ShowHud(playerid)
{
    
PlayerTextDrawShow(playeridTD_HUD[playerid][0]);
    
PlayerTextDrawShow(playeridTD_HUD[playerid][1]);
    
PlayerTextDrawShow(playeridTD_HUD[playerid][2]);
    
PlayerTextDrawShow(playeridTD_HUD[playerid][3]);
    
    
KillTimer(TIMER[playerid]);
    
TIMER[playerid] = SetTimerEx("UpdateHud"1000true"i"playerid);
    return 
1;

PHP Code:
UpdateHud(playerid)
{
    if(
PLAYER_EXP[playerid] >= PLAYER_LEVEL[playerid] * 12)
    {
        
PLAYER_LEVEL[playerid] ++;
        
PLAYER_EXP[playerid] = 1;
        
        new 
Query[256];
        
format(Querysizeof Query"UPDATE PLAYERS SET LEVEL = '%d', EXP = '%d' WHERE PLAYER_ID = '%d';"PLAYER_LEVEL[playerid], PLAYER_EXP[playerid], PLAYER_ID[playerid]);
        
db_query(DT_DBQuery);
    }
    new 
str[64];
    
format(strsizeof str"$%d"PLAYER_MONEY[playerid]); //MONEY
    
PlayerTextDrawSetString(playeridTD_HUD[playerid][0], str);
    
    
format(strsizeof str"- %d -"PLAYER_LEVEL[playerid]); //LEVEL
    
PlayerTextDrawSetString(playeridTD_HUD[playerid][1], str);
    
    
format(strsizeof str"- %.3f -"PLAYER_EXP[playerid]); //EXP
    
PlayerTextDrawSetString(playeridTD_HUD[playerid][2], str);
    
    
format(strsizeof str"- %s -"PLAYER_CLAN[playerid]); //CLAN
    
PlayerTextDrawSetString(playeridTD_HUD[playerid][3], str);
    return 
1;

Reply
#4

You go like this, then just call UpdateHud() in the places where you code makes changes. If you have a lot of function calls where this data is changed like SetPlayerMoney() you just need to hook that function or create new ones that performs the task and calls the update. I doubt there is going to many places where level, exp, clan are changed so you just need to make sure to call UpdateHud() whenever a change is made. You can add any sqlite/mysql update queries you have here as well.

Code:
#define         UPDATE_TYPE_MONEY       0
#define         UPDATE_TYPE_LEVEL       1
#define         UPDATE_TYPE_EXP         2
#define         UPDATE_TYPE_CLAN        3

UpdateHud(playerid, type)
{
    new str[64];
	switch(type)
	{
	    case UPDATE_TYPE_MONEY:
	    {
		    format(str, sizeof str, "$%d", PLAYER_MONEY[playerid]); //jn+
		    PlayerTextDrawSetString(playerid, TD_HUD[playerid][0], str);
	    }
	
	    case UPDATE_TYPE_LEVEL:
	    {
		    format(str, sizeof str, "- %d -", PLAYER_LEVEL[playerid]); //LEVEL
		    PlayerTextDrawSetString(playerid, TD_HUD[playerid][1], str);
	    }
	    
	    case UPDATE_TYPE_EXP:
	    {
		    format(str, sizeof str, "- %.3f -", PLAYER_EXP[playerid]); //EXP
		    PlayerTextDrawSetString(playerid, TD_HUD[playerid][2], str);
	    }
	    
	    case UPDATE_TYPE_CLAN:
	    {
		    format(str, sizeof str, "- %s -", PLAYER_CLAN[playerid]); //CLAN
		    PlayerTextDrawSetString(playerid, TD_HUD[playerid][3], str);
	    }
	}
	return 1;
}
Reply
#5

What I'm doing is much better than using 'OPU' or a timer in 'OnGameModeInit'?

By the way, I do not think I have to call 'UpdateHud' every time a change is made, since the timer that is in 'ShowHud' is repeated every second.

EDIT: Instead of using a timer, could you execute the function directly from where the variables change?

Example:

PHP Code:
//OnPlayerDeath
PLAYER_MONEY[killerid] += 50;
PLAYER_EXP[killerid] ++;
UpdateHud(killerid);
//Command give / set money
PLAYER_MONEY[params[0]] += params[1];
UpdateHud(params[0]);
//etc... 
Reply
#6

Bump
Reply
#7

Just use y_timers with 1 second update. No need to worry about CPU and related things.
Reply
#8

Quote:
Originally Posted by SymonClash
View Post
Just use y_timers with 1 second update. No need to worry about CPU and related things.
You have absolutely no idea what you are doing! Just don't respond. Ignore him N3mesiS.


I told you what to do..... forget about timers here! They don't exist! Gone! Forgotten! What is a timer? Get it? Got it? Hope so!

Call UpdateHud() whenever a change is made! Plain simple effective PERIOD .
Reply
#9

Quote:
Originally Posted by Pottus
View Post
You have absolutely no idea what you are doing! Just don't respond. Ignore him N3mesiS.


I told you what to do..... forget about timers here! They don't exist! Gone! Forgotten! What is a timer? Get it? Got it? Hope so!
I think you should get a chamomile and learn some respect to who's older than you.

And no, i'm not talking about Sa-Mp age.

First, using y_timers for updating the stats textdraw i can't see what's the bad thing in that. Are you serious?

"Forget about timers here! They don't exist! Gone!" Seriously? Seriously? I'm at the circus or what?
Reply
#10

Quote:
Originally Posted by Y_Less
View Post
No, I meant do this:

Code:
AddToMoneyStat(playerid, amount)
{
    PLAYER_MONEY[playerid] += amount; 
    UpdateHud(playerid);
}
Then call that function for any update. Don't mutate variables directly.
Well, that would do it when you increase the money to any player. But when you just want to update the level, the name of the clan or the exp as it would? By that I mean the commands of '/ setlevel, / clanname and / giveexp'
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)