Jump to content

The official unoffical "help a fellow plugin developer" thread


Recommended Posts

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?

Link to comment
Share on other sites

  • 2 weeks later...

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!
Link to comment
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
Link to comment
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?

Link to comment
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.

Link to comment
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
Link to comment
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:

Link to comment
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;
}

 

Link to comment
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;

 

Link to comment
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

Link to comment
Share on other sites

Hi, what exactly is "World Space" and how does it relate to the position vectors in KSP?

Specifically, to FlightGlobals.currentMainBody.position and FlightGlobals.ActiveVessel.transform.position ?

I'm trying to draw some lines using Vectrosity from a set of coordinates. That worked using the Unity.LineRenderer, but when I try to use  the very same coordinates for rendering of Vectrosity.VectorLine, the lines go out straight out of the screen and behave weirdly when rotating the camera.

ps.: Is there some sort of "reference frame guide" for KSP? I keep getting confused by World/Body-Relative/Krakensbane/Surface-Relative and all kinds of coordinate systems.

Link to comment
Share on other sites

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