Jump to content

The official unoffical "help a fellow plugin developer" thread


Recommended Posts

Well, I don't think the issue is accessing Unity methods from constructors (we just don't access Time.realtimeSinceStartup from constructors). The only thing KSP complains about is about using Time.realtimeSinceStartup on a thread function (we spawn a thread and inside this thread's main function we call Time.realtimeSinceStartup). In case the source code helps, take a look: https://github.com/godarklight/DarkMultiPlayer/blob/b1bf1ef3d3850ffa33c624435ba0ee079f8aee73/Client/NetworkWorker.cs#L210-L307
 

Link to comment
Share on other sites

On 12/13/2016 at 10:59 PM, nightingale said:

@DMagic - looks like I inadvertently broke this will adding rounding in to fix another issue.  I tried to be clever and round using the display format, which doesn't work with the P0 display format (because Float.Parse() doesn't have smarts to recognize a percentage).

As a workaround, try using N1 + asPercentage.

@nightingale @DMagic 

Could you give an example? I added "displayFormat = N2" and now have the following minus the toolTip:

[GameParameters.CustomFloatParameterUI("Minimum Zoom", displayFormat = "N2", asPercentage = true, minValue = 0.05f, maxValue = 0.6f)]
        public float zoomMin = 0.35f;

This lets me move the slider and adjust the ones place (35.00%, 36.00%, 37.00%), but displays two decimal points that I don't want. N1 shows only one decimal place but only lets me adjust the tens place (30.0%, 40.0%), and N0 doesn't let me adjust anything but has no decimals. Is there a way to adjust the ones place without displaying decimals anymore?

Link to comment
Share on other sites

@ev0 - Ah, that's not a good workaround then.  Okay, new plan - use a property:

public float zoomMin = 0.35f;

[GameParameters.CustomFloatParameterUI("Minimum Zoom (%)", displayFormat = "N0", asPercentage = false, minValue = 5f, maxValue = 60f)]
public float zoomMinParam {
    get { return zoomMin * 100; }
    set { zoomMin = value / 100.0f; }
}

