Jump to content
sarogahtyp

[Code Snippet] create method library - GetFuel, SetFuel (Arma 3 similar)

Recommended Posts

I will just continue to post snippets out of my tools class which show how create methods which can be used similar to Arma 3.

Here I ll show you SetFuel and later I ll add GetFuel as well.

 

The goal is to have an Arma 3 like fuel setter which uses values between 0 (0%)and 1 (100%) to set the fuel. The challenge is that in AR vehicles can have multiple fuel tanks which are handled by fuelnodes which are "part" of the FuelManagerComponent:

 

Spoiler

class SaRo_Tools
{
	// Arma 3 like fuel setter (0.0 ... 0.5 ... 1.0)
	static void SetFuel ( IEntity vehicle, float fuel )
	{
		//get fuel manager
		FuelManagerComponent fuelManagerComponent = FuelManagerComponent.Cast (vehicle.FindComponent(FuelManagerComponent));
		
		ref array<BaseFuelNode> a_fuelNodes = {};
		
		fuelManagerComponent.GetFuelNodesList(a_fuelNodes);
		
		int fuelNodeCount = a_fuelNodes.Count();
		
		foreach (BaseFuelNode currentFuelNode : a_fuelNodes)
		{			
			currentFuelNode.SetFuel( fuel * currentFuelNode.GetMaxFuel() );
		};
	}
};

usage example:

SaRo_Tools.SetFuel ( pOwnerEntity, 0.0);

 

EDIT:

 

Now we create the fuel getter. But I realized that parts of the fuel setter are needed in the fuel getter as well. Therefore I created an additional method for not doing things twice.

Now we have SetFuel, GetFuel and the protected method GetFuelNodes:

Spoiler

class SaRo_Tools
{
// *************************  Fuel Tools

	// Arma 3 like fuel setter (0.0 ... 0.5 ... 1.0)
	static void SetFuel ( IEntity vehicle, float fuel )
	{
		ref array<BaseFuelNode> a_fuelNodes = GetFuelNodes (vehicle);
		
		foreach (BaseFuelNode currentFuelNode : a_fuelNodes)
		{			
			currentFuelNode.SetFuel( fuel * currentFuelNode.GetMaxFuel() );
		};
	}

	// Arma 3 like fuel getter (0.0 ... 0.5 ... 1.0)
	static float GetFuel ( IEntity vehicle )
	{	
		ref array<BaseFuelNode> a_fuelNodes = GetFuelNodes (vehicle);
		
		float maxFuel = 0, fuel = 0;
		
		foreach (BaseFuelNode currentFuelNode : a_fuelNodes)
		{			
			maxFuel += currentFuelNode.GetMaxFuel();
			fuel += currentFuelNode.GetFuel();
		};
		
		//zero divisor failsafe
		if(maxFuel > 0) return (fuel / maxFuel);
		
		return 0.0;
	}
	
    //method for getting all fuel nodes (or fuel tanks)
	protected static array<BaseFuelNode> GetFuelNodes ( IEntity vehicle )
	{
		//get fuel manager
		FuelManagerComponent fuelManagerComponent = FuelManagerComponent.Cast (vehicle.FindComponent(FuelManagerComponent));
		
		ref array<BaseFuelNode> a_fuelNodes = {};
		
		fuelManagerComponent.GetFuelNodesList(a_fuelNodes);
		
		return a_fuelNodes;
	}
};

 

 

usage example:

float f_fuelAmount;

f_fuelAmount = SaRo_Tools.GetFuel (vehicleEntity);

 

There is one more thing to note about this.

If a vehicle has several differently filled tanks and you empty them using the two methods, remember the fuel quantity and then fill them up again, then in the end all the tanks will be the same percentage full, even if they were filled to different percentages beforehand.
The overall amount of fuel will be the same as before.
This really matters in a very few cases only I think.

  • Like 2

Share this post


Link to post
Share on other sites

Thank you, great work just what I was looking for.

 

There is only one problem I have noticed,  the UI of the vehicle still shows the fuel in the vehicle, but the vehicle will not start.

Share this post


Link to post
Share on other sites
Just now, joemac90 said:

Thank you, great work just what I was looking for.

 

There is only one problem I have noticed,  the UI of the vehicle still shows the fuel in the vehicle, but the vehicle will not start.

This is correct. I did notice that as well. the UI fuel gauge does not register the changed fuel amount but idk why and how to fix it currently.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×