26.03.2018, 12:55
How to check if there is a FBI Rancher around me? Like 2 meters far. Thanks
for(new k = 0; k < your_max_vehicles; k++) { if(IsPlayerInRangeOfPoint(playerid, 2, Car[k][XCoord], Car[k][YCoord], Car[k][ZCoord]) && vehicleid == id_of_fbi_rancher) { // Your Code here } }
native IsValidVehicle(vehicleid);
stock IsPlayerNearRancher(playerid) {
for(new v = 0; v < MAX_VEHICLES; v++) {
if(IsValidVehicle(v) && GetVehicleModel(v) == VEH_RANCHER) {
new Float: x, Float: y, Float: z;
GetVehiclePos(v, x, y, z);
if(IsPlayerInRangeOfPoint(playerid, 2.0, x, y, z))
return 1;
}
}
return 0;
}
// In a timer
if(IsPlayerNearRancher(playerid)) {
// code
}
stock IsPlayerNearSpecifiedVehicle(playerid, model) {
for(new v = 0; v < MAX_VEHICLES; v++) {
if(IsValidVehicle(v) && GetVehicleModel(v) == model) {
new Float: x, Float: y, Float: z;
GetVehiclePos(v, x, y, z);
if(IsPlayerInRangeOfPoint(playerid, 2.0, x, y, z))
return 1;
}
}
return 0;
}
1. If you store the vehicle data you are not only eating up unnecessary memory, you are going to need to continually update the data. This will be either by a timer, update procedure, or something else, requiring more code and used resources for the same effect.
2. Your code shouldn't call IsPlayerInRangeOfPoint for all 1000 vehicles, regardless of if they are actually valid vehicles or not. That is most likely one of the slowest native functions so you will want to keep the calls to a minimum.
stock IsPlayerNearSpecifiedVehicle(playerid, model) { for(new v = 0; v < MAX_VEHICLES; v++) { if(IsValidVehicle(v) && GetVehicleModel(v) == model) { new Float: x, Float: y, Float: z; GetVehiclePos(v, x, y, z); if(IsPlayerInRangeOfPoint(playerid, 2.0, x, y, z)) return 1; } } return 0; }
4. "No need for all these stocks." This is probably the 10th time I've read this over the past week. I would love someone to explain the logic behind this to me. Are they damaging in some way?
No. I'd not update anything. I don't mind if server has to store some floats in memory. When starting the server vehicles will be created at those coords, the coords will get stored in the vehicle's enum after that, so you can use the same coords to check if you are near a vehicle of those without stocking around. Also you are forgetting that you will have the vehicle to respawn not only when it gets destroyed, but also when someone rams that car away. You would use another stock to respawn it, I'd use a "simple" for() and whenever the coords arent the same the car will get respawned. Keep it clean.
|
Am sorry, but you did something similar.
there could be many cars with the same id, or he could need to check all of them to make moved cars respawn. This will make script getvehiclepos n times, and check the range of point n times. What's wrong with that? That you need to get the position everytime creating new floats everytime, and then do the checking. This ain't memory cheap thing at all. I prefer having my enum so the checkings are less, used functions are less. Also, you check MAX_VEHICLES regardless if the server has only 50 cars around. |
IsPlayerNearSpecificVehicle(playerid, vehicle_modelid, Float:radius)
{
new Float:vx, Float:vy, Float:vz;
for(new vi = 0; vi < MAX_VEHICLES; vi++)
{
GetVehiclePos(vi, vx, vy, vz);
if(vi == INVALID_VEHICLE_ID || GetVehicleModel(vi) != vehicle_modelid || !IsPlayerInRangeOfPoint(playerid, radius, vx, vy, vz)) continue;
return 1;
}
return 0;
}
1) No, for moved vehicles I could use GetVehiclePos. It depends, I am telling you that simply I won't suggest using GetVehiclePos for every vehicle, even the one standing untouched in their spawn place.
Scenario: Max vehicles is 1000, players online are 200. Not all of them are in a vehicle for sure. Some vehicles got rammed away 100% and I can assure you some will go on spawn points. And why not, explode. Make a player spawn there and he will shout "F****NG SERVER" and puff go away forever. |
2) Yes, and your code will be needed to re-execute eveytime the model changes, right?. If there are only 1 FBI rancher you will loop MAX_VEHICLES for what? And there are 4 sanchez on that hill. That is a waste as you loop every vehicle and do something only on some of them. Btw, technically you are not comparing the distances but checking if the player is in range of point of such points. IsPlayerInRangeOfPoint doesn't return the distance, but only if you are near there or not.
|