Downside of that approach is that the properties and fields will appear out of the order you expect (blame C#).  Workaround for *that* would then be to make everything a property.

Link to comment
Share on other sites

51 minutes ago, nightingale said:

@ev0 - Ah, that's not a good workaround then.  Okay, new plan - use a property:


public float zoomMin = 0.35f;

[GameParameters.CustomFloatParameterUI("Minimum Zoom (%)", displayFormat = "N0", asPercentage = false, minValue = 5f, maxValue = 60f)]
public float zoomMinParam {
    get { return zoomMin * 100; }
    set { zoomMin = value / 100.0f; }
}

Downside of that approach is that the properties and fields will appear out of the order you expect (blame C#).  Workaround for *that* would then be to make everything a property.

That works, thanks. Wish I could have kept the percent sign after the number though:(

Link to comment
Share on other sites

  • 2 weeks later...

I believe KSP keeps a list of the possible Kerbal types (i.e. Pilot/Engineer/Scientist) within the vanilla game that is used to generate new Kerbals (such as by KerbalRoster.GetNewKerbal(Crew).  Using mods that add in additional types (such as RoverDude's USI collection or maculator's Colonists! mod), it looks like KSP updates the record of the possible types when rolling random Kerbals.  That is, types such as Geologist or Quartermaster appear in addition to Pilot and Engineer.

Does anyone know where that information would be stored and how to access it?  The KerbalRoster and HighLogic classes don't have anything that is obvious.

Some background regarding my application:

I want to be able to convert Kerbals from one occupation to another, but I'm not sure what mods someone may or not be using.  For example, I want to change a Scientist to something else.  In vanilla, the only options would be Pilot/Engineer.  But if there are mods present, something like Geologist would be available, so Pilot/Engineer/Geologist.

I think there are two approaches, but I'm not sure which is best:

1)  Create a Module Manager plugin to add/subtract classes called in a module.  This options seems like more overhead for me but is definitely doable with what I presently know.

2)  Implement the logic to check the available Kerbal traits in C# so I don't need to explicitly know what traits are being used (I can delegate it to an iterative loop).  This seems like the more "correct" solution, but I'm not sure if it's possible.

Thank you!

Link to comment
Share on other sites

Messing around with trying to update some mods.  I see that the StrutConnector class is no longer present, but don't see anything in the docs about it's replacement.  Any idea as to how to tell if a part is a strut connector?

Link to comment
Share on other sites

Just now, Diazo said:

Pretty sure that there is a dedicated partMoudule for that, it's CModuleStrut or something I think?

D.

Ok, i saw that.  The code I was looking at has if (part is StrutConnector) {...}, but it seems a part can never be a CModuleStrut.  I'm just getting my feet wet in all this stuff, so I may need to do more digging to find out the difference between a Part and a PartModule

Link to comment
Share on other sites

Currently a Part is the actual object in the game world takes care of the model, collisions, rendering on-screen, etc.

Then that Part is assigned different PartModules depending on what functionality you want. There is no limit to the number of partModuls a part can have which is how you get multi-function parts.

The (part is StrutConnector) you are referring to is the old system where there were only Parts, no partModules, so a Part could only be one thing. This was a crippling limitation and so has been replaced by the partModule system where a Part is a Part, functions are enabled by giving it partModules.

D.

Link to comment
Share on other sites

Ok, so I bet I can do something like this: if (part.FindModulesImplementing<CModuleStrut>().Count > 0) {...}.  The old code was calling BreakJoint() on the StrutConnector, however I don't see anything like that on the part, nor the module.  Perhaps we don't need to do that anymore?

Link to comment
Share on other sites

Struts use a special kind of Part derivative: CompoundPart. And they use the PartModules found in the CompoundParts namespace, one of which is CModuleStrut.

They are actually pretty easy to work with and have a nice method for terminating their connection: OnTargetLost().

Link to comment
Share on other sites

4 hours ago, DMagic said:

Struts use a special kind of Part derivative: CompoundPart. And they use the PartModules found in the CompoundParts namespace, one of which is CModuleStrut.

They are actually pretty easy to work with and have a nice method for terminating their connection: OnTargetLost().

I would think a method with the name OnTargetLost would be an event handler that would fire when the connection was terminated, and not actually cause the connection TO terminate. 

Link to comment
Share on other sites

So I followed this thread to create my first KSP mod:

and I use this Hello World code from that thread:

using System;
using UnityEngine;

namespace HelloWorld
{
	[KSPAddon(KSPAddon.Startup.Flight, false)]
	public class Hello : MonoBehaviour
	{
		public void Update()
		{
			Debug.Log("Hello world! " + Time.realtimeSinceStartup);
		}
	}
}

as MyClass.cs and compiled it using this Makefile:

cmdlist = -t:library -lib:/home/feanor/Development/Programming/KSP_Modding/Managed -reference:Assembly-CSharp.dll -reference:Assembly-CSharp-firstpass.dll -reference:UnityEngine.dll -reference:UnityEngine.UI.dll
dlllist = MyClass.dll
cslist = MyClass.cs
ksp_dir = "/home/feanor/Development/Programming/KSP_Modding/Kerbal Space Program"
all:
	mcs $(cmdlist) $(cslist)

clean:
	rm $(dlllist)

install:
	cp $(dlllist) $(ksp_dir)/GameData

run:
	$(ksp_dir)/KSP.x86_64

It successfully compiles the file, and KSP successfully loads the .dll file

feanor@silmaril ~/.c/u/S/Kerbal Space Program> grep MyClass Player.log 
Load(Assembly): /MyClass
AssemblyLoader: Loading assembly at /home/feanor/Development/Programming/KSP_Modding/Kerbal Space Program/GameData/MyClass.dll
Non platform assembly: /home/feanor/Development/Programming/KSP_Modding/Kerbal Space Program/GameData/MyClass.dll (this message is harmless)
MyClass v0.0.0.0
MyClass.dll

5jC1pRu.png

 According to the thread, this plugin should spam the debug log with Hello World! messages, but there is no message at all:

GW1scwc.png

What is wrong then with my setup?

 

Edited by Aghanim
Link to comment
Share on other sites

@nightingale It works!

oSf4vV6.png

So the guide is misleading, because it states this:

Quote

When you start KSP and reach the main menu, if everything is setup correctly, the code above will spam "Hello world!" to the console which is toggled on with Alt-F12. It will also append the time since the game was started so you can see the lines of text change.

 

 

Edited by Aghanim
Link to comment
Share on other sites

5 hours ago, Aghanim said:

as MyClass.cs and compiled it using this Makefile:

Be aware that this will compile as a .NET 4.5 lib. You should add "-sdk 2", "-nostdlib" and reference the mscorlib.dll shipped with KSP. Not sure how recent mono will deal with that...

Link to comment
Share on other sites

2 hours ago, JuhaJGamer said:

Sorry if this has been asked before, how to add particle effects to modules in C#

If you have an effect defined in the part's EFFECTS node, you can cause it to play in a module by calling part.Effect(effectName, power) where effectName is the name of the effect in the EFFECTS node and power is a float from 0 to 1 indicating how strongly the effect should be played.

Link to comment
Share on other sites

16 minutes ago, blowfish said:

If you have an effect defined in the part's EFFECTS node, you can cause it to play in a module by calling part.Effect(effectName, power) where effectName is the name of the effect in the EFFECTS node and power is a float from 0 to 1 indicating how strongly the effect should be played.

Thank you, I'm just starting. Also im trying to use ConfigNode but ksp just doesn't like it: System.IO.FileNotFoundException: Could not load file or assembly 'KSPUtil, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
File name: 'KSPUtil, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'

Link to comment
Share on other sites

1 minute ago, JuhaJGamer said:

Thank you, I'm just starting. Also im trying to use ConfigNode but ksp just doesn't like it: System.IO.FileNotFoundException: Could not load file or assembly 'KSPUtil, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
File name: 'KSPUtil, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'

Remove the reference to KSPUtil.  Everything that was in that DLL was moved back into Assembly-CSharp.

Link to comment
Share on other sites

Has anyone got any idea of how to make a vessel rendezvous with another vessel, and then set the velocity relative to the target at ~50m/s. Rendezvous in kind of like the in-game cheat rendezvous which sets the vessel 150m away from the target.

Edited by JuhaJGamer
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...