Jump to content
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. Los Santos Drug Wars
      2. Updates
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Red Dead Online

      1. Blood Money
      2. Frontier Pursuits
      3. Find Lobbies & Outlaws
      4. Help & Support
    3. Crews

    1. Grand Theft Auto Series

      1. Bugs*
      2. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Classic GTA SA
      2. Guides & Strategies
      3. Help & Support
    6. GTA Vice City

      1. Classic GTA VC
      2. Guides & Strategies
      3. Help & Support
    7. GTA III

      1. Classic GTA III
      2. Guides & Strategies
      3. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

    2. Support

    3. Suggestions

Happy Holidays from the GTANet team!

[C#] Random crash when creating multiple vehicles.


ES_SENCE
 Share

Recommended Posts

Hello, guys.

I'm working on making a simple script mod using .Net framework.

I'm a beginner at C# (actually at coding) and I don't know why my script makes my game random crash sometimes.

The reason that I said "random" is because sometimes it works fine, sometimes it crashes.

 

Event viewer and WinDbg of Windows 10 say the exception code is "0xc0000005" which is "Access violation" and error module is "nvwgf2umx.dll".

It seems it crashes because of nvidia driver.

I completely deleted my nvidia driver and installed latest version of it but it didn't help.

 

Additionally, creating single vehicle by my logic works always fine for now.

But when I tried to create "multiple" vehicles by for loop, it crashes randomly.

Is there anyone who can find my flaws of my codes ?

Here they are:

public class EntitySet    {        private Ped ped;        private Vehicle vehicle;        private Vector3 position;        public EntitySet(Ped p, Vehicle v)        {            ped = p;            vehicle = v;            position = Vector3.Zero;        }        public EntitySet(Ped p, Vehicle v, Vector3 v3)        {            ped = p;            vehicle = v;            position = v3;        }        public Ped Ped        {            get { return IsExists(ped) ? ped : null; }        }        public Vehicle Vehicle        {            get { return IsExists(vehicle) ? vehicle : null; }        }        public Vector3 Position        {            get { return position; }        }        public void MarkAsNoLongerNeeded()        {            if (IsExists(ped)) ped.MarkAsNoLongerNeeded();            if (IsExists(vehicle))            {                List<VehicleSeat> seats = new List<VehicleSeat> { VehicleSeat.LeftFront, VehicleSeat.RightFront, VehicleSeat.LeftRear, VehicleSeat.RightRear };                foreach (VehicleSeat vs in seats)                {                    if (!vehicle.IsSeatFree(vs))                        vehicle.GetPedOnSeat(vs).MarkAsNoLongerNeeded();                }                vehicle.MarkAsNoLongerNeeded();            }        }    }---------------------------------------------------------------------------------------------------// Codes below are inside of main script class.private static List<Vector3> racingPosition = new List<Vector3> { new Vector3(1411.75f, 3012.3f, 41.1f), new Vector3(-981.0f, -2995.0f, 13.1f), new Vector3(2119.5f, 4806.3f, 41.2f), new Vector3(171.62f, 6603.35f, 32.0f), new Vector3(-2558.05f, 2327.3f, 33.0f), new Vector3(-714.85f, -932.65f, 19.2f), new Vector3(1212.5f, -1403.6f, 35.38f) };private static List<string> racerCarNames = new List<string> { "infernus2", "cheetah2", "turismo2", "xa21", "gp1", "le7b", "nero", "nero2", "prototipo", "tyrus", "vagner", "zentorno", "cyclone", "visione", "cheetah3", "schaftergtr", "comet4", "typhoon", "vacca", "sheava", "t20", "entityxf", "turismor", "cheetah" };private static Random dice = new Random();private List<EntitySet> racers = new List<EntitySet>();public static bool IsExists(Entity en)        {            return (en != null && en.Exists());        }private static bool IsBlipExists(Entity en)        {            return (IsExists(en) && en.CurrentBlip != null && en.CurrentBlip.Exists());        }private static Vector3 GetSafePosition(bool isForPed)        {            Entity[] nearbyEntities = World.GetNearbyEntities(Game.Player.Character.Position, radius);            Vector3 v = Vector3.Zero;            if (nearbyEntities.Length <= 0) return Vector3.Zero;            foreach (Entity en in nearbyEntities)            {                if (!en.IsOnScreen || IsBehindSomething(en)) // The method IsBehindSomething is not complete yet. It always returns false for now.                {                    v = en.Position;                    break;                }            }            return isForPed ? World.GetNextPositionOnSidewalk(v) : World.GetNextPositionOnStreet(v);        }private Vehicle CreatingVehicle(Model m, Vector3 v3, float h)        {            if (!m.Request(1000) || !m.IsLoaded) return null;            Vehicle v = World.CreateVehicle(m, v3, h);            Function.Call(Hash.SET_ENTITY_RECORDS_COLLISIONS, v, true);            Function.Call(Hash.SET_ENTITY_LOAD_COLLISION_FLAG, v, true);            Function.Call(Hash.SET_ENTITY_LOD_DIST, v, 1000);            m.MarkAsNoLongerNeeded();            if (!IsExists(v)) return null;            v.PlaceOnGround();            Array vehicleColors = Enum.GetValues(typeof(VehicleColor));            v.PrimaryColor = (VehicleColor)vehicleColors.GetValue(dice.Next(vehicleColors.Length));            v.SecondaryColor = (VehicleColor)vehicleColors.GetValue(dice.Next(vehicleColors.Length));            return v;        }private void AddingBlip(Entity en, BlipSprite bs, BlipColor bc, string bn)        {            en.AddBlip();            en.CurrentBlip.Scale = 0.7f;            en.CurrentBlip.Sprite = bs;            en.CurrentBlip.Color = bc;            en.CurrentBlip.Name = bn;            en.CurrentBlip.IsShortRange = true;        }private void Tuning(Vehicle v)        {            if (!IsExists(v)) return;            v.InstallModKit();            v.ToggleMod(VehicleToggleMod.Turbo, true);            using (IEnumerator<VehicleMod> etr = Enum.GetValues(typeof(VehicleMod)).Cast<VehicleMod>().GetEnumerator())            {                while (etr.MoveNext())                {                    int cur = (int)etr.Current;                    if (cur != (int)VehicleMod.Horns && cur != (int)VehicleMod.FrontWheels && cur != (int)VehicleMod.BackWheels)                        v.SetMod((VehicleMod)cur, dice.Next(-1, v.GetModCount((VehicleMod)cur)), false);                }            }            Array neonColors = Enum.GetValues(typeof(KnownColor));            v.NeonLightsColor = Color.FromKnownColor((KnownColor)neonColors.GetValue(dice.Next(neonColors.Length)));            v.SetNeonLightsOn(VehicleNeonLight.Back, true);            v.SetNeonLightsOn(VehicleNeonLight.Front, true);            v.SetNeonLightsOn(VehicleNeonLight.Left, true);            v.SetNeonLightsOn(VehicleNeonLight.Right, true);        }private void ActivateRacingEvent()        {            Vector3 racingPoint = racingPosition[dice.Next(0, racingPosition.Count)];            Vector3 safePosition = GetSafePosition(false);            if (safePosition.Equals(Vector3.Zero)) return;            for (int i = 0; i < 4; i++)            {                Vehicle spawnedVehicle = CreatingVehicle(racerCarNames[dice.Next(0, racerCarNames.Count)], safePosition.Around(5.0f), Game.Player.Character.Heading);                if (!IsExists(spawnedVehicle)) continue;                Ped spawnedPed = spawnedVehicle.CreateRandomPedOnSeat(VehicleSeat.Driver);                if (!IsExists(spawnedPed))                {                    spawnedVehicle.Delete();                    continue;                }                Function.Call(Hash.SET_ENTITY_RECORDS_COLLISIONS, spawnedPed, true);                Function.Call(Hash.SET_ENTITY_LOAD_COLLISION_FLAG, spawnedPed, true);                Function.Call(Hash.SET_ENTITY_LOD_DIST, spawnedPed, 1000);                Tuning(spawnedVehicle);                spawnedPed.RelationshipGroup = racerID;                spawnedPed.AlwaysKeepTask = true;                spawnedPed.BlockPermanentEvents = true;                if (!IsBlipExists(spawnedPed))                {                    AddingBlip(spawnedPed, BlipSprite.PersonalVehicleCar, (BlipColor)17, "Racer " + spawnedVehicle.FriendlyName);                    racers.Add(new EntitySet(spawnedPed, spawnedVehicle, racingPoint));                }                else                {                    spawnedVehicle.Delete();                    spawnedPed.Delete();                }                Wait(250);            }            foreach (EntitySet es in racers)            {                TaskSequence ts = new TaskSequence();                ts.AddTask.DriveTo(es.Vehicle, es.Position, 10.0f, 100.0f, (int)DrivingStyle.AvoidTrafficExtremely);                ts.AddTask.Wait(10000);                ts.AddTask.CruiseWithVehicle(es.Vehicle, 100.0f, (int)DrivingStyle.AvoidTrafficExtremely);                ts.Close();                es.Ped.Task.PerformSequence(ts);                ts.Dispose();                Function.Call(Hash.FLASH_MINIMAP_DISPLAY);            }        }

 

Thank you for reading and have a nice day guys.

 

- Forgot to write IsBlipExists method so I've just added it.

Edited by ES_SENCE
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.