SA-MP Forums Archive
[Help]How to make this code shorter - 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: [Help]How to make this code shorter (/showthread.php?tid=494709)



[Help]How to make this code shorter - AnthonyTimmers - 14.02.2014

Код HTML:
SetPlayerTeamFromClass(playerid, classid)
{
	if (classid == 0)
	{
		gTeam[playerid] = TEAM_BALLAS;
	}
	else if (classid == 1)
	{
		gTeam[playerid] = TEAM_BALLAS;
	}
	else if (classid == 2)
	{
		gTeam[playerid] = TEAM_BALLAS;
	}
	else if (classid == 3)
	{
		gTeam[playerid] = TEAM_GROVE;
	}
	else if (classid == 4)
	{
		gTeam[playerid] = TEAM_GROVE;
	}
	else if (classid == 5)
	{
		gTeam[playerid] = TEAM_GROVE;
	}
	else if (classid == 6)
	{
		gTeam[playerid] = TEAM_VAGOS;
	}
	else if (classid == 7)
	{
		gTeam[playerid] = TEAM_VAGOS;
	}
	else if (classid == 8)
	{
		gTeam[playerid] = TEAM_VAGOS;
	}
}
How can I make this shorter? Because this is only for 9 skins, and I plan to add a bunch more..

I've tried:
Код HTML:
	if (classid == 0 || 1 || 2)
	{
		gTeam[playerid] = TEAM_BALLAS;
	}
	else if (classid == 3 || 4 || 5)
	{
		gTeam[playerid] = TEAM_GROVE;
	}
	else if (classid == 6 || 7 || 8)
	{
		gTeam[playerid] = TEAM_VAGOS;
	}
But that didn't seem to work, is there no if (a or b or c) kind of way?


Re: [Help]How to make this code shorter - CuervO - 14.02.2014

PAWN syntax does not work like that, you can do, however..

Код:
classid == 0 || classid == 1 || classid == 2
or

Код:
classid >= 0 && classid <= 2
or even

Код:
0 <= classid <= 2
or work with switches

pawn Код:
switch(classid)
{

     case 0 .. 2:
     {
          //blabla
     }
     case 3 .. 4:
     {
          //blabla
     }
}



Re: [Help]How to make this code shorter - AnthonyTimmers - 14.02.2014

So..

Код HTML:
SetPlayerTeamFromClass(playerid, classid)
{
	if (0 <= classid <= 2)
	{
		gTeam[playerid] = TEAM_BALLAS;
	}
	else if (3 <= classid <= 5)
	{
		gTeam[playerid] = TEAM_GROVE;
	}
	else if (6 <= classid <= 8)
	{
		gTeam[playerid] = TEAM_VAGOS;
	}
}
?

EDIT:
Thanks, it works (:

Could you tell me why:

GameTextForPlayer(playerid, "Text", 3000, 6)

Does not display anything?

Is there also a way to display this text until a new class is selected?
For example if the skin selector is at a Grove skin it says Grove, until you switch to Ballas where it would say Ballas?


Re: [Help]How to make this code shorter - CuervO - 14.02.2014

Quote:

OnPlayerRequestClass

Called when a player changes class at class selection (and when class selection first appears).

Send a really long lasting message (that lasts 60000 ms or longer). When the player switches classes at OnPlayerRequestClass the newest one will overlay the last one.

As for it not working, I've got no idea of why it could be caused, where are you using it at?


Re: [Help]How to make this code shorter - AnthonyTimmers - 14.02.2014

Quote:
Originally Posted by CuervO
Посмотреть сообщение
Send a really long lasting message (that lasts 60000 ms or longer). When the player switches classes at OnPlayerRequestClass the newest one will overlay the last one.

As for it not working, I've got no idea of why it could be caused, where are you using it at?
I managed to get it working, I accidently used = instead of == on some locations, maybe that fixed it..

I don't suppose there's a way to adjust the text location?

And you can't add any custom colors for the textdraws?

Found this on the wiki, but those are all preset

Text Colors

~n~ New line
~r~ Red
~g~ Green
~b~ Blue
~w~ White
~y~ Yellow
~p~ Purple
~l~ Black


Re: [Help]How to make this code shorter - CuervO - 14.02.2014

You can adjust position and have a very long range of colors (4B I believe) if you use TextDraws, research about it on the wiki; here's a nice filterscript that does the job easily.

https://sampforum.blast.hk/showthread.php?tid=376758


Re: [Help]How to make this code shorter - AnthonyTimmers - 14.02.2014

Yeah, I just found a website and I'm making the colors, however I don't see any options for how long these textdraws show?

EDIT: Just tested it ingame, and I don't find these textdraws to look very... Well, I find 'em ugly.

I guess I could use ~b~ for blue and then use ~h~ to make it lighter, if I wanted Cyan?


Re: [Help]How to make this code shorter - ColeMiner - 14.02.2014

Text draws show until you manually hide them. There is no time parameter on them. Making a generic function to handle that isn't hard though - either a very generic "TextDrawSetTime" or a possibly more useful "TextDrawShowForPlayerTimed". Actually, either one of those seems like a good candidate for fixes.inc.


Re: [Help]How to make this code shorter - Beckett - 14.02.2014

You'll have to set a timer for that if you want the textdraw to appear for a short time when the timer ends HideTextDrawForPlayer.


Re: [Help]How to make this code shorter - AnthonyTimmers - 14.02.2014

Quote:
Originally Posted by ColeMiner
Посмотреть сообщение
Text draws show until you manually hide them. There is no time parameter on them. Making a generic function to handle that isn't hard though - either a very generic "TextDrawSetTime" or a possibly more useful "TextDrawShowForPlayerTimed". Actually, either one of those seems like a good candidate for fixes.inc.
'THE' ****** responds on my thread

I'm not gonna use TextDraw though, as I don't find it to look very good, gonna try to see if I can get blue to look like cyan with ~h~, not sure if that's possible though.