Log in to reply
 

My mod won't update the player's current vehicle and only lets you run code on the first car the player enters.



  • I made a NativeUI mod that lets you open doors on cars. I followed the NativeUI tutorial from user AKH1221, which was excellent, but to make it work with "Game.Player.Character.CurrentVehicle;" is becoming difficult.

    It works fine, except it only works on the first car the player gets in. When you get into a different car, the controls open/close the hood and truck on the first car you entered, event though you're in another car.

    How can I make it so the open/close controls work for the current car the player is sitting in?

    The current code looks like this:

    // Tutorial on how to build this menu and import into GTA5 is included in the readme that came with the download
    // Credits to jedijosh920 for the string ideas (as shown below)
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Drawing;
    using System.Windows.Forms;
    using GTA;
    using GTA.Native;
    using GTA.Math;
    using NativeUI;
    
    namespace CarControls
    {
    
    	public class Class1 : Script
    	{
    
    		MenuPool modMenuPool;
    		UIMenu mainMenu;
    		UIMenu vehicleControlsMenu;
    
    
    		public Class1()
    		{
    			Setup();
    			Tick += onTick;
    			KeyDown += onKeyDown;
    		}
    
    		void Setup()
    		{
    			modMenuPool = new MenuPool();
    			mainMenu = new UIMenu("Mod Menu", "SELECT AN OPTION");
    			modMenuPool.Add(mainMenu);
    			vehicleControlsMenu = modMenuPool.AddSubMenu(mainMenu, "Vehicle Controls");
    			SetupVehicleControlsFunctions();
    		}
    
    
    			void VehicleControlsSelector()
    		{
    				//submenu Door Controls
    				UIMenu submenuOpenDoors = modMenuPool.AddSubMenu(vehicleControlsMenu, "Door Controls Open");
    				UIMenu submenuCloseDoors = modMenuPool.AddSubMenu(vehicleControlsMenu, "Door Controls Close");
    
    				Ped player = Game.Player.Character;
    				Vehicle vehicle = player.CurrentVehicle;
    
    				//open UI
    				UIMenuItem openDoorHood = new UIMenuItem("Open Hood");
    				submenuOpenDoors.AddItem(openDoorHood);
    				UIMenuItem openDoorTrunk = new UIMenuItem("Open Trunk");
    				submenuOpenDoors.AddItem(openDoorTrunk);
    
    
    				//close UI
    				UIMenuItem closeDoorHood = new UIMenuItem("Close Hood");
    				submenuCloseDoors.AddItem(closeDoorHood);
    				UIMenuItem closeDoorTrunk = new UIMenuItem("Close Trunk");
    				submenuCloseDoors.AddItem(closeDoorTrunk);
    
    			submenuOpenDoors.OnItemSelect += (sender, item, index) =>
    			{
    
    				if (item == openDoorHood)
    				{
    					Function.Call(Hash.SET_VEHICLE_DOOR_OPEN, vehicle, 4);
    				}
    				if (item == openDoorTrunk)
    				{
    					Function.Call(Hash.SET_VEHICLE_DOOR_OPEN, vehicle, 5);
    				}
    			};
    
    			submenuCloseDoors.OnItemSelect += (sender, item, index) =>
    			{
    
    				{
    					Function.Call(Hash.SET_VEHICLE_DOOR_SHUT, vehicle, 3);
    				}
    				if (item == closeDoorHood)
    				{
    					Function.Call(Hash.SET_VEHICLE_DOOR_SHUT, vehicle, 4);
    				}
    				if (item == closeDoorTrunk)
    				{
    					Function.Call(Hash.SET_VEHICLE_DOOR_SHUT, vehicle, 5);
    				}
    
    			};
    		}
    
    		void SetupVehicleControlsFunctions()
    		{
    			VehicleControlsSelector();
    		}
    
    		void onTick(object sender, EventArgs e)
    		{
    			if (modMenuPool != null)
    				modMenuPool.ProcessMenus();
    		}
    
    			void onKeyDown(object sender, KeyEventArgs e)
    		{
    			if (e.KeyCode == Keys.Z && !modMenuPool.IsAnyMenuOpen())
    			{
    				mainMenu.Visible = !mainMenu.Visible;
    			}
    		}
    	}
    }
    


  • @Gosuphobia Your vehicle variable is declared at local scope and the anonymous functions are always pointing to its value, which will never change. You should declare vehicle at class scope and update it to point at Game.Player.Character.CurrentVehicle, or, just use the direct reference to Game.Player.Character within the scope of your anonymous functions.



  • @CamxxCore
    I'm pretty new to C# would you be able to show me how to do either of those?



  • @Gosuphobia
    Get rid of these lines:
    Ped player = Game.Player.Character;
    Vehicle vehicle = player.CurrentVehicle;

    and then replace every reference to "vehicle" with "Game.Player.Character.CurrentVehicle".



  • @stillhere said in My mod won't update the player's current vehicle and only lets you run code on the first car the player enters.:

    Game.Player.Character.CurrentVehicle

    Nice, this worked.

    I'm not sure why it works though, how come "Game.Player.Character.CurrentVehicle" updates while "Ped player = Game.Player.Character; Vehicle vehicle = player.CurrentVehicle;" doesn't?

    Before I posted this, in another mod, I actually got around the vehicle not updating by putting the entire code in the onTick void, but I doubt it's a good workaround.

    Sorry for the late reply btw. The gta update messed me up so it took a bit to get the old version back.



  • @Gosuphobia
    CamxxCore explained it well, basically you only run your VehicleControlsSelector() method once (at startup), which is all good, but your "vehicle" variable is only set once (at startup), within the scope of the above-mentioned method.

    This means that the "vehicle" variable will either be null if you are not in a vehicle when the script loads/reloads, or will only be equal to the vehicle you are in when you load/reload your script. So as CamxxCore said, either declare the "vehicle" variable at class scope (for example, right under the line "UIMenu vehicleControlsMenu;") and then update it to point at Game.Player.Character.CurrentVehicle (for example, in your OnTick), or just directly reference Game.Player.Character.CurrentVehicle (which is the example I wrote in my previous post). Hope that helps!



  • @stillhere

    BlockquoteCamxxCore explained it well, basically you only run your VehicleControlsSelector() method once (at startup), which is all good, but your "vehicle" variable is only set once (at startup), within the scope of the above-mentioned method

    Thanks a lot, that's what I thought was happening but I couldn't update it, because I couldn't figure out what type of variable "vehicle" was.

    Before making this thread, and while using this:

    Ped player = Game.Player.Character;
    Vehicle vehicle = player.CurrentVehicle;
    

    I added this global variable under "namespace CarControls" :

    public static class Globals
        {
            public static string MyGlobalValue = "";
        }
    

    and put Globals.MyGlobalValue = vehicle; in OnTick.

    I started getting errors, because "string" is not the correct type for the vehicle variable. I tried replacing "string" with boolean, integer, and some others, but couldn't figure out which variable was the correct one.



  • @Gosuphobia
    Well you can't change a variable in a static class afaik, even if the class was public and the variable type was correct (which is "Vehicle").

    This would be another way to solve your initial code, which is the first option that CamxxCore suggested:

    // Tutorial on how to build this menu and import into GTA5 is included in the readme that came with the download
    // Credits to jedijosh920 for the string ideas (as shown below)
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Drawing;
    using System.Windows.Forms;
    using GTA;
    using GTA.Native;
    using GTA.Math;
    using NativeUI;
    
    namespace CarControls
    {
    
    	public class Class1 : Script
    	{
    
    		MenuPool modMenuPool;
    		UIMenu mainMenu;
    		UIMenu vehicleControlsMenu;
    Vehicle vehicle;
    
    		public Class1()
    		{
    			Setup();
    			Tick += onTick;
    			KeyDown += onKeyDown;
    		}
    
    		void Setup()
    		{
    			modMenuPool = new MenuPool();
    			mainMenu = new UIMenu("Mod Menu", "SELECT AN OPTION");
    			modMenuPool.Add(mainMenu);
    			vehicleControlsMenu = modMenuPool.AddSubMenu(mainMenu, "Vehicle Controls");
    			SetupVehicleControlsFunctions();
    		}
    
    
    			void VehicleControlsSelector()
    		{
    				//submenu Door Controls
    				UIMenu submenuOpenDoors = modMenuPool.AddSubMenu(vehicleControlsMenu, "Door Controls Open");
    				UIMenu submenuCloseDoors = modMenuPool.AddSubMenu(vehicleControlsMenu, "Door Controls Close");
    
    
    				//open UI
    				UIMenuItem openDoorHood = new UIMenuItem("Open Hood");
    				submenuOpenDoors.AddItem(openDoorHood);
    				UIMenuItem openDoorTrunk = new UIMenuItem("Open Trunk");
    				submenuOpenDoors.AddItem(openDoorTrunk);
    
    
    				//close UI
    				UIMenuItem closeDoorHood = new UIMenuItem("Close Hood");
    				submenuCloseDoors.AddItem(closeDoorHood);
    				UIMenuItem closeDoorTrunk = new UIMenuItem("Close Trunk");
    				submenuCloseDoors.AddItem(closeDoorTrunk);
    
    			submenuOpenDoors.OnItemSelect += (sender, item, index) =>
    			{
    
    				if (item == openDoorHood)
    				{
    					Function.Call(Hash.SET_VEHICLE_DOOR_OPEN, vehicle, 4);
    				}
    				if (item == openDoorTrunk)
    				{
    					Function.Call(Hash.SET_VEHICLE_DOOR_OPEN, vehicle, 5);
    				}
    			};
    
    			submenuCloseDoors.OnItemSelect += (sender, item, index) =>
    			{
    
    				{
    					Function.Call(Hash.SET_VEHICLE_DOOR_SHUT, vehicle, 3);
    				}
    				if (item == closeDoorHood)
    				{
    					Function.Call(Hash.SET_VEHICLE_DOOR_SHUT, vehicle, 4);
    				}
    				if (item == closeDoorTrunk)
    				{
    					Function.Call(Hash.SET_VEHICLE_DOOR_SHUT, vehicle, 5);
    				}
    
    			};
    		}
    
    		void SetupVehicleControlsFunctions()
    		{
    			VehicleControlsSelector();
    		}
    
    		void onTick(object sender, EventArgs e)
    		{
    vehicle = Game.Player.Character.CurrentVehicle;
    			if (modMenuPool != null)
    				modMenuPool.ProcessMenus();
    		}
    
    			void onKeyDown(object sender, KeyEventArgs e)
    		{
    			if (e.KeyCode == Keys.Z && !modMenuPool.IsAnyMenuOpen())
    			{
    				mainMenu.Visible = !mainMenu.Visible;
    			}
    		}
    	}
    }
    

Log in to reply
 

Looks like your connection to GTA5-Mods.com Forums was lost, please wait while we try to reconnect.