Hey fellas!
I want to create a script, which creates several “parking spots” and spawns a car on the first available parking spot.
This is realized by using several box-zones, one for each spot.
Now I need to figure out which zones are still “free”.
For accomplishing that, I thought of creating a boxzone and qb-target boxzones come with a method isPointInside(entity)
.
Therefore I could fetch a vehicle gamepool, and execute this method for each vehicle.
The first boxzone, that does not return true on any vehicle, is therefore free.
Yet, this would result in a lot of loops.
I’d have to check each box-zone against each vehicle. This could be very time- and performance-consuming.
Any better ideas to solve this?
Lovely would be some kind of “getEntitiesInBoxZone” or smth, or “GetCloseVehicles” or smth so I wouldn’t have to iterate ALL vehicles.
Just a thought (though I haven’t thought ultra hard so it may not be useful) you might be able to use QBCore.Functions.GetClosestVehicle() from the center point of each box zone. If the distance is less than X then return used and if it’s greater than X then return free.
1 Like
Whoa! For a second I thought “THAT’S IT!”, unfortunately it’s also making use of the whole GamePool.
Honestly I’m not even sure anymore there is another solution, so I might as well give it a try, hmm.
Might even use this one then.
Edit: Well. I just have to execute once(!) someone wants to get a vehicle out of a garage. It’s not like I need that in a thread 10times per second, so I guess I’ll just go for the suggested solution.
Unless I’m understanding wrong (which is totally possible, don’t get me wrong lol) it should go through the entire vehicle pool in the game. If you look at the QB docs and go to the documentation for GetClosestVehicle() on line 3 it says, “local vehicles = GetGamePool(‘CVehicle’)” Again, I may be misunderstanding, but if I am understanding correctly, it should work.
No, you’re right, but I never said something else? 
Haha.
That’s the thing. I wish there was a way without iterating the whole pool of possibly hundreds of vehicles. But I guess i’m overthinking this, since this is only executed once in minutes, so whatever.
assuming all parking spots are close to each other you just need to iterate the whole GamePool once, store the vehicles in a 250/500m radius and then just match those vehicles to each parking spot instead of the whole GamePool