Function help
#1

Me again, yay!
I was trying to make a function so it can be called when a player joins a class- it will then calculate how many trucking missions they have done and display their "rank" in chat.

Code:
SetPlayerTruckerClassRank(playerid)
{
	printf("FUNCTION CALLED");
	new Name[24], Msg[128];
	
	// Get the player's name
	GetPlayerName(playerid, Name, sizeof(Name));
	
    switch(APlayerData[playerid][StatsTruckerJobs])
	{
		case 0:
     	{
     	    if (APlayerData[playerid][StatsTruckerJobs] < 15)
     	    {
				TruckerRank = "Newbie";
				format(Msg, sizeof(Msg), "{FFFF00}Newbie {FFFF00}%s{FFFF00} joined {FFFF00}Truckers", Name);
				SendClientMessage(playerid, COLOR_WHITE,  "{FFFF00}test");
				SendClientMessage(playerid, COLOR_WHITE,  "{FFFF00}test.");
			}
		}
I call it with SetPlayerTruckerClassRank(playerid); under OnPlayerRequestClass.
In console, the printf does indeed print FUNCTION CALLED. However, the rest doesn't appear to work.
The code looks a bit messy (the forum formatted it strangely). You can also view it here: https://pastebin.com/ypFcy3J9 - Please note that this is not all of the code, it is only the first rank because it's all the same (except the numbers are different).
Reply
#2

What's the issue though? You say everything works.
Reply
#3

Yeah what you want to ask?
Reply
#4

oh, I meant appears not to work* my bad. The text does not appear.
Reply
#5

Quote:
Originally Posted by Proxus
View Post
oh, I meant appears not to work* my bad. The text does not appear.
I don't think you know how switches work. Please read the SA-MP Wiki. If I were to translate your code into an if-else statement, it would be:

Code:
if(APlayerData[playerid][StatsTruckerJobs] == 0){
    if(APlayerData[playerid][StatsTruckerJobs] < 15){
        // code here..
    }
    // rest of the code here....
}
Reply
#6

I added the if statement after it stopped working. I do know how switch statements work but I'll remove the if statement to see if that changes anything. Thanks!

Edit: just realised, I'm really dumb. I didn't use SendClientMessageToAll for the Msg, sorry everyone! (:
However, how can I make it so that the cases affect other numbers inbetween? Like, at 0, it should be cases 0-14 and then at 15, cases 15-49, etc.
Reply
#7

Quote:
Originally Posted by Proxus
View Post
I added the if statement after it stopped working. I do know how switch statements work but I'll remove the if statement to see if that changes anything. Thanks!

Edit: just realised, I'm really dumb. I didn't use SendClientMessageToAll for the Msg, sorry everyone! (:
However, how can I make it so that the cases affect other numbers inbetween? Like, at 0, it should be cases 0-14 and then at 15, cases 15-49, etc.
Code:
switch(APlayerData[playerid][StatsTruckerJobs])
{
    case 0..14: // StatsTruckerJobs >= 0 && StatsTruckerJobs <= 14
    {
        // foo
    }
    case 15..49: // StatsTruckerJobs >= 15 && StatsTruckerJobs <= 49
        // foobar
    }
}
Although, do note that using switch statement for number ranges at three digit range values (case 400..600, etc) and above is much slower than using the equivalent if-else statement.
Reply
#8

Quote:
Originally Posted by ComDuck
View Post
Code:
switch(APlayerData[playerid][StatsTruckerJobs])
{
    case 0..14: // StatsTruckerJobs >= 0 && StatsTruckerJobs <= 14
    {
        // foo
    }
    case 15..49: // StatsTruckerJobs >= 15 && StatsTruckerJobs <= 49
        // foobar
    }
}
Although, do note that using switch statement for number ranges at three digit range values (case 400..600, etc) and above is much slower than using the equivalent if-else statement.
Thank you, repped.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)