Cephei

The official unoffical "help a fellow plugin developer" thread

Recommended Posts

9 hours ago, wasml said:

@MaxZhaoI notice two odd characters just before PART in your post of the file where you changed the cost - if this is in the actual file it could cause this issue. Did your text editor add some extra characters? maybe it didn't save in a basic ASCII format? This would be the only thing I can think of.

 

Yeah I did notice it, however, the problem persists even after I've removed the characters. I don't know what those are but they don't appear in any other part cfgs. 

Share this post


Link to post
Share on other sites

I'm Having a problem creating my own generator. I want a small generator that can take ore and electricity and turn it into either liquid fuel, oxidiser or mono propellant as well as store the excess. I am new at modding KSP. I have made a model and texture that work but for some reason my code is wrong. Every resource value I enter appears in the game as 0. 

For example:


    MODULE
    {
        name = ModuleResourceConverter
        ConverterName = Mono Propellant
        StartActionName = Start Mono Propellant
        StopActionName = Stop Mono Propellant
        AutoShutdown = false
        GeneratesHeat = false
        UseSpecialistBonus = false
        INPUT_RESOURCE
        {
            ResourceName = ElectricCharge
            Rate = 0.5
        }
        INPUT_RESOURCE
        {
            ResourceName = Ore
            Rate = 0.5
        }
        OUTPUT_RESOURCE
        {
            ResourceName = MonoPropellant
            Rate = 0.95
            DumpExcess = false
        }

Can anyone see a problem? I have tried coping parts of the coding from stock game parts.

Help! Please.

Full Code:

PART
{
    name = FuelGen2
    module = Part
    author = Brahimus
    rescaleFactor = 1
    node_attach = 0, 0, 0, 1, 0, 0, 0
    TechRequired = largeElectrics
    entryCost = 2250
    cost = 750
    category = Utility
    subcategory = 0
    title = Super Ore Converter
    manufacturer = Brahimus Inc.
    description = A small generator that can convert electricity and ore into liquid fuel, mono propellant and oxidizer.
    attachRules = 0,1,0,0,0

    // --- standard part parameters ---
    mass = 0.05
    dragModelType = default
    maximum_drag = 0.2
    minimum_drag = 0.2
    angularDrag = 2
    crashTolerance = 7000
    maxTemp = 7000

    MODEL
    {
        model = Squad/Parts/Utility/FuelCell2/FuelCell
    }
    MODULE
    {
        name = ModuleResourceConverter
        ConverterName = Liquid Fuel
        StartActionName = Create Liquid Fuel
        StopActionName = Stop Liquid Fuel
        AutoShutdown = false
        GeneratesHeat = false
        UseSpecialistBonus = false
    canOverflow = false
     deactivateIfFull = false
    deactivateIfEmpty = false 
        INPUT_RESOURCE
        {
            ResourceName = ElectricCharge
            Rate = 0.5
        }
        INPUT_RESOURCE
        {
            ResourceName = Ore
            Rate = 0.05
        }
        OUTPUT_RESOURCE
        {
            ResourceName = LiquidFuel
            Rate = 0.95
            DumpExcess = false
        }
    }
    MODULE
    {
        name = ModuleResourceConverter
        ConverterName = Oxidizer
        StartActionName = Start Oxidizer
        StopActionName = Stop Oxidizer
        AutoShutdown = false
        GeneratesHeat = false
        UseSpecialistBonus = false
    canOverflow = false
     deactivateIfFull = false
    deactivateIfEmpty = false 
        INPUT_RESOURCE
        {
            ResourceName = ElectricCharge
            Rate = 0.5
        }
        INPUT_RESOURCE
        {
            ResourceName = Ore
            Rate = 0.05
        }
        OUTPUT_RESOURCE
        {
            ResourceName = Oxidizer
            Rate = 0.95
            DumpExcess = false
        }
    }
    MODULE
    {
        name = ModuleResourceConverter
        ConverterName = Mono Propellant
        StartActionName = Start Mono Propellant
        StopActionName = Stop Mono Propellant
        AutoShutdown = false
        GeneratesHeat = false
        UseSpecialistBonus = false
    canOverflow = false
     deactivateIfFull = false
    deactivateIfEmpty = false 
        INPUT_RESOURCE
        {
            ResourceName = ElectricCharge
            Rate = 0.5
        }
        INPUT_RESOURCE
        {
            ResourceName = Ore
            Rate = 0.005
        }
        OUTPUT_RESOURCE
        {
            ResourceName = MonoPropellant
            Rate = 0.95
            DumpExcess = false
        }
    }
    RESOURCE
    {
        name = LiquidFuel
        amount = 100
        maxAmount = 100
    }
    RESOURCE
    {
        name = Oxidizer
        amount = 100
        maxAmount = 100
    }
    RESOURCE
    {
        name = MonoPropellant
        amount = 100
        maxAmount = 100
    }
}
 

Edited by Brahimus

Share this post


Link to post
Share on other sites

How does one create an input lock so that I can have the player left-click somewhere on the vessel in the editor but not have the part get picked up off the vessel? I've got it working when CTRL is held down (similarly to UbioZur Welding), but not without a modifier key. Any ideas?

Share this post


Link to post
Share on other sites

Hey fellow modders

I have been looking at a bit of code, and this is popping out at me: 

double Ec = GetResourceAmount("ElectricCharge");
double req = part.RequestResource("ElectricCharge", Ec);

When the variable req is used, would it automatically request all the electric charge on the vessel?

Edited by Benjamin Kerman
Please quote me so that I know you answered. Thanks!

Share this post


Link to post
Share on other sites

No. Once you assign to a variable, it just has that value. If you want that behavior, define a method as such:

	static double Req()
	{
	 return part.RequestResource("ElectricCharge", GetResourceAmount("ElectricCharge"));
	}
	
Edited by 0111narwhalz

Share this post


Link to post
Share on other sites
1 hour ago, 0111narwhalz said:

No. Once you assign to a variable, it just has that value. If you want that behavior, define a method as such:


	static double Req()
	{
	 return part.RequestResource("ElectricCharge", GetResourceAmount("ElectricCharge"));
	}
	

So the original bit of code I have, from https://github.com/Darknesshas1/DK-BHTanks/blob/master/src/DK-BHTanks/GameData/BlackHoleTanks/Plugins/BHStorageCfg.cs should work to draw a set amount of charge, as intended?

Share this post


Link to post
Share on other sites

Uh, no.

All you're doing by line 203 is saying "Hey! Look at this!" It doesn't re-execute line 192. You can imagine the chaos that might cause. For that, you need a method.

Share this post


Link to post
Share on other sites

@Benjamin Kerman

Just do the following in the class:

static double Req()
{
 return Part.RequestResource("ElectricCharge", GetResourceAmount("ElectricCharge"));
}

Then you call Req() to grab all the ElectricCharge. It also returns whatever Part.RequestResource() returns.

If you want to get fancy, try:

static double Req(string res)
{
 return Part.RequestResource(res, GetResourceAmount(res));
}

This will request whatever resource you name in res.

Share this post


Link to post
Share on other sites

:rolleyes:

Share this post


Link to post
Share on other sites

Some glitchy posts removed for clarity. 

Share this post


Link to post
Share on other sites

Hey @0111narwhalz if I could bother you (or anyone else :)) again...

