if statement
#1

Hi, I have normal vehicles and dynamic vehicles (they are owned by players only) in my gamemode. Their windows can be rolled down. But only dynamic vehicles' window can be broken. So what I want to do is when player is in a vehicle and the vehicle's window is broken or they are rolled down then send a message to everyone nearby, else send a message only to the passengers. I have this:
Код:
if (!IsBike(vehicleid) && !VehicleInfo[vehicleid][VehicleWindowDown][0] && !VehicleInfo[vehicleid][VehicleWindowDown][1] && !VehicleInfo[vehicleid][VehicleWindowDown][2] && !VehicleInfo[vehicleid][VehicleWindowDown][3] && (GetVehicleDynamicID(vehicleid) != -1 && !DynamicVehicleInfo[GetVehicleDynamicID(vehicleid)][VehicleWindowBroken]))
{
	for (new i, j = GetPlayerPoolSize(); i <= j; i++) if (IsPlayerConnected(i) && GetPlayerVehicleID(i) == vehicleid) SendClientMessageEx(i, COLOR_GOLD, "(vehicle) %s says: %s", GetName(playerid, 0), text);
	return 0;
}
and it works well on dynamic vehicles, the message is sent to all passengers if the window is not broken or the windows are closed, but when you are in a normal vehicle then it sends a message to all players nearby even though the windows are closed. I tried changing the if statement, but it doesn't work how I want it to. Do I need to break it into two pieces to make it work?
Reply
#2

This will never really work correctly, because there is only one possible case: All Windows are closed. But the vehicle cannot be both, Dynamic and non-Dynamic. Thus it will always fail/succeed, depending on the data in the arrays.

Код:
if(!IsBike(...) && ((IsDynamicVehicle && AllWindowsClosed) || (!IsDynamicVehicle && AllWindowsClosed)))
{
// Send to passenger(s)
}
else
{
// Send to all nearby
}
This should be the correct logic.

You must replace AllWindowsClosed etc. with your checks, but keep the brackets in place.

All your conditions were checked with AND, however some have to be put in brackets and checked with OR, since it cannot be both a Dynamic Vehicle and a non-Dynamic one, but it should consider both cases.

The brackets you put were not doing any difference, since AND statements have no particular order.


Btw why do you split the vehicles into two arrays? You could keep them in one and just make some "ownable".
Since you access both with vehicleid, it should work fine. That would probably simplify things like this a bit.
But I don't know if you have any reason for splitting them, so this is just an idea.
Reply
#3

Thank you, it works now.
Quote:
Originally Posted by NaS
Посмотреть сообщение
Btw why do you split the vehicles into two arrays? You could keep them in one and just make some "ownable".
Since you access both with vehicleid, it should work fine. That would probably simplify things like this a bit.
But I don't know if you have any reason for splitting them, so this is just an idea.
I don't know, but they are not both accessed with vehicleid.
This is what DynamicVehicleInfo looks like:
Код:
enum dynamicVehicleInfo
{
	VehicleID,
	VehicleExists,
	VehicleRealID,
	VehicleModel,
	VehicleFuel,
	Float:VehiclePos[4],
	Float:VehicleHealth,
	VehicleDamages[4],
	VehicleWindowBroken,
	VehicleOwner,
	VehiclePlate[8],
	VehicleLocked,
	VehicleColor1,
	VehicleColor2,
	VehiclePaintjob,
	VehicleMods[14]
}
new DynamicVehicleInfo[MAX_DYNAMIC_VEHICLES][dynamicVehicleInfo];
Reply
#4

Quote:
Originally Posted by GoldenLion
Посмотреть сообщение
Thank you, it works now.

I don't know, but they are not both accessed with vehicleid.
This is what DynamicVehicleInfo looks like:
Код:
enum dynamicVehicleInfo
{
	VehicleID,
	VehicleExists,
	VehicleRealID,
	VehicleModel,
	VehicleFuel,
	Float:VehiclePos[4],
	Float:VehicleHealth,
	VehicleDamages[4],
	VehicleWindowBroken,
	VehicleOwner,
	VehiclePlate[8],
	VehicleLocked,
	VehicleColor1,
	VehicleColor2,
	VehiclePaintjob,
	VehicleMods[14]
}
new DynamicVehicleInfo[MAX_DYNAMIC_VEHICLES][dynamicVehicleInfo];
Oops, sorry I read too quickly.
Then that should be correct, did you try the if() logic I mentioned above? If the data is correct, that should work.
Reply
#5

Quote:
Originally Posted by NaS
Посмотреть сообщение
Oops, sorry I read too quickly.
Then that should be correct, did you try the if() logic I mentioned above? If the data is correct, that should work.
Yes, as you could see above I replied "Thank you, it works now." along with the quote.
Reply
#6

I find checking whether a vehicle is dynamic or not redundant as no matter what, it will still execute the same code if it is not bike and depending on the windows' state.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)