else if and case: - 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: else if and case: (
/showthread.php?tid=478651)
else if and case: -
BpX - 30.11.2013
Hello all,
I've been using 'else if's the whole time, until I found out the switch statements make things more efficient. However, I'm a little confused with its usage.
pawn Код:
switch(GetVehicleModel(vehicleid))
{
//AIRPLANES//
case 460: //code x1
case 476: //code x2
case 511: //code x3
case 512: //code x4
case 513: //code x5
}
The code is substantially long, and it occupies 5 times as much space as compared to using else if. Is there a way to do it such that I only need to have just one of it is carried out throughout the 5 cases?
pawn Код:
if(GetVehicleModel(playerid) == 460 || GetVehicleModel(playerid) == 476 || GetVehicleModel(playerid) == 511 || GetVehicleModel(playerid) == 512 || GetVehicleModel(playerid) == 513)
{
//code x1
}
Thanks
Re: else if and case: -
Loot - 30.11.2013
pawn Код:
IsAirplane(vehicleid)
{
switch(GetVehicleModel(vehicleid))
{
case 460, 476, 511 .. 513: return 1;
default: return 0;
}
return 0;
}
//usage
if(IsAirplane(vehicleid))
{
//..
}
Re: else if and case: -
J4mmyHD - 30.11.2013
pawn Код:
IsAPlane(vehicleid)
{
switch(GetVehicleModel(vehicleid))
{
case 460, 476, 511, 512, 513, 519, 520, 553, 577, 592, 593: return 1;
default: return 0;
}
return 0;
}
CMD:airplane(playerid, params[])
{
new Vehicleid = GetPlayerVehicleID(playerid);
If(IsAPlane(vehicleid)) return SendClientMessage(playerid, 0xFFFFFFFF, "You are in a Plane!");
If(!IsAPlane(vehicleid)) return SendClientMessage(playerid, 0xFFFFFFFF, "You are not in a Plane!");
return 1;
}
Re: else if and case: -
Konstantinos - 30.11.2013
Quote:
Originally Posted by J4mmyHD
pawn Код:
IsAPlane(vehicleid) { switch(GetVehicleModel(vehicleid)) { case 460, 476, 511, 512, 513, 519, 520, 553, 577, 592, 593: return 1; default: return 0; } return 0; }
CMD:airplane(playerid, params[]) { new Vehicleid = GetPlayerVehicleID(playerid); If(IsAPlane(vehicleid)) return SendClientMessage(playerid, 0xFFFFFFFF, "You are in a Plane!"); If(!IsAPlane(vehicleid)) return SendClientMessage(playerid, 0xFFFFFFFF, "You are not in a Plane!"); return 1; }
|
If the model is not any of those above, it will just end the switch so the default is pointless.
There's
if and not
If. An as for the example, you don't need to store the vehicleid to a variable and neither use it IsAPlane function twice.
pawn Код:
// Forward it before using it because of the bool tag.
forward bool: IsAPlane(vehicleid);
// ---
CMD:airplane(playerid, params[])
{
if(IsAPlane(GetPlayerVehicleID(playerid))) SendClientMessage(playerid, 0xFFFFFFFF, "You are in a Plane!");
else SendClientMessage(playerid, 0xFFFFFFFF, "You are not in a Plane!");
return 1;
}
// At the bottom:
stock bool: IsAPlane(vehicleid)
{
switch(GetVehicleModel(vehicleid))
{
case 460, 476, 511 .. 513, 519, 520, 553, 577, 592, 593: return true;
}
return false;
}
Re: else if and case: -
BpX - 01.12.2013
Oh, I see that you can use commas within cases. Thanks guys!