SA-MP Forums Archive
Weird crash, if I add something - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Weird crash, if I add something (/showthread.php?tid=180326)



Weird crash, if I add something - Memoryz - 30.09.2010

Working:
pawn Код:
public DashUpdate(playerid)
{
      new vehid = GetPlayerVehicleID(playerid);
      new estring[7];
      switch(EngineStatus[vehid])
      {
         case 0: estring = "Off";
         case 1: estring = "On";
         default: estring = "Broken";
      }
      new tstring[300];
      format(tstring,sizeof(tstring),"Engine: %s~n~Fuel: %d~n~Speed: %d KM" , estring, Fuel[vehid], GetVehicleSpeed(vehid));
      TextDrawSetString(Dash[playerid], tstring);
      TextDrawHideForPlayer(playerid,Dash[playerid]);
      TextDrawShowForPlayer(playerid,Dash[playerid]);
     return 1;
}
Not Working:
pawn Код:
public DashUpdate(playerid)
{
    new State = GetPlayerState(playerid);
    if(State == PLAYER_STATE_DRIVER && IsPlayerInAnyVehicle(playerid))
    {
      new vehid = GetPlayerVehicleID(playerid);
      new estring[7];
      switch(EngineStatus[vehid])
      {
         case 0: estring = "Off";
         case 1: estring = "On";
         default: estring = "Broken";
      }
      new tstring[300];
      format(tstring,sizeof(tstring),"Engine: %s~n~Fuel: %d~n~Speed: %d KM" , estring, Fuel[vehid], GetVehicleSpeed(vehid));
      TextDrawSetString(Dash[playerid], tstring);
      TextDrawHideForPlayer(playerid,Dash[playerid]);
      TextDrawShowForPlayer(playerid,Dash[playerid]);
    }
    else
    {
       TextDrawHideForPlayer(playerid,Dash[playerid]);
       KillTimer(DashUpdate(playerid));
    }
    return 1;
}
Why is the not-working one causing the server to crash, and how can I fix it. This is really confusing


Re: Weird crash, if I add something - -Sneaky- - 30.09.2010

Try debugging..

pawn Код:
public DashUpdate(playerid)
{
    print("a");

    new State = GetPlayerState(playerid);

    if(State == PLAYER_STATE_DRIVER && IsPlayerInAnyVehicle(playerid))
    {
        print("b");

        new vehid = GetPlayerVehicleID(playerid);
        new estring[7];

        switch(EngineStatus[vehid])
        {
            case 0: estring = "Off";
            case 1: estring = "On";
            default: estring = "Broken";
         }

         print("c");

         new tstring[300];

         format(tstring,sizeof(tstring),"Engine: %s~n~Fuel: %d~n~Speed: %d KM" , estring, Fuel[vehid], GetVehicleSpeed(vehid));

         print("d");

         TextDrawSetString(Dash[playerid], tstring);

         print("e");

         TextDrawHideForPlayer(playerid,Dash[playerid]);
         TextDrawShowForPlayer(playerid,Dash[playerid]);
         
    }
    else
    {
        print("f");

        TextDrawHideForPlayer(playerid,Dash[playerid]);
        KillTimer(DashUpdate(playerid));

        print("g");
    }
    return 1;
}
For example, say the last thing it would print in your server log is "d" then we can assume it's crashing at TextDrawSetString(Dash[playerid], tstring);


Re: Weird crash, if I add something - [XST]O_x - 30.09.2010

I know the problem.

You cannot kill timers if you've only implemented them, you need to assign a variable to the timer first before killing it.

For example:
pawn Код:
new timer_dash_update;

timer_dash_update = SetTimerEx("DashUpdate",time,true/false,"i",playerid);

KillTimer(timer_dash_update);
Sneaky, hats off for the effort but you've missed the main problem ^


Re: Weird crash, if I add something - -Sneaky- - 30.09.2010

Quote:
Originally Posted by [XST]O_x
Посмотреть сообщение
I know the problem.

You cannot kill timers if you've only implemented them, you need to assign a variable to the timer first before killing it.

For example:
pawn Код:
new timer_dash_update;

timer_dash_update = SetTimerEx("DashUpdate",time,true/false,"i",playerid);

KillTimer(timer_dash_update);
Sneaky, hats off for the effort but you've missed the main problem ^
I don't know whether the timer is the problem or not but I'm actually trying to learn this guy something (debugging code), if he would debug he would figure out himself whats wrong, you just gave him the (possible) answer and he learns nothing!


Re: Weird crash, if I add something - [XST]O_x - 30.09.2010

Quote:
Originally Posted by -Sneaky-
Посмотреть сообщение
I don't know whether the timer is the problem or not but I'm actually trying to learn this guy something (debugging code), if he would debug he would figure out himself whats wrong, you just gave him the (possible) answer and he learns nothing!
Well, very kind of you, but I'm also sure that's the real problem as I've encountered it couple of times, so I suggested a quick fix for a common problem, but you're pretty right as debugging almost always helps to find out mistakes.


Re: Weird crash, if I add something - Memoryz - 30.09.2010

Thank-you for the Timer help.

I always debug, and yes I did learn something.

That you cannot kill a timer with variables.


Re: Weird crash, if I add something - -Sneaky- - 30.09.2010

Quote:
Originally Posted by [XST]O_x
Посмотреть сообщение
Well, very kind of you, but I'm also sure that's the real problem as I've encountered it couple of times, so I suggested a quick fix for a common problem, but you're pretty right as debugging almost always helps to find out mistakes.
Yep, if only people would do it more often


Re: Weird crash, if I add something - Memoryz - 01.10.2010

Quote:
Originally Posted by GazaWeSay
Посмотреть сообщение
pawn Код:
public DashUpdate(playerid)
{
    if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
    {
      new vehid = GetPlayerVehicleID(playerid);
      new estring[7];
      switch(EngineStatus[vehid])
      {
         case 0: estring = "Off";
         case 1: estring = "On";
         default: estring = "Broken";
      }
      new tstring[300];
      format(tstring,sizeof(tstring),"Engine: %s~n~Fuel: %d~n~Speed: %d KM" , estring, Fuel[vehid], GetVehicleSpeed(vehid));
      TextDrawSetString(Dash[playerid], tstring);
      TextDrawHideForPlayer(playerid,Dash[playerid]);
      TextDrawShowForPlayer(playerid,Dash[playerid]);
    }
    else
    {
       TextDrawHideForPlayer(playerid,Dash[playerid]);
       KillTimer(DashUpdate[playerid]);
    }
    return 1;
}
If you can read the thread, it says the problem has been fixed.

And the code you posted would not work anyways.