SA-MP Forums Archive
Reduce - 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: Reduce (/showthread.php?tid=660081)



Reduce - KinderClans - 24.10.2018

Is there a way to reduce this code to less lines?

pawn Код:
if(!strcmp(sub_cmd, "joinmsg"))
    {
        switch (Player[playerid][JoinMsg])
    {
        case 0:
        {
            Player[playerid][JoinMsg] = 1;
            SCM(playerid, COLOR_YELLOW, "* You have enabled join messages.");
        }
        case 1:
        {
            Player[playerid][JoinMsg] = 0;
            SCM(playerid, COLOR_YELLOW, "* You have disabled join messages.");
        }
    }  
    }
Thanks.


Re: Reduce - Undef1ned - 24.10.2018

Why further reduce the code? Not because it's shorter means it's faster.

replaces "if(!strcmp(sub_cmd, "joinmsg"))" with "if(!strcmp(sub_cmd, "joinmsg", true))"


Re: Reduce - Infin1ty - 24.10.2018

Quote:
Originally Posted by Undef1ned
Посмотреть сообщение
Why further reduce the code? Not because it's shorter means it's faster.

replaces "if(!strcmp(sub_cmd, "joinmsg"))" with "if(!strcmp(sub_cmd, "joinmsg", true))"
Код:
replaces "if(!strcmp(sub_cmd, "joinmsg"))" with "if(!strcmp(sub_cmd, "joinmsg", true))"
Why are you advising him to do that? The 'sub command' is specifically 'joinmsg' and not 'JoInMsG' or 'JoinMsg'.


Re: Reduce - KinderClans - 24.10.2018

Exactly.

I wanna reduce it not because i want it faster, but i hate long codes when settings a variable to 0/1...


Re: Reduce - kristo - 24.10.2018

Код:
if(!strcmp(sub_cmd, "joinmsg"))
{
    switch (Player[playerid][JoinMsg])
    {
        case 0:
        {
            Player[playerid][JoinMsg] = 1;
            SCM(playerid, COLOR_YELLOW, "* You have enabled join messages.");
        }
        case 1:
        {
            Player[playerid][JoinMsg] = 0;
            SCM(playerid, COLOR_YELLOW, "* You have disabled join messages.");
        }
    }   
}
16 lines.

Код:
if(!strcmp(sub_cmd, "joinmsg"))
{
    if(Player[playerid][JoinMsg])
    {
        Player[playerid][JoinMsg] = 0;
        SCM(playerid, COLOR_YELLOW, "* You have disabled join messages.");
    }
    else
    {
        Player[playerid][JoinMsg] = 1;
        SCM(playerid, COLOR_YELLOW, "* You have enabled join messages.");
    }
}
13 lines.

Код:
if(!strcmp(sub_cmd, "joinmsg"))
{
    Player[playerid][JoinMsg] = !Player[playerid][JoinMsg];
    if(Player[playerid][JoinMsg])
    {
        SCM(playerid, COLOR_YELLOW, "* You have enabled join messages.");
    }
    else
    {
        SCM(playerid, COLOR_YELLOW, "* You have disabled join messages.");
    }
}
12 lines.

Код:
if(!strcmp(sub_cmd, "joinmsg"))
{
    if((Player[playerid][JoinMsg] = !Player[playerid][JoinMsg]))
    {
        SCM(playerid, COLOR_YELLOW, "* You have enabled join messages.");
    }
    else
    {
        SCM(playerid, COLOR_YELLOW, "* You have disabled join messages.");
    }
}
11 lines.

Код:
if(!strcmp(sub_cmd, "joinmsg"))
{
    SCM(playerid, COLOR_YELLOW, (Player[playerid][JoinMsg] = !Player[playerid][JoinMsg]) ? ("* You have enabled join messages.") : ("* You have disabled join messages."));
}
4 lines.

Код:
if(!strcmp(sub_cmd, "joinmsg")) SCM(playerid, COLOR_YELLOW, (Player[playerid][JoinMsg] = !Player[playerid][JoinMsg]) ? ("* You have enabled join messages.") : ("* You have disabled join messages."));
1 line.

Код:
if(!strcmp(sub_cmd, "joinmsg")){switch (Player[playerid][JoinMsg]){case 0:{Player[playerid][JoinMsg] = 1;SCM(playerid, COLOR_YELLOW, "* You have enabled join messages.");}case 1:{Player[playerid][JoinMsg] = 0;SCM(playerid, COLOR_YELLOW, "* You have disabled join messages.");}}}
But this would also be one line. The right balance between legibility and compactness is somewhere in the middle.


Re: Reduce - KinderClans - 24.10.2018

Lmao that's what i was looking for. Thanks.


Re: Reduce - KinderClans - 24.10.2018

Quote:
Originally Posted by ******
Посмотреть сообщение
Код:
Player[playerid][JoinMsg] ^= true;
^=?

Never heard of this. What does?


Re: Reduce - GangstaSunny. - 24.10.2018

its not in it.
i know it has been told somewhere but its not in it.

But since he may had defined it as an integer, I guess ^= will give the ability to set it like a bool?


Re: Reduce - Y_Less - 24.10.2018

Quote:
Originally Posted by GangstaSunny.
Посмотреть сообщение
its not in it.
i know it has been told somewhere but its not in it.
It is, took me 10 seconds to confirm that - 9 of those just for downloading the file.


Re: Reduce - GangstaSunny. - 24.10.2018

Quote:
Originally Posted by ******
Посмотреть сообщение
It is, took me 10 seconds to confirm that - 9 of those just for downloading the file.
founds, thanks.
my smartphone processor was just not fast enough to find the symbol in this big pdf file. lol.