Sign in to follow this  
robearded

[C#] TaskEnterVehicle

Recommended Posts

Posted (edited)

So after a full day of trying to understand why this doesn't works and discussing the problem on discord and still didn't managed to fix it i decided to post here on forum, maybe someone that has already been here with this issue found a fix. I also want to thank George and the other people from discord that tried to help me fix this.

 

So here is the issue: I'm trying to call player.TaskEnterVehicle but with no success, I made sure the vehicle is not null, I made sure the vehicle is close to the player, still not working. I tried the exact same code but with a different task (it was something with exit vehicle) and it worked just fine. But for some reason the TaskEnterVehicle doesn't wants to.

 

	public class FGVehicleEnter : RAGE.Events.Script
	{

		private bool lastFstatus = false;
		private bool lastGstatus = false;

		public FGVehicleEnter()
		{
			RAGE.Events.Tick += Event_Key;
		}

		private void Event_Key(List<RAGE.Events.TickNametagData> nametags)
		{
			RAGE.Game.Pad.DisableControlAction(0, 23, true);

			// Check if Key is pressed and if Cursor is not active(so it won't trigger when chat is open)
			if (RAGE.Input.IsDown(0x47) && !RAGE.Ui.Cursor.Visible)
			{
				if (!lastGstatus)
				{
					lastGstatus = true;
					RAGE.Chat.Output("Pressed G");
					Player player = Player.LocalPlayer;
					Vehicle closestVehicle = GetClosestVehicle(player.Position);
					if (closestVehicle != null && closestVehicle.Position.DistanceTo(player.Position) < 5 && closestVehicle.AreAnySeatsFree())
					{
						Player.LocalPlayer.TaskEnterVehicle(closestVehicle.Handle, 5000, 0, 2.0f, 1, 0); // Problem: not entering vehicle
						RAGE.Chat.Output("Seat free"); // Gets printed to the chat, so that means the above line doesn't throws any error?
					}
				}
			}
			else
				lastGstatus = false;
		}

		public static Vehicle GetClosestVehicle(RAGE.Vector3 position)
		{
			List<Vehicle> allVehicles = RAGE.Elements.Entities.Vehicles.All;
			Vehicle closestVehicle = null;

			foreach(Vehicle vehicle in allVehicles)
			{
				if (closestVehicle == null) closestVehicle = vehicle;
				else if (position.DistanceTo(vehicle.Position) < position.DistanceTo(closestVehicle.Position)) closestVehicle = vehicle;
			}

			return closestVehicle;
		}

		
	}

So when I press F and there is no nearby car it just says "Pressed F", as it should. when I'm near a car, it also says "Seat free" as it should, but the line above that (the task) does nothing. It's my C# implementation of this resource: https://rage.mp/files/file/74-vehicle-fg-keys-entering/

I want to implement it on my own because I want to keep the server C# only, and do not mix it with JS resources and also I want to add some more custom functionality to it (for server admins) so using that resource isn't really a fix for me.

 

Other things I tried (still not working):

Player.LocalPlayer.TaskEnterVehicle(closestVehicle.Handle, 5000, 0, 2, 1, 0); // Speed as int instead of float (let the function handle the conversion)
Player.LocalPlayer.TaskEnterVehicle(closestVehicle.Handle, 5000, 0, 2, 1534654, 0); // Random int as p5
Player.LocalPlayer.TaskEnterVehicle(closestVehicle.Handle, 5000, 0, 1, 1, 0); // Speed 1 (Walk) instead of 2 (Run)
Player.LocalPlayer.TaskEnterVehicle(closestVehicle.Handle, 5000, 0, 2, 3, 0); // Teleport the player to the vehicle (p5 = 3)
// I also tried with different seat numbers, still nothing
// I tried with different p6 (last arg), wiki says it should be 0 always tho, not working with 0, not working with any other number

 

Edited by robearded
  • Like 1

Share this post


Link to post
Share on other sites
Posted (edited)

Did u try negative int on seat? (-1)

Also, try to increase the timeout value.

Edited by hyddro

Share this post


Link to post
Share on other sites
27 minutes ago, hyddro said:

Did u try negative int on seat? (-1)

Also, try to increase the timeout value.

 

Already tried both of them, not working. Tried with seats ranging from -1 to 3, also tried with timeout as big as 20000.

Share this post


Link to post
Share on other sites
Posted (edited)
22 minutes ago, robearded said:

 

Already tried both of them, not working. Tried with seats ranging from -1 to 3, also tried with timeout as big as 20000.

This is strange.. Try seat -2 (Any available seat)

Is there a chance that it might be a problem with Bridge?

Maybe?:

TaskEnterVehicle(player, closestVehicle.Handle, 10000, -2, 2.0f, 1, 0);
// Or
TaskEnterVehicle(player.Handle, closestVehicle.Handle, 10000, -2, 2.0f, 1, 0);

 

 

Edited by hyddro

Share this post


Link to post
Share on other sites
38 minutes ago, hyddro said:

This is strange.. Try seat -2 (Any available seat)

Is there a chance that it might be a problem with Bridge?

Maybe?:

TaskEnterVehicle(player, closestVehicle.Handle, 10000, -2, 2.0f, 1, 0);
// Or
TaskEnterVehicle(player.Handle, closestVehicle.Handle, 10000, -2, 2.0f, 1, 0);

 

 

Nope, not working even if I use the original one inside the RAGE.Game.Ai

I think it's a problem with bridge, just made myself this and it's working perfectly fine:

 

JS file:

mp.events.add({
	"startEnterVehicle" : (vehHandle, timeout, seat, speed, type) => {
		mp.players.local.taskEnterVehicle(vehHandle, timeout, seat, speed, type, 0);
	}
});

C# script:

RAGE.Events.CallLocal("startEnterVehicle", closestVehicle.Handle, 5000, 0, 2, 1);

 

Would still be nice tho if I could use this without having to do workarounds, but well, I guess for now it works.

  • Sad 1

Share this post


Link to post
Share on other sites

Why don't you just print out the vehicle it's trying to get into, if there's no vehicle being returned then it can't get into the vehicle.

Share this post


Link to post
Share on other sites
29 minutes ago, Crossy1998 said:

Why don't you just print out the vehicle it's trying to get into, if there's no vehicle being returned then it can't get into the vehicle.

You either didn't read the original post where i posted the code where you can see that i check if vehicle is not null or you either didnt read my above post were I say how i fixed it just by calling a local event and passing the vehicle to that event and then process the event in js and call task enter vehicle from there. 

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.