Profile image

Mod Script Question (and we probably found a bug)

8 TheSovietOnion  7.8 years ago

So I just found out about the unity modding API, and I am trying to create a simple script that refills the aircraft's fuel over time. Is there any way to add to the player's aircraft's fuel without changing the fuel capacity?

ServiceProvider.Instance.PlayerAircraft.Fuel += 0.001f

Will add to the aircraft's total fuel, but will also set the aircraft's fuel capacity to the fuel value. Is there any way to prevent this?

  • Log in to leave a comment
  • Profile image
    Dev WNP78

    @TheSovietOnion Nathan (a dev) made some reflection tools a while ago to make it easier. I personally don't use them but it's probably the best way for you to approach them. They are on GitHub here

    7.8 years ago
  • Profile image

    @HellFireKoder @WNP78
    So how exactly would this be done with a reflection? Haven't used C# in awhile, so I have no idea on how to do this.

    Also, thanks for your help.

    7.8 years ago
  • Profile image

    @TheSovietOnion it's probably a bug, but unless/until the devs fix it, the only way is with reflection as suggested by @WNP78

    7.8 years ago
  • Profile image

    @WNP78
    You shouldn't need a reflection to modify the amount of fuel in your aircraft. The serviceprovider probably has all you need to add fuel. Serviceprovider.Instance.PlayerAircraft.Fuel is able to add to your total fuel value, but when it does that the fuel capacity also changes. There should be a way to prevent this, but I have not yet found one. If there is no way to do so, this could either be a bug (unlikely), or it could be a bit harder to do this than it seems.

    7.8 years ago
  • Profile image
    Dev WNP78

    @TheSovietOnion to get anything more than what the service provider gives, you have to use reflection. I don't know if you have much/any c# experience, but when learning, I found this pretty helpful. If you need help, ask.

    7.8 years ago
  • Profile image

    @WNP78
    Seems like a good idea, but ServiceProvider.Instance.PlayerAircraft.FuelCapacity seems to be read-only. There is no way to set the fuel capacity (as far as I know). I've tried to use ServiceProvider.Instance.PlayerAircraft.FuelCapacity = (variable), but this throws an error that says that ServiceProvider.Instance.PlayerAircraft.FuelCapacity cannot be assigned to.

    7.8 years ago
  • Profile image
    Dev WNP78

    @HellFireKoder @TheSovietOnion actually, I do get what he meant, as I had to make a workaround when making the refueling mod. I think you have to first get the values of FuelCapacity and InitialFuelCapacity, store them (in variables), set the fuel like you were, then reset FuelCapacity and InitialFuelCapacity to the values you stored.

    7.8 years ago
  • Profile image
    7,698 RealQcan

    Wow. Impressive

    7.8 years ago
  • Profile image

    @HellFireKoder
    Ok thanks.
    red

    7.8 years ago
  • Profile image

    @TheSovietOnion hmm... I'll look into it later and get back to you...

    To make any text red like this, put backticks around it (`red` makes red)

    7.8 years ago
  • Profile image

    Yes, ServiceProvider.Instance.PlayerAircraft.FuelCapacity is for reading the plane's capacity,

    And yes, even if the fuel level is 0, and you add 1 with PlayerAircraft.Fuel, the capacity will be set to 1. I check the fuel level with that same 'if' statement, and after the fuel decreases, the script sets the fuel capacity and the fuel level itself to the current fuel level + 1.

    Also, how can I make the code colorful in the forums? Its a bit hard to read when the code is the same color as the text.

    7.8 years ago
  • Profile image

    I haven't tested this, but it looks like ServiceProvider.Instance.PlayerAircraft.FuelCapacity should be for capacity...
    You mean that setting ServiceProvider.Instance.PlayerAircraft.Fuel above the fuel capacity will increase the fuel capacity?
    If that's the case, just check if(ServiceProvider.Instance.PlayerAircraft.Fuel < ServiceProvider.Instance.PlayerAircraft.FuelCapacity - 0.001f) before increasing the fuel.

    If it really sets the capacity even when it's below the capacity, then I'll have to check some things...

    7.8 years ago