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



Function help - Proxus - 09.02.2019

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).


Re: Function help - ComDuck - 09.02.2019

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


Re: Function help - TokicMajstor - 09.02.2019

Yeah what you want to ask?


Re: Function help - Proxus - 09.02.2019

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


Re: Function help - ComDuck - 09.02.2019

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....
}



Re: Function help - Proxus - 09.02.2019

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.


Re: Function help - ComDuck - 09.02.2019

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.


Re: Function help - Proxus - 09.02.2019

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.