Line 97 has Part.RequestResource(res, 0.0f)

This doesn't let me put BHECCost (in line 39) in where the 0.0f is. It throws a CS0120 error. 

Take a look?

Share this post


Link to post
Share on other sites

@Benjamin Kerman

Line 39: Add the static tag to BHECCost. You're trying to access a property of an object without an object. For the same reason, Req() has the tag. The only reason Start() (and the other Unity-related methods) doesn't have it is because Unity has this object (it's the GameObject, here a part).

Edited by 0111narwhalz

Share this post


Link to post
Share on other sites

Guys, open a thread please.

Share this post


Link to post
Share on other sites

One last thing, new dev here :wink: 

I successfully compiled the plugin with no errors (Yay!), but am not sure how to add it to the parts. Do I create a module in the cfg file called "ModuleBlackHole", or is there some other way? 

Thank you for all your help!

Share this post


Link to post
Share on other sites

Hello everyone :sticktongue:. I am making my first KSP mod, which simply displays a gui, with a label telling you how much science is stored on a vessel. However, I am stuck on trying to find out how much science is stored in each ModuleScienceContainer or ModuleScienceExperiment. Does someone know how this can be achieved, or is it impossible? Here is my code:

using System;
using System.Collections.Generic;
using UnityEngine;
using KspScienceCount.Extensions;
using KSP;

namespace KspScienceCount
{
	public class ScienceCount : PartModule
	{
		private Rect _windowPosition = new Rect();
		private GUIStyle _windowStyle, _labelStyle;
		private bool _hasInitStyles = false;

		public void OnGUI()
		{
			if (!_hasInitStyles) InitStyles();
			if (Event.current.type != EventType.Repaint || Event.current.isMouse)
			{
				if (this.vessel == FlightGlobals.ActiveVessel && this.part.IsPrimary(this.vessel.parts, this.ClassID))
					_windowPosition = GUILayout.Window(10, _windowPosition, OnWindow, "This is a title",_windowStyle);
			}

		}

		private void OnWindow(int windowId)
		{
			GUILayout.BeginHorizontal();
			GUILayout.Label("Science on this vessel: " + CountScience(), _labelStyle);
			GUI.DragWindow();
			GUILayout.EndHorizontal();
		}

		private void InitStyles()
		{
			_windowStyle = new GUIStyle(HighLogic.Skin.window);
			_windowStyle.fixedWidth = 150f;

			_labelStyle = new GUIStyle(HighLogic.Skin.label);
			_labelStyle.stretchWidth = true;

			_hasInitStyles = true;
		}

		private float CountScience()
		{
			float count = 0;
			foreach (Part rocketPart in this.vessel.parts)
			{
				foreach (PartModule module in rocketPart.Modules)
				{
					if (module.GetComponent<ModuleScienceExperiment>() != null)
					{
						for (int i = 0; i == module.GetComponent<ModuleScienceContainer>().GetData().Length - 1; i++)
						{
							//count += module.GetComponent<ModuleScienceExperiment>().GetData()[i]. <-- this bit here
						}
					}
						
				}
			}

			return count;
		}
	}
}

Many thanks in advance,

TheUltimateKerbonaut :cool:

Share this post


Link to post
Share on other sites

@TheUltimateKerbonaut Try this:

double ScienceValue(string subject_id, float size)
{
  ScienceSubject subject = ResearchAndDevelopment.GetSubjectByID(subject_id);
  if (subject == null) return 0.0f;
  return ResearchAndDevelopment.GetScienceValue(size, subject)
       * HighLogic.CurrentGame.Parameters.Career.ScienceGainMultiplier;
} 

float CountScience(Vessel v)
{
  float credits = 0.0f;
  foreach(IScienceDataContainer c in v.FindPartModulesImplementing<IScienceDataContainer>())   
  {
    foreach(ScienceData sd in c.GetData())
    {
      credits += ScienceValue(sd.subjectID, sd.dataAmount);
    }
  }
  return credits;
}

 

Share this post


Link to post
Share on other sites

I have a KSPField that I'm trying to only have show up in flight, however it seems to also be showing up in the editor, even after setting guiActiveEditor to false.  Does guiActive override this?

[KSPField(isPersistant = true, guiName = "Current EMR", guiActive = true, guiActiveEditor = false, guiUnits = ":1"),
  UI_FloatEdit(incrementSmall = 0.1f, incrementLarge = 1.0f, incrementSlide = 0.01f, sigFigs = 2, unit = ":1")]
public float currentEMR;

 

Share this post


Link to post
Share on other sites
15 minutes ago, rsparkyc said:

I have a KSPField that I'm trying to only have show up in flight, however it seems to also be showing up in the editor, even after setting guiActiveEditor to false.  Does guiActive override this?


[KSPField(isPersistant = true, guiName = "Current EMR", guiActive = true, guiActiveEditor = false, guiUnits = ":1"),
  UI_FloatEdit(incrementSmall = 0.1f, incrementLarge = 1.0f, incrementSlide = 0.01f, sigFigs = 2, unit = ":1")]
public float currentEMR;

 

You might have to set the scene on the UI_FloatEdit to UI_Scene.Flight

Share this post


Link to post
Share on other sites
3 minutes ago, blowfish said:

You might have to set the scene on the UI_FloatEdit to UI_Scene.Flight

That was it, thanks!

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