ES_SENCE Posted October 17, 2017 Share Posted October 17, 2017 (edited) 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 October 18, 2017 by ES_SENCE Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now