SA-MP Forums Archive
Check vehicles nearby - 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: Check vehicles nearby (/showthread.php?tid=651723)



Check vehicles nearby - Drak03 - 26.03.2018

How to check if there is a FBI Rancher around me? Like 2 meters far. Thanks


Re: Check vehicles nearby - v1k1nG - 26.03.2018

Create an enum for vehicles, in the enum store X Y Z and rot coordinates of every vehicle. Then simply do a if() check,
Код:
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
}
}



Re: Check vehicles nearby - Grim_ - 26.03.2018

No need for storing each vehicle's data
pawn Код:
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
}



Re: Check vehicles nearby - v1k1nG - 26.03.2018

No need for all these stocks. As you did that he needs a stock for every vehicle. That'd be messy. I suggest to do the hard work with arrays at the beginning so you can add/remove/check them with a "simple" for(), without making all these stocks around.


Re: Check vehicles nearby - Grim_ - 26.03.2018

So, a few notes...

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.

3. You don't need a stock for each vehicle. He asked for one use case so he got it. Otherwise, he could do something like so...
pawn Код:
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?


Re: Check vehicles nearby - v1k1nG - 26.03.2018

Код:
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.
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.

Код:
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.
Am sorry, but you did something similar.

Код:
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;
}
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.

Код:
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?
https://sampforum.blast.hk/showthread.php?tid=570635


Re: Check vehicles nearby - Grim_ - 26.03.2018

Quote:
Originally Posted by v1k1nG
Посмотреть сообщение
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.
Let's not make any assumptions since we don't know how this guy is writing his gamemode. So let's take a common case in a server where vehicles don't respawn until they are destroyed. A player gets in the vehicle, drives 100 units away. Now your script will not only NOT detect when the player is near the vehicle, but will also DETECT when they are next to the original cords WITHOUT the vehicle there.

Quote:
Originally Posted by v1k1nG
Посмотреть сообщение
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.
My code checks the distance AFTER verifying 1) it's a valid vehicle and 2) it's the correct model. Lets say there are 20 FBI ranchers in the server. Mine will compare the distance between those 20 vehicles, not all 1000 like yours. You should also break out of the loop when a valid match is found.

Quote:
Originally Posted by v1k1nG
Посмотреть сообщение
Technically this is true, but has zero bearing on the output of the compiled code. In fact, I think the function will simply be excluded from the compiled source if not used at all. It's nit-picky (which I used to be as well) but hardly an "issue". I could complain about no use of Hungarian notation, improper use of the static keyword, no use of the const keyword, people misidentifying pawn as having types (ints, floats, strings, etc), but I'd rather stick to what is more important.


Re: Check vehicles nearby - v1k1nG - 26.03.2018

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.

3) I prefer listening to who's more pro than I'll ever be.


I am sorry if I misunderstood anything. Guests came and cant reply properly. Ill pass by later.


Re: Check vehicles nearby - Lokii - 26.03.2018

here i also tested it:

you can set you'r own radius too

PHP код:
IsPlayerNearSpecificVehicle(playeridvehicle_modelidFloat:radius)
{
    new 
Float:vxFloat:vyFloat:vz;
    for(new 
vi 0vi MAX_VEHICLESvi++)
    {
        
GetVehiclePos(vivxvyvz);
        if(
vi == INVALID_VEHICLE_ID || GetVehicleModel(vi) != vehicle_modelid || !IsPlayerInRangeOfPoint(playeridradiusvxvyvz)) continue; 
        return 
1;
    }
    return 
0;




Re: Check vehicles nearby - Grim_ - 26.03.2018

Quote:
Originally Posted by v1k1nG
Посмотреть сообщение
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.
Yet you still have to keep track of the vehicles to know their position. My point...

Quote:
Originally Posted by v1k1nG
Посмотреть сообщение
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.
No. And take a wild guess at how IsPlayerInRangeOfPoint works under the hood...lol.

Quote:
Originally Posted by v1k1nG
Посмотреть сообщение
3) I prefer listening to who's more pro than I'll ever be.
Hello.

Quote:
Originally Posted by v1k1nG
Посмотреть сообщение
I am sorry if I misunderstood anything. Guests came and cant reply properly. Ill pass by later.
I don't expect you to understand something you simply cannot comprehend. No worries. You will one day hopefully.