"You can sell this vehicle at..." systems -
RajatPawar - 03.12.2013
I have seen a lot (I mean - a lot!) of people using horrible switch case systems to do this and I am here to share a better one. There
may be better methods than this but this is quite good too.
We'll create an array in which we'll store the vehicle model ID, the vehicle's price to be decided by you and - it's name.
The layout of the array will be like:
pawn Code:
new array_name[][][]=
{
iModel, iPrice, "szVehicleName",
..
The steps will be as follows -
1) Create the array with the information
2) Show the player that information when required
* and then check against it when he wants to sell the vehicle
BAD WAY OF DOING THIS:
pawn Code:
public OnPlayerStateChange(..)
{
if(newstate == PLAYER_STATE_DRIVER)
{
new iCurVeh = GetVehicleModel(GetPlayerVehicleID(playerid));
switch(iCurVeh)
{
case 411: // infernus!
....
......
.........
That is the most horrible way to do it, ever! But I admit even I did this when I was new, so no problem if you are doing it, you learn something new each day
1) Creating the array
This is the most amazing guide to arrays you'll find - if you don't understand anything about the following array, consult this or simply ask!
pawn Code:
new arrVehInfo[][][] =
{
411, 56000, "Infernus",
424, 12000, "Some other vehicle"
// model, price, name
}
;
You can manually insert the name or you can use a GetVehicleName stock available at ease on the forums. Similarly you can use a linked list instead of a static array if you need to save memory or simply want to OR if you don't want to add rows manually - you can create a stock if you use LLs like addListRow() !
2) Showing the information
Usually - AFAIK, thanks to servers like SACNR/CBCNR - the information is shown when a player enters the respective vehicle, so let's do it under OnPlayerStateChange and not OnPlayerEnterVehicle.
pawn Code:
public OnPlayerStateChange(playerid, newstate, oldstate)
{
// Was he onfoot and THEN he entered the driver and if yes, then only show him the info!
if(oldstate == PLAYER_STATE_ONFOOT && newstate == PLAYER_STATE_DRIVER)
{
new iVeh = GetPlayerVehicleID(playerid); // Get his vehicle ID not vehicle MODEL.
new iVehModel = GetVehicleModel(iVeh); // Can call implicitly the above 2 functions, but multiple calls, no - no or just for simplicity's sake
for(new index = 0; index < sizeof( arrVehInfo ); index++ )
{
if( arrVehInfo[ index ][ 0 ] == iVehModel )
// Yes - he's in that row's model
{
new szString[80];
format(szString, sizeof szString, "VEHICLE: You can sell this %s at a nearby carshop for $%d.",
arrVehInfo[ index ][ 2 ],
arrVehInfo[ index ][ 1 ] ) ;
// remember - the 0th pos was the model, 1st pos - the price and the 2nd pos - the string.
SendClientMessage(playerid, -1, szString);
break ; // You got the vehicle, why loop anymore?
}
}
return 1;
}
Now you can easily use this piece of code to your content!
.. and how to use it for a vehicle sale system
Suppose a player goes into a checkpoint to sell his vehicle.
You want to see if he can sell it?
Loop through the array, if the model is there - then he can!
pawn Code:
for(new index = 0; index < sizeof( arrVehInfo ); index++ )
{
if( arrVehInfo[ index ][ 0 ] == PlayerCurrentVehicleModel )
// Yes - he's in that row's model
{
// optionally you can check if he's got enough money
GivePlayerMoney( playerid, - (arrVehInfo[index][1] ) ); // 1st pos - price!
return SendClientMessageEx(... "Sold %s for %d!", arrVehInfo[index][2], arrVehInfo[index][1] );
}
}
So, in this better way, you add ONE row to an array instead of a whole switch CASE block!
Cheers!