SA-MP Forums Archive
Code not working - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Code not working (/showthread.php?tid=559022)



Code not working - Gaurav_Rawat - 20.01.2015

Leave saving aside it isn't showing any increment what could be wrong ?



in OnGameModeInit

pawn Код:
SetTimer("TimeUpdater",1000,true);

pawn Код:
forward TimeUpdater(playerid);
public TimeUpdater(playerid)
{
    if(IsPlayerConnected(playerid))
    {
        PlayerInfo[playerid][Seconds]++;
        if(PlayerInfo[playerid][Seconds] >= 59)
        {
            PlayerInfo[playerid][Seconds]=0;
            PlayerInfo[playerid][Minutes] = PlayerInfo[playerid][Minutes] +1;
        }
        if(PlayerInfo[playerid][Minutes] >= 59)
        {
            PlayerInfo[playerid][Minutes]=0;
            PlayerInfo[playerid][Hours] = PlayerInfo[playerid][Hours] +1;
        }
    }
    return 1;
}
When i try to test the code with command
pawn Код:
CMD:timeplayed(playerid,params[])
{
new String[200];
format(String,sizeof(String),"Time played: Seconds: %d %d Minute %d Hours",PlayerInfo[Seconds],PlayerInfo[Minutes],PlayerInfo[Hours]);
SendClientMessage(playerid,RED,String);
return 1;
}
It shows 0 0 0 only what could be wrong with it ?


Re: Code not working - Schneider - 20.01.2015

You can't just make up values like playerid in a function unless you pass it to it (Using SetTimerEx).

It's better to loop through all the online players:
pawn Код:
forward TimeUpdater();
public TimeUpdater()
{
    for(new i; i<MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
        {
            PlayerInfo[i][Seconds]++;
            if(PlayerInfo[i][Seconds] >= 59)
            {
                PlayerInfo[i][Seconds]=0;
                PlayerInfo[i][Minutes] = PlayerInfo[i][Minutes] +1;
            }
            if(PlayerInfo[i][Minutes] >= 59)
            {
                PlayerInfo[i][Minutes]=0;
                PlayerInfo[i][Hours] = PlayerInfo[i][Hours] +1;
            }
        }
    }
    return 1;
}



Re: Code not working - xVIP3Rx - 20.01.2015

Exactly as Schneider, and I suggest using foreach for faster looping.
pawn Код:
//OnGameModeInit
    SetTimer("TimeUpdater",1000,true);
//anywhere in the script
forward TimeUpdater(playerid);
public TimeUpdater(playerid)
{
    foreach(Player, i)
    //I suggest using foreach, if you don't want you can use this
    //for(new i; i<MAX_PLAYERS; i++)
    {
        PlayerInfo[i][Seconds]++;
       
        if(PlayerInfo[i][Seconds] >= 59)
        {
            PlayerInfo[i][Seconds] = 0;
            PlayerInfo[i][Minutes] ++;
           
            if(PlayerInfo[i][Minutes] >= 59)
            {
                PlayerInfo[i][Minutes] = 0;
                PlayerInfo[i][Hours] = ++;
            }
        }
    }
    return 1;
}



Re: Code not working - Gaurav_Rawat - 20.01.2015

Still it shows 0 only.


Re: Code not working - Schneider - 20.01.2015

You can do it even without a timer. Just get the timestamp when player joins and whenever you need the time you get the current timestamp, subtract the joined timestamp. It will get you the amount of milliseconds the player has played, then you can convert it to hours, minutes and seconds:

pawn Код:
public OnPlayerConnect(playerid)
{
    PlayerInfo[playerid][TimeJoined] = tickcount();
    return 1;
}

CMD:timeplayed(playerid,params[])
{
    new String[32], TimeOnline, Hours, Minutes, Seconds;
    TimeOnline = (tickcount() - PlayerInfo[playerid][TimeJoined])/1000;
    Minutes = TimeOnline/60; Seconds = TimeOnline%60;
    Hours = Minutes/60; Minutes = Minutes%60;
    format(String,sizeof(String),"Time played: %d:%02d:%02d", Hours, Minutes, Seconds);
    SendClientMessage(playerid,RED,String);
    return 1;
}



Re: Code not working - xVIP3Rx - 20.01.2015

pawn Код:
#include <a_samp>
#include <foreach>

public OnGameModeInit()
{
    SetTimer("TimeUpdater",1000,true);
    return 1;
}

forward TimeUpdater(playerid);
public TimeUpdater(playerid)
{
    foreach(Player, i)
    {
        SendClientMessage(i, -1, "increased your seconds by 1");
        PlayerInfo[i][Seconds]++;

        if(PlayerInfo[i][Seconds] >= 59)
        {
            PlayerInfo[i][Seconds] = 0;
            PlayerInfo[i][Minutes] ++;

            if(PlayerInfo[i][Minutes] >= 59)
            {
                PlayerInfo[i][Minutes] = 0;
                PlayerInfo[i][Hours] = ++;
            }
        }
    }
    return 1;
}
Tell me if it says anything every second


Re: Code not working - Gaurav_Rawat - 20.01.2015

The message is being displayed "increased your seconds by 1" every second but still it shows 0 0 0 over /timeplayed :/


Re: Code not working - Schneider - 20.01.2015

Are you sure the player variables are not reset anywhere in the script?


Re: Code not working - xVIP3Rx - 20.01.2015

pawn Код:
#include <a_samp>
#include <foreach>

public OnGameModeInit()
{
    SetTimer("TimeUpdater",1000,true);
    return 1;
}

forward TimeUpdater(playerid);
public TimeUpdater(playerid)
{
    foreach(Player, i)
    {
        SendClientMessage(i, -1, "increased your seconds by 1");
        PlayerInfo[i][Seconds]++;

        if(PlayerInfo[i][Seconds] >= 59)
        {
            PlayerInfo[i][Seconds] = 0;
            PlayerInfo[i][Minutes] ++;

            if(PlayerInfo[i][Minutes] >= 59)
            {
                PlayerInfo[i][Minutes] = 0;
                PlayerInfo[i][Hours] = ++;
            }
        }
    }
    return 1;
}

CMD:timeplayed(playerid,params[])
{
    new String[128];
    format(String, sizeof(String), "Time played: Seconds: %d %d Minute %d Hours", PlayerInfo[playerid][Seconds],PlayerInfo[playerid][Minutes],PlayerInfo[playerid][Hours]);
    SendClientMessage(playerid,RED,String);
    return 1;
}
How are you declaring your enum, it seems you're not adding [playerid] to them, try this code and if it didn't work show me your enum for players info


Re: Code not working - Gaurav_Rawat - 20.01.2015

Edit:

Did some edits and with both of your help its working now thanks rep for both of u