c# worldgetallvehicle performance issue
-
Hey all me again!
I've been working on a c# script to remove semis without trailers. I seem to have something that works but over time the performance is noticeable.
I wonder if I wrote my script wrong, or if there's a more optimized way to handle the data collection or something.
namespace nakedSemiSuppressor { public class Main : Script { public Main() { Interval = 200; Tick += OnTick; } private void OnTick(object sender, EventArgs e) { Vehicle[] all_vehs = World.GetAllVehicles(); foreach (var veh in all_vehs) { if (Function.Call<int>(Hash.GET_ENTITY_MODEL, veh) == Function.Call<int>(Hash.GET_HASH_KEY, "hauler") && !Function.Call<bool>(Hash.IS_VEHICLE_ATTACHED_TO_TRAILER, veh)) { veh.Delete(); } else if (Function.Call<int>(Hash.GET_ENTITY_MODEL, veh) == Function.Call<int>(Hash.GET_HASH_KEY, "packer") && !Function.Call<bool>(Hash.IS_VEHICLE_ATTACHED_TO_TRAILER, veh)) { veh.Delete(); } else if (Function.Call<int>(Hash.GET_ENTITY_MODEL, veh) == Function.Call<int>(Hash.GET_HASH_KEY, "phantom") && !Function.Call<bool>(Hash.IS_VEHICLE_ATTACHED_TO_TRAILER, veh)) { veh.Delete(); } } } } }
-
Further revised to exclude previous/current vehicle if its a semi as well as leaving parked semis alone.
namespace nakedSemiSuppressor { public class Main : Script { public Main() { Tick += OnTick; } private void OnTick(object sender, EventArgs e) { //add semis to collection Vehicle[] all_semis = World.GetAllVehicles(VehicleHash.Hauler, VehicleHash.Packer, VehicleHash.Phantom); //if (Game.Player.Character.Exists() && Game.Player.CanControlCharacter) if (Game.Player.Character.Exists()) { foreach (var semis in all_semis) { if (Function.Call<bool>(Hash.IS_VEHICLE_ATTACHED_TO_TRAILER, semis)) { Function.Call(Hash.SET_ENTITY_HEALTH, semis, 750); } else if (!Function.Call<bool>(Hash.IS_VEHICLE_ATTACHED_TO_TRAILER, semis)) { if (!Game.Player.Character.IsSittingInVehicle(semis)) { Vector3 veh_coords = Function.Call<Vector3>(Hash.GET_ENTITY_COORDS, semis); bool veh_pos = Function.Call<bool>(Hash.IS_POINT_ON_ROAD, veh_coords.X, veh_coords.Y, veh_coords.Z, semis); if (veh_pos == true) { Vehicle veh_prev = Function.Call<Vehicle>(Hash.GET_VEHICLE_PED_IS_IN, Game.Player.Character, true); Vehicle veh_curr = Function.Call<Vehicle>(Hash.GET_VEHICLE_PED_IS_TRYING_TO_ENTER, Game.Player.Character); if (veh_prev != semis && veh_curr != semis) { if (semis.Health > 750) { semis.Delete(); } } } } } } } } } }