Timer help
#1

Hello, I have currently made a command that heals me over time and until I reach 100 HP.

How could I make the command to last for only 30 seconds?
Here is the code!

Код:
new healer_timer[MAX_PLAYERS];
forward public HealSlowly( playerid );
Код:
if (strcmp("/heal", cmdtext, true, 10) == 0)
    {
      healer_timer[ playerid ] = SetTimerEx("HealSlowly", 2000, 1, "d", playerid);
      ApplyAnimation(playerid,"SMOKING","M_smk_in",4.2,0,0,0,0,0,1);
      SendClientMessage(playerid, 0xFF0000AA, "You are on drugs, health refilling until 100!");
      return 1;
    }
Код:
public HealSlowly( playerid )
{
      new Float:health;
      GetPlayerHealth(playerid, health);
      if( health == 100)
      {
       KillTimer(healer_timer[playerid]);
      }
      else SetPlayerHealth( playerid, health + 3 );
      return 1;
}
Код:
public OnPlayerDisconnect(playerid, reason)
{
    KillTimer(healer_timer[playerid]);
	return 1;
}
Reply
#2

pawn Код:
new healer_timer[MAX_PLAYERS];
new healer_unix[MAX_PLAYERS];
forward HealSlowly( playerid );
pawn Код:
public OnPlayerConnect(playerid)
{
    healer_unix[playerid] = 0;
    return 1;
}
pawn Код:
// Command
    if (strcmp("/heal", cmdtext, true, 5) == 0)
    {
        healer_unix[playerid] = GetTickCount( );
        healer_timer[ playerid ] = SetTimerEx("HealSlowly", 2000, 1, "d", playerid);
        ApplyAnimation(playerid,"SMOKING","M_smk_in",4.2,0,0,0,0,0,1);
        SendClientMessage(playerid, 0xFF0000AA, "You are on drugs, health refilling until 100!");
        return 1;
    }
pawn Код:
public HealSlowly( playerid )
{
    if( healer_unix[playerid] != 0 && (GetTickCount( ) - healer_unix[playerid] ) >= 30 )
    {
        healer_unix[playerid] = 0;
        return KillTimer(healer_timer[playerid]);
    }
    new Float:health;
    GetPlayerHealth(playerid, health);
    if( health == 100) KillTimer(healer_timer[playerid]);
    else SetPlayerHealth( playerid, health + 3 );
    return 1;
}
Reply
#3

Now my HP isn't refilling at all D:, but the code compiles fine.
And after adding a client message I discovered that it lasts only 3 seconds.
Reply
#4

Thats because GetTickCount returns a millesconds value and he only used 30s instead of 30000ms

But I would prefer this version
pawn Код:
//
    if(strcmp("/heal", cmdtext, true) == 0) {
        HealSlowly(playerid, 45); // 45 ticks because 30 seconds * 3 hp / 2 seconds = 45 health
        ApplyAnimation(playerid, "SMOKING", "M_smk_in", 4.2, 0, 0, 0, 0, 0, 1);
        return SendClientMessage(playerid, 0xFF0000AA, "You are on drugs, slowly refilling 45 health until 100!");
    }
pawn Код:
forward HealSlowly(playerid, ticks);
public HealSlowly(playerid, ticks) {
    new
        Float: health
    ;
    if(GetPlayerHealth(playerid, health)) {
        SetPlayerHealth(playerid, ++health);

        if(ticks-- && (health < 100.0)) {
            SetTimerEx("HealSlowly", 666, false, "ii", playerid, ticks);
        }
    }
}
Reply
#5

Yes, it works, thanks a bunch, I really appreciate this, definately earned a REP from me.
1 more question, where should I add a client message saying that the "HEAL" has worn off.
I can't figure it out.
Reply
#6

pawn Код:
//
        if(ticks-- && (health < 100.0)) {
            SetTimerEx("HealSlowly", 666, false, "ii", playerid, ticks);
        } else {
            SendClientMessage(playerid, 0xFF0000AA, "The drug has worn off!");
        }
Reply
#7

Thanks a bunch, kind sir.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)