Jump to content

Saving and loading editor-defined engine parameters


Recommended Posts

So I want to make an engine with user-defined thrust.  I've gotten my part loaded in the editor, and I can adjust the values, but when it launches, the thrust is the default value from the cfg file, not whatever it was changed to in the editor.

I'm using "part.GetComponent<ModuleEngines>();" to get the engine and "engine.maxThrust = mymaxthrust" to set the maximum thrust value, but it clearly isn't overriding the value in the config file.

Here are some screenshots illustrating the situation:

http://imgur.com/a/iAxhi

And here's my .cfg file:
 

PART
{
	// many fields propagated based on info here:
	// http://wiki.kerbalspaceprogram.com/wiki/CFG_File_Documentation

	// general params
	name = ProceduralEnginePart
	module = Part
	author = Humanegg

	// asset params

	MODEL 
	{
		model = ProceduralRealEngines/Parts/model
		position = 0.0,0.0,0.0
		scale = 1,1,1
	}

	// attachment rules: stack, srfAttach, allowStack, allowSrfAttach, allowCollision
	attachRules =        1,     0,         1,          0,              0

	// node params     (x,   y,     z,   angx,angy,angz,  size)
	
	node_stack_top    = 0.0, 0.7,   0.0, 0.0, 1.0,  0.0,  1
	node_stack_bottom = 0.0, -.5,   0.0, 0.0, -1.0, 0.0,  1

	fx_exhaustFlame_blue      = 0.0, -.5, 0.0, 0.0, 1.0, 0.0, running
	fx_exhaustLight_blue      = 0.0, -.5, 0.0, 0.0, 0.0, 1.0, running
	fx_smokeTrail_light       = 0.0, -.5, 0.0, 0.0, 1.0, 0.0, running
	fx_exhaustSparks_flameout = 0.0, -.5, 0.0, 0.0, 1.0, 0.0, flameout
	sound_vent_medium = engage
	sound_rocket_hard = running
	sound_vent_soft = disengage
	sound_explosion_low = flameout

	// editor params
	entryCost = 0
	cost = 0
	category = Engine
	subcategory = 0
	title = Procedural Engine
	manufacturer = DnyaDynamics
	description = If you make your motor out of klay, you can just mush it around until it does what you want. Adjust the performance characteristics of your motor until it perfectly fits your discerning taste!
	

	// standard part params
	mass = 0.5
	heatConductivity = 0.06
	skinInternalConductionMult = 4.0
	emissiveConstant = 0.8 // engine nozzles are good at radiating.

	dragModelType = default
	maximum_drag = 0.2
	minimum_drag = 0.2
	angularDrag = 2
	crashTolerance = 8
	maxTemp = 3200
	stagingIcon = LIQUID_ENGINE
	bulkheadProfiles = size1
	tags = engine launch propuls rocket

	MODULE
	{
		// http://wiki.kerbalspaceprogram.com/wiki/Module#ModuleEngines
		name= ModuleEngines
		thrustVectorTransformName = thrustTransform
		exhaustDamage = True
		exhaustDamageDistanceOffset = 0.6
		throttleLocked = False
		exhaustDamage = True
		allowShutdown = True
		ignitionThreshold = 0.1
		fxOffset = 0, 0, .5
		EngineType = LiquidFuel

		// these values will get overriden by the procedural engine module, but are defaults for now.
		minThrust = 0
		maxThrust = 215

		// these doesn't get overriddent yet.
		heatProduction = 100
		atmosphereCurve
		{
			key = 0 320
			key = 1 250
		}
	
		PROPELLANT
		{
			name = LiquidFuel
			ratio = 0.9
			DrawGauge = True
		}
		PROPELLANT
		{
			name = Oxidizer
			ratio = 1.1
		}

	}

	MODULE
	{
		name = ProceduralEngineModule

		// default thrust and isps
		maxThrust = 100
		isp0 = 320
		isp1 = 250
		
		// Heat Produced = heatPerThrust * sqrt(thrust) / (1+total mass)
		// borrowed from the old procedural srb by proceduralparts
		heatPerThrust = 25
	}
}

and my module:
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Megalonyx
{
    public class ProceduralEngineModule : PartModule
    {
        public ProceduralEngineModule() { }
         
        private void log(String message)
        {
            Debug.Log("[ProceduralEngine]" + message);
        }
        private ModuleEngines getEngine()
        {
            return part.GetComponent<ModuleEngines>();
        }
        private void Logstatus(String message)
        {
            String mt = maxThrust.ToString();
            String emt = getEngine().maxThrust.ToString();
            log(message + " Internal max thrust: " + mt + ", Engine max thrust: " + emt);
        }
        public override void OnActive()
        {
            Logstatus("[Active]");
        }

        public override void OnAwake()
        {
            Logstatus("[Awakening]");
            base.OnAwake();
            UpdateThrust();
            Logstatus("[Awake]");
        }

//        public override void OnStart(StartState state)
//        {
//            Logstatus("[Starting]");
//            base.OnStart(state);
//            Logstatus("[Started]");
//        }

        public override void OnLoad(ConfigNode node)
        {
            Logstatus("[Loading]");
            base.OnLoad(node);
            UpdateThrust();
            Logstatus("[Loaded]");
        }

        public override void OnSave(ConfigNode node)
        {
            Logstatus("[Saving]");
            base.OnSave(node);
            UpdateThrust();
            Logstatus("[Saved]");
        }

        public void Update()
        {            
            UpdateThrust();
        }

        public void OnUpdateEditor()
        {
            //Debug.Log("updating while in editor.");
            UpdateThrust();
        }

        [UI_FloatRange(minValue = 0.0f, maxValue = 1000.0f, stepIncrement = 5.0f, controlEnabled = true)]
        [KSPField(isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "Max Thrust")]
        public float maxThrust = 100.0f;
        private float oldMaxThrust = 100.0f;

        public void UpdateThrust()
        {
            
            ModuleEngines engine = getEngine();
            oldMaxThrust = engine.GetMaxThrust();
            if (oldMaxThrust != maxThrust)
            {
                EngineType etype = engine.engineType;
                engine.maxThrust = maxThrust;
                log("found engine:" + engine.ToString() + " of type " + etype);
                log("updated thrust to new level:" + engine.maxThrust.ToString());
                Logstatus("");
            }
        }
    }
}

Is there some other way I should be setting the thrust of the engine module? Or am I missing some obvious goofiness?
I got most of my inspiration from the ProceduralParts source code, and I've read through it reasonably thoroughly, but couldn't find anything that applied directly here.  
Any advice or suggestions are very welcome.  
Thanks in advance!

Link to comment
Share on other sites

12 hours ago, humanegg said:

Is there some other way I should be setting the thrust of the engine module? Or am I missing some obvious goofiness?

Try and look at some of my code for engine switch

Calculating the maxFuelFlow (this is central for engine thrust, just as sarbian said)

https://github.com/WarezCrawler/Guybrush101/blob/master/GTI_Utilities/Utilities/GenericFunctions.cs#L37-L41

Using it

https://github.com/WarezCrawler/Guybrush101/blob/master/Guybrush101/Core/EngineClassSwitch.cs#L536-L540

Notice that manipulating ISP means manipulating the curves.... That's fun stuff. I made this function to help me insert new curves, like atmCurve, atmosphereCurve etc.

https://github.com/WarezCrawler/Guybrush101/blob/master/GTI_Utilities/Utilities/GenericFunctions.cs#L139-L179

hope this helps. Using the MultiModeEngine lets you change any setting in engines. And if two modes is not enough for you, you can use 

It's work fine with KSP 1.2.2 as well..... I just played.

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...