View Single Post
Old 27/05/2020, 09:14 PM   #9
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,862
Reputation: 1693
Default Re: Discord DCC_GetRoleName

Are you sure that there are no more "break"s in the "My actions" part?
The second example should absolutely not be done. break stops a loop and proceeds to code after the loop. If we take this example:

pawn Code:
main()
{
    for (new i; i <= 2; i++)
    {
        if (!i) //Aka "if (i == 0)". !variable equals variable == 0. This statement will only return true if i is 0 (-1 would also return true)
            continue; //If i is 0, cancel the current loop and go to the next loop.
        printf("This is message number %d!", i);
    }
}
This would produce:
Code:
>> Start loop with i, run it as long as i is 2 or less, increase i with every loop
[i = 0]
if (!i) is true: continue to the next loop, increase i with 1 (i++)
[i = 1]
if (!i) is false: do nothing (thus proceeding to the rest of the code)
print: This is message number 1!
Increase i with 1 (i++)
[i = 2]
if (!i) is false: do nothing (thus proceeding to the rest of the code)
print: This is message number 2!
Increase i with 1 (i++)
[i = 3]: Aborting loop
Now if you add a break to it like you did on the second example:
pawn Code:
main()
{
    for (new i; i <= 2; i++)
    {
        if (!i) //Aka "if (i == 0)". !variable equals variable == 0. This statement will only return true if i is 0 (-1 would also return true)
            continue; //If i is 0, cancel the current loop and go to the next loop.
        printf("This is message number %d!", i);
        break;
        print("This code is unreachable!");
    }
}
This would produce (in the runtime):
Code:
>> Start loop with i, run it as long as i is 2 or less, increase i with every loop
[i = 0]
if (!i) is true: continue to the next loop, increase i with 1 (i++)
[i = 1]
if (!i) is false: do nothing (thus proceeding to the rest of the code)
print: This is message number 1!
>BREAK<; Aborting entire loop
As you can see it skips a part that you actually wanted to show. Your code does not use continue so using that second example it will run only once.

However, if we take another look at your code:
pawn Code:
if(!strcmp(command, "!mycommand", true)) {
            for(new  i = 0; i < roleCount; i++) {
                DCC_GetGuildRole(g_Discord_Guild, i, rolename);
                if(rolename == g_Role_Level_1 || rolename == g_Role_Level_2 || rolename == g_Role_Level_3 || rolename == g_Role_Level_4) {
                    //My actions
                    break;
                } else return DCC_SendChannelMessage(g_Discord_Admin_CMD, "```ERROR: You are not a high enough level to use this command```");
            }
        }
Let's assume we have this sitation:

>> We have 5 g_Role_Level_s; g_Role_Level 0 up to g_Role_Level 4. Let's assume they are called this on Discord:
g_Role_Level_0 = User
g_Role_Level_1 = Admin level 1
g_Role_Level_2 = Admin level 2
g_Role_Level_3 = Admin level 3
g_Role_Level_4 = Admin level 4

Let's say that "g_Role_Level_0" is actually called before the other ones in the loop (i = 0 == g_Role_Level_0). In that scenario this would happen:
Code:
command equals "!mycommand"
>> Start loop with i, run it as long as i is less than variable roleCount. Increase i with every loop
[i = 0]
DCC_GetGuildRole on index 0 (i), save outcome to variable rolename
rolename = g_Role_Level_0 (User)
rolename does not equal g_Role_Level_1, g_Role_Level2, g_Role_Level3 or g_Role_Level 4; running the code after else:
>>return<< DCC_SendChannelMessage...
return: break the loop, return the return value of DCC_SendChannelMessage and break the entire function
That's it, the end. No matter if someone is admin or not (assuming the User level is called first), it would never work this way. Hence I recommend using DCC_HasGuildMemberRole()

As for the unreachable code; double check if there are no return, break or continue keywords in the code that should run. Also make sure, if it's used, there is no code in the rest of that level. Eg.

pawn Code:
MyCommand()
{
    //This space is the highest level inside MyCommand()
    new i;
    while (i < 50)
    {
        //Lower level [1]
        i++;
        if (i == 20)
        {
            //Lower level [2]
        }
        else continue; //NOTE: If this was "else return" like you did the print() function below (outside of the loop) would never run!
        //Code placed here would never run anyway
    }
    //Even more code here, back to highest level of MyCommand()
    print("This will not print if else continue is else return");
}
You can see in this example that if you use "else return", the entire function would stop; it does not just stop the code in that specific level.
I hope these examples may clear things out a bit
__________________
When the opportunity presents itself to flip-a da table, uh, you flip-a da table.

Discord: Kwarde#8009

Last edited by Kwarde; 16/06/2020 at 11:31 PM.
Kwarde is offline   Reply With Quote