Skip to content
View in the app

A better way to browse. Learn more.

LCPDFR.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[Method] Vehicle Spawning by Driving Distance

Featured Replies

Hi, I've developed a method which is helpful if you need to spawn vehicles in your plugin and you do not want to wait forever in order to do so. I've never run it outside of a new GameFiber but it doesn't take long to find a good spot usually. Basically what it does is it makes sure that the spawnpoint that is randomly picked based off a radius from the origin is within a developer-determined driving distance by well getting random spawn points and checking the driving distance. This is helpful if the player is on the freeway and the game wants to spawn the vehicle on the other side and there are no places the AI will turn around for a mile or so. It is also helpful when the game wants to spawn the vehicle in the LS river or in the freeway when where it needs to go is in the city.

If it fails to get a spawn point after 125 tries it will just revert back to the usual method. I have yet to encounter it needing to do this.

        /// <summary>
        /// Find a close-by-travel-distance vehicle spawn (not guaranteed)
        /// Recommended to run in a different game fiber
        /// </summary>
        /// <param name="origin">The center of where you want the spawn to be. Should be close to destination.</param>
        /// <param name="radius">Max radius of spawn zone</param>
        /// <param name="maxDist">Maximum driving distance acceptable</param>
        /// <returns></returns>
        public static Vector3 FindCloseVehicleSpawn(Vector3 origin, float radius, float maxDist)
        {
            bool running = true;
            int timesRun = 0;
            while(running & timesRun < 125)
            {
                Vector3 testPoint = World.GetNextPositionOnStreet(origin.Around(radius));
                //float CALCULATE_TRAVEL_DISTANCE_BETWEEN_POINTS(float x1, float y1, float z1, float x2, float y2, float z2)
                float drivDist = Rage.Native.NativeFunction.CallByName<float>("CALCULATE_TRAVEL_DISTANCE_BETWEEN_POINTS",
                    testPoint.X,
                    testPoint.Y,
                    testPoint.Z,
                    origin.X,
                    origin.Y,
                    origin.Z);
                if(drivDist <= maxDist)
                {
                    running = false;
                    return testPoint;
                }
                timesRun++;
            }
            return World.GetNextPositionOnStreet(origin.Around(radius));
        }

I'm open to criticism on my code as well, I am by no means a professional developer and am eager to learn :)

Edited by goigle

So correct me if I'm wrong, but this calculates distance based on the actual driving distance, as opposed to radius distance "as the crow flies"? 

In other words, 40 meters behind you on the freeway is closer than 10 meters away on the opposite side. 

Nice idea! You may not have to run it in a separate fiber, but you're right, it is better to. But you would need to wait for the function to finish... Or put the calling function in the same separate fiber.   

Stealth22
LSPDFR Tester | Plugin Developer
My Plugins: Code 3 Callouts | Traffic Control | Keep Calm | ALPR+

Please do not PM me for any kind of technical support.
I unfortunately do not have enough free time to answer every PM that I get. For issues with my plugins, please post in the comments section of the file, or it's forum thread. You'll get a much quicker response from me there than if you send me a PM; I do my best to respond to every question in the comments sections. For API/programming questions, please post them in the API Development forum, so all developers can benefit from the answer as well. Thanks!

  • Author

So correct me if I'm wrong, but this calculates distance based on the actual driving distance, as opposed to radius distance "as the crow flies"? 

In other words, 40 meters behind you on the freeway is closer than 10 meters away on the opposite side. 

Nice idea! You may not have to run it in a separate fiber, but you're right, it is better to. But you would need to wait for the function to finish... Or put the calling function in the same separate fiber.   

You are correct, it is calculating based on the driving distance the AI would take. I'm pretty sure it's equivalent to what shows up in the bottom left of the minimap when you set a waypoint. About fibers, you would have to keep it all in one fiber for initialization or at least check to make sure it Exists() before continuing.

GameFiber.StartNew((ThreadStart)(() =>
{
Vector3 spawn = Extensions.FindCloseVehicleSpawn(SpawnPoint, 95f, 130f);
vehicle = new Vehicle("burrito3", spawn);
}));

I personally tend to keep things in a separate GameFiber whenever I am spawning things anyway, not sure how others do it

 

Edit: not really sure why I included the code

Edited by goigle

Spawning a single vehicle isn't an expensive operation. If you're doing a lot of stuff, the game may lock up for the player in some situations.

The only thing to keep in mind when using fibers (or threads, outside the RPH world), is that if your function has to return something, the method calling your fiber/thread operation needs to wait for it to complete. Which, unless you do it properly, will lock up the main thread anyway.

Stealth22
LSPDFR Tester | Plugin Developer
My Plugins: Code 3 Callouts | Traffic Control | Keep Calm | ALPR+

Please do not PM me for any kind of technical support.
I unfortunately do not have enough free time to answer every PM that I get. For issues with my plugins, please post in the comments section of the file, or it's forum thread. You'll get a much quicker response from me there than if you send me a PM; I do my best to respond to every question in the comments sections. For API/programming questions, please post them in the API Development forum, so all developers can benefit from the answer as well. Thanks!

  • Author

I personally already use something similar in my plugins.

Something to keep in mind is that the native you're using does not calculate driving distance; it calculates walking distance. Because of this, highway spawns and pedestrianised areas may not be accurately represented by it.

Interesting, where did you find that out? I haven't had any trouble with it so far but that is definitely worth noting... I'll do some more tests and see if it causes a big enough problem.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.