Jump to content

Engine propellant switching


Recommended Posts

I'm working on a mod for engines. One basic function is supposed to be switching the propellant on an engine, like fuelswitch does for fueltanks. It is supposed to function as an alternative to the engine switch like on rapiers. I aim at integrating against tech level, and maybe inflight upgrading by EVA or something.

Now, my current issue is, I cannot get the propellant switch to work. I can change the name of the propellant, but that does not change the propellant.

A copy of the full C# module in it's current state can be obtained here.

Spoiler

        private void assignPropToPart(bool calledByPlayer)
        {
            foreach (var moduleEngine in ModuleEngines)
            {
                foreach(var prop in moduleEngine.propellants)
                {
                    //Debug.Log(prop.id + " / " + prop.name + " / " + prop.ignoreForIsp + " / ");
                    if (prop.name == "LiquidFuel")
                    {

                    }
                    else if (prop.name == "IntakeAir")
                    {
                        prop.name = "Oxidizer";
                        //prop.id
                        //[Log]: 374119730 / LiquidFuel / False /     
                        //[Log]: -1909417378 / Oxidizer / True / 
                    }
                }
            }
        }

 

I think I need to change the ".id" of the propellant to get the game to recognise the change. If this is what I need to do, then I do not know how to obtain the resource ID's other than through the error logs in KSP. I've tried "PartResourceLibrary.GetDefinition(string)" but I get an error I simply cannot figure out how to fix. --> "An object reference is required for the non-static field, method, or property 'PartResourceLibrary.GetDefinition(string)'."

I reality I would really like to be able to delete the old propellants and replace them by new ones.

Can anyone help?

 

Link to comment
Share on other sites

Thanks wasml, will try that!

 

EDIT:

After testing I can say that  PartResourceLibrary.Instance.GetDefinition(string)  does give me access to the propellant (resource id), and can be changed. However, I still don't know how to delete existing ones. => I stuck to the amount of propellents there are initially... 

Edited by Warezcrawler
Tested Reply from wasml
Link to comment
Share on other sites

I had something working previously (in the editor anyway) that went something like this:

            part.Resources.list.Remove(oldResource);
            cellResource = AddResourceToPart(part, newResource.name, newVolume);
            part.Resources.UpdateList();

There's some commented out code in that area so I I was working on it and don't remember if the above was the working code.

Link to comment
Share on other sites

1 hour ago, NathanKell said:

It would be better to just make a ConfigNode, stick the new PROPELLANT nodes in it, and call the engine's OnLoad on the confignode.

OK... Sorry but I've never used ConfigNode before. So how does it work. How do if declare it correctly and find the node I wish to process, i.e. the part which have my module, and the player have just right clicked in order to do their thing?

Link to comment
Share on other sites

20 minutes ago, NathanKell said:

ConfigNode foo = new ConfigNode();

ConfigNode propNode = foo.AddNode("PROPELLANT");

propNode.AddValue("name", "LiquidFuel");

propNode.AddValue("ratio", 0.9f);

// do Ox too

engineModule.Load(foo);

That's great. And my testing just showed that it does delete the old nodes for propellants at the same tid. It does not save with the game through. So am I forced to load the configuration of the engine each time the module loads?

Link to comment
Share on other sites

OK, next issue. When changing the propellants I want to activate drawgauge for the new propellant. Now this happens like this

propNode.AddValue("DrawGauge", true);

However, KSP does not remove the gauge of past propellants like when using the engineswitch on the rapier. How do I get it to reset the gauges?

I've tried this before updating the propellants, but it doesn't have an effect.

foreach (var localPropellant in moduleEngine.propellants)
{
	localPropellant.drawStackGauge = false;
}

If I quicksave and reload, the gauges are perfectly aligned to the propellant setup. So I figure it just a matter of forcing KSP to update the gauges.

Link to comment
Share on other sites

Another issue I've run into. Since changing propellant should change ISP (atmCurve and velCurve) as well, I figured I'd replace these as well. So I have these defined in my module as they are defined in the engine module.

MODULE
{
  name = EngineClassSwitch
  //engineID = "NOT IMPLEMENTED"
  //engineNames = "NOT IMPLEMENTED"
  propellantNames = LiquidFuel;LiquidFuel,Oxidizer;MonoPropellant
  propellantRatios = 100;45,55;100
  propellantIgnoreForIsp = false;false,false;false			//default = false
  stagingEnabled = false
  availableInFlight = false
  availableInEditor = true
  velCurve
  {
    key = 0 1 0 0.08333334
    key = 1 0.98 0.42074 0.42074
  }
  atmCurve
  {
    key = 0 0 0 0
    key = 1 1 7.914787 7.914787
  }
}

The value in the curves are just for testing. Now I need to load them, and I currently do that like this

        [KSPField]
        public FloatCurve[] velCurve;
        [KSPField]
        public FloatCurve[] atmCurve;

I'm not sure that is correct. Next I need to transfer these curves into the engine when I change the propellant. I just can't figure out how to manipulate those curves. I've tried ConfigNode, but they did not seem to update like the propellant.

I've tried something like this, but it does NOT work.

foreach (var moduleEngine in ModuleEngines)
{
    //moduleEngine.velCurve = velCurve;
    Debug.LogError("moduleEngine.velCurve.ToString " + moduleEngine.velCurve.ToString());
    Debug.LogError("moduleEngine.atmCurve.ToString " + moduleEngine.atmCurve.ToString());

    //moduleEngine.velCurve.Curve.
    int i = 0;
    foreach(var key in moduleEngine.velCurve.Curve.keys)
    {
        Debug.LogError("Key[" + i + "]: " + key.time + " " + key.value + " " + key.inTangent + " " + key.outTangent);
        i++;
    }
    //moduleEngine.velCurve.Curve.AddKey(0, 1, 0, 0);
    //moduleEngine.atmCurve = atmCurve;

    ConfigNode newVelCurveNode = new ConfigNode();
    ConfigNode Node = newVelCurveNode.AddNode("velCurve");
    Node.AddValue("key", "0 1 1 1");
    Node.AddValue("key", "0.2 0.98 2 2");
    moduleEngine.Load(Node);
    i = 0;
    foreach (var key in moduleEngine.velCurve.Curve.keys)
    {
        Debug.LogError("Key[" + i + "]: " + key.time + " " + key.value + " " + key.inTangent + " " + key.outTangent);
        i++;
    }

I hope someone out there can help me figure this thing out....

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...