House cars problem. -
thimo - 31.01.2014
Hello i am experiencing a problem with my car buying system. When i buy a vehicle when i have two houses i automaticly get 2 vehicles. Instead it should search for a free spot in the first house. If theres none search in second house if theres none then cancel. This is my code:
pawn Код:
stock BuyVehicle(ModelID, Float:PosX, Float:PosY, Float:PosZ, Float:PosRot, OwnerName[], CarName[], playerid, Price)
{
new string[255], HouseID, i = GetFreeCarIDSpot(), vid, pName[MAX_PLAYER_NAME];
GetPlayerName(playerid, pName, sizeof(pName));
for (new HouseSlot; HouseSlot < MAX_HOUSESPERPLAYER; HouseSlot++)
{
HouseID = APlayerData[playerid][Houses][HouseSlot];
printf("OnPlayerBuyVehicle::HouseID = %d", HouseID);
if (HouseID != 0)
{
for (new CarSlot; CarSlot < House_GetMaxCarSlots(HouseID); CarSlot++)
{
if (AHouseData[HouseID][VehicleIDs][CarSlot] == 0)
{
GivePlayerCash(playerid, -Price);
format(string, sizeof(string), "INSERT INTO Vehicles (HouseID, ID, Owner, Model, X, Y, Z, Rot, Color1, Color2) VALUES ('%d', '%d', '%s', '%d', '%f', '%f', '%f', '%f', '1', '1')", HouseID, i, OwnerName, ModelID, PosX, PosY, PosZ, PosRot);
mysql_function_query(Handle, string, false, "", "");
format(string, sizeof(string), "You have succesfully bought a %s!", CarName);
SendClientMessage(playerid, 0x00FF00FF, string);
vid = Vehicle_Create(ModelID, PosX, PosY, PosZ, PosRot, 1, 1, 600);
printf("House_AddVehicle::vid= %d", vid);
AHouseData[HouseID][VehicleIDs][CarSlot] = vid;
printf("House_AddVehicle::AHouseData[%d][VehicleIDs][%d]= %d", HouseID, CarSlot, AHouseData[HouseID][VehicleIDs][CarSlot]);
AVehicleData[vid][CarModel] = ModelID;
AVehicleData[AHouseData[HouseID][VehicleIDs][CarSlot]][SaveID] = i;
// Save the spawn-data of the vehicle
AVehicleData[vid][SpawnX] = PosX;
AVehicleData[vid][SpawnY] = PosY;
AVehicleData[vid][SpawnZ] = PosZ;
AVehicleData[vid][SpawnRot] = PosRot;
// Also set the fuel to maximum
AVehicleData[vid][FuelData] = MaxFuel;
// Also set the owner
AVehicleData[vid][Owned] = true;
format(AVehicleData[vid][Owner], 24, pName);
// Save the HouseID for the vehicle
AVehicleData[vid][BelongsToHouse] = HouseID;
VehicleLoad[i] = true;
SavePlayer(playerid);
break;
}
}
}
}
return 1;
}
How would i go around doing that?
Re: House cars problem. -
Misiur - 31.01.2014
You need to break out of two loops on success, not single one (the break moves out of one level). To do this you can:
a) create a boolean flag, which will be raised on success, and checked in outer loop - if it's raised, break of that loop as well
b) use
goto with a label (it's one of the last real world use cases for that keyword)
Re: House cars problem. -
thimo - 31.01.2014
Could you show me an example? I have never used the goto thingy
Re: House cars problem. -
Misiur - 31.01.2014
Using goto
pawn Код:
stock BuyVehicle(ModelID, Float:PosX, Float:PosY, Float:PosZ, Float:PosRot, OwnerName[], CarName[], playerid, Price)
{
new string[255], HouseID, i = GetFreeCarIDSpot(), vid, pName[MAX_PLAYER_NAME];
GetPlayerName(playerid, pName, sizeof(pName));
for (new HouseSlot; HouseSlot < MAX_HOUSESPERPLAYER; HouseSlot++)
{
HouseID = APlayerData[playerid][Houses][HouseSlot];
printf("OnPlayerBuyVehicle::HouseID = %d", HouseID);
if (HouseID != 0)
{
for (new CarSlot; CarSlot < House_GetMaxCarSlots(HouseID); CarSlot++)
{
if (AHouseData[HouseID][VehicleIDs][CarSlot] == 0)
{
GivePlayerCash(playerid, -Price);
format(string, sizeof(string), "INSERT INTO Vehicles (HouseID, ID, Owner, Model, X, Y, Z, Rot, Color1, Color2) VALUES ('%d', '%d', '%s', '%d', '%f', '%f', '%f', '%f', '1', '1')", HouseID, i, OwnerName, ModelID, PosX, PosY, PosZ, PosRot);
mysql_function_query(Handle, string, false, "", "");
format(string, sizeof(string), "You have succesfully bought a %s!", CarName);
SendClientMessage(playerid, 0x00FF00FF, string);
vid = Vehicle_Create(ModelID, PosX, PosY, PosZ, PosRot, 1, 1, 600);
printf("House_AddVehicle::vid= %d", vid);
AHouseData[HouseID][VehicleIDs][CarSlot] = vid;
printf("House_AddVehicle::AHouseData[%d][VehicleIDs][%d]= %d", HouseID, CarSlot, AHouseData[HouseID][VehicleIDs][CarSlot]);
AVehicleData[vid][CarModel] = ModelID;
AVehicleData[AHouseData[HouseID][VehicleIDs][CarSlot]][SaveID] = i;
// Save the spawn-data of the vehicle
AVehicleData[vid][SpawnX] = PosX;
AVehicleData[vid][SpawnY] = PosY;
AVehicleData[vid][SpawnZ] = PosZ;
AVehicleData[vid][SpawnRot] = PosRot;
// Also set the fuel to maximum
AVehicleData[vid][FuelData] = MaxFuel;
// Also set the owner
AVehicleData[vid][Owned] = true;
format(AVehicleData[vid][Owner], 24, pName);
// Save the HouseID for the vehicle
AVehicleData[vid][BelongsToHouse] = HouseID;
VehicleLoad[i] = true;
SavePlayer(playerid);
goto outside_loop;
}
}
}
}
outside_loop:
return 1;
}
Using flag:
pawn Код:
stock BuyVehicle(ModelID, Float:PosX, Float:PosY, Float:PosZ, Float:PosRot, OwnerName[], CarName[], playerid, Price)
{
new string[255], HouseID, i = GetFreeCarIDSpot(), vid, pName[MAX_PLAYER_NAME], bool:flag = false;
GetPlayerName(playerid, pName, sizeof(pName));
for (new HouseSlot; HouseSlot < MAX_HOUSESPERPLAYER && !flag; HouseSlot++)
{
HouseID = APlayerData[playerid][Houses][HouseSlot];
printf("OnPlayerBuyVehicle::HouseID = %d", HouseID);
if (HouseID != 0)
{
for (new CarSlot; CarSlot < House_GetMaxCarSlots(HouseID); CarSlot++)
{
if (AHouseData[HouseID][VehicleIDs][CarSlot] == 0)
{
GivePlayerCash(playerid, -Price);
format(string, sizeof(string), "INSERT INTO Vehicles (HouseID, ID, Owner, Model, X, Y, Z, Rot, Color1, Color2) VALUES ('%d', '%d', '%s', '%d', '%f', '%f', '%f', '%f', '1', '1')", HouseID, i, OwnerName, ModelID, PosX, PosY, PosZ, PosRot);
mysql_function_query(Handle, string, false, "", "");
format(string, sizeof(string), "You have succesfully bought a %s!", CarName);
SendClientMessage(playerid, 0x00FF00FF, string);
vid = Vehicle_Create(ModelID, PosX, PosY, PosZ, PosRot, 1, 1, 600);
printf("House_AddVehicle::vid= %d", vid);
AHouseData[HouseID][VehicleIDs][CarSlot] = vid;
printf("House_AddVehicle::AHouseData[%d][VehicleIDs][%d]= %d", HouseID, CarSlot, AHouseData[HouseID][VehicleIDs][CarSlot]);
AVehicleData[vid][CarModel] = ModelID;
AVehicleData[AHouseData[HouseID][VehicleIDs][CarSlot]][SaveID] = i;
// Save the spawn-data of the vehicle
AVehicleData[vid][SpawnX] = PosX;
AVehicleData[vid][SpawnY] = PosY;
AVehicleData[vid][SpawnZ] = PosZ;
AVehicleData[vid][SpawnRot] = PosRot;
// Also set the fuel to maximum
AVehicleData[vid][FuelData] = MaxFuel;
// Also set the owner
AVehicleData[vid][Owned] = true;
format(AVehicleData[vid][Owner], 24, pName);
// Save the HouseID for the vehicle
AVehicleData[vid][BelongsToHouse] = HouseID;
VehicleLoad[i] = true;
SavePlayer(playerid);
flag = true;
break;
}
}
}
}
return 1;
}
Re: House cars problem. -
thimo - 31.01.2014
And using the goto thing what would i place there then? Code for the second house?
Re: House cars problem. -
Misiur - 31.01.2014
? Both versions already fix your problem from first post question, that's all there is to use of goto/flag
Re: House cars problem. -
thimo - 01.02.2014
Thank you
Re: House cars problem. -
PowerPC603 - 01.02.2014
Replace the "goto" command by "return 1;", as the goto command goes to the end of the function anyway.
Goto is usually bad practice.
Re: House cars problem. -
Misiur - 01.02.2014
Huh, why I didn't simply think of normal return? Oh well (maybe if in the future he wants to add something), also
that's one of the last goto strongholds