Jump to content

The official unoffical "help a fellow plugin developer" thread


Recommended Posts

OnLoad is the proper time. Module are started in the loading screen and your code should check if it is actually attached to a vessel before doing anything that night break (also helps for the editor).

Link to post
Share on other sites
8 minutes ago, sarbian said:

OnLoad is the proper time. Module are started in the loading screen and your code should check if it is actually attached to a vessel before doing anything that night break (also helps for the editor).

Well, I managed to modify stuff so that OnLoad works for populating GetInfo. But now I have a different problem. I load the sub-nodes into a List in the OnLoad method. But that list is empty when I actually use the part in the editor. It looks like OnLoad isn't called when the module is instantiated.

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

Well, I managed to modify stuff so that OnLoad works for populating GetInfo. But now I have a different problem. I load the sub-nodes into a List in the OnLoad method. But that list is empty when I actually use the part in the editor. It looks like OnLoad isn't called when the module is instantiated.

You probably want to read this for some background

Now, the problem you're running into with the value of that field disappearing is Unity serialization - when the part prefab is instantiated your field isn't copied over.  I should really do a post about this some time.  There aren't really any simple general-purpose solutions, just annoying workarounds.  Here's one.

2 minutes ago, garwel said:

Um... Tripleposted... Now, how do I delete a post?

Report your own post and a moderator will remove it.

Link to post
Share on other sites

Thanks for clearing it up @blowfish. I was afraid there is something more to it than just me not using the proper methods or attributes, and it seems to be true. I guess this is why we have mods like B9 Part Switch that do stuff, which should've been trivial. In my case, I guess I'll do without the custom sub-nodes, though it won't be as neat.

Link to post
Share on other sites
  • 3 weeks later...

I'm looking to retrieve all science data upon recovery of vessel/transmission of science and then get info as to what CelestialBody the science came from. I'm thinking I can accomplish this using "OnScienceRecieved" or "onVesselRecoveryRequested/OnVesselRecoveryProcessing", but I'm not sure how to extract those parameters. Can anyone give me any advice?

EDIT: I figured it out. Here's the code in case anyone else needs.

		private void Start()
        {
            GameEvents.OnScienceRecieved.Add(ScienceProcessingCallback);
        }

        private void OnDestroy()
        {
            GameEvents.OnScienceRecieved.Remove(ScienceProcessingCallback);
        }

        void ScienceProcessingCallback(float sciValue, ScienceSubject sub, ProtoVessel pv, bool test)
        {
            Debug.Log("Science Data: " + sub.title);
            Debug.Log("Science Value: " + sub.science);
			if (celestialBodyName in sub.title)
			{
				//code here
			}
        }

 

EDIT2: Second question: How can I retrieve data from ConfigNode? Here's what I have so far:

public override void OnSave(ConfigNode node)
{
	string oldValue = 0.ToString();
    key = name;
    if (node.Contains(key))
    {
    	value = node.GetValue(key);
        oldValue = float.Parse(value);
        newValue = oldValue + addedValue;
        node.SetValue(key, newValue);
        addedValue = 0;
    }
    else
    {
        node.AddValue(key, addedValue);
    }

Using debugging, it doesn't get past the "if node.Contains(key)" , but if I don't use the if statement, then it crashes at the "GetValue(key)" code. I've been looking at other people's code and looking for online tutorials, but there always seems to be something missing, either in their example or in my brain.

Edited by Codapop
Link to post
Share on other sites
  • 1 month later...

Hi. Hoping someone can help.  Am new to KSP/Unity/C#, but a full time programmer elsewhere.

  1. Does a KSP plugin have access to the data in the Archives tab of the R&D building? The science history data specifically. I combed over the docs for the Game object, no luck. Tried to come up with creative guesses for elsewhere, but here I am.
  2. What is the correct name for this contruct so I can investigate further:   [KSPAddon(KSPAddon.Startup.SpaceCentre, false)]

Thank you!

Link to post
Share on other sites
13 minutes ago, OrbitalManeuvers said:
  1. Does a KSP plugin have access to the data in the Archives tab of the R&D building? The science history data specifically. I combed over the docs for the Game object, no luck. Tried to come up with creative guesses for elsewhere, but here I am.

Usually the easiest way to answer a question like this is to find a mod that already does it and then check the source to see how. I think ForScience accesses that data, possibly here?

13 minutes ago, OrbitalManeuvers said:
  1. What is the correct name for this contruct so I can investigate further:   [KSPAddon(KSPAddon.Startup.SpaceCentre, false)]

Those are C# attributes; they allow the compiler to associate additional metadata with a class or function, that can later be queried by other code. KSP uses the one you quoted to find classes that should be treated as entry points for mods.

Link to post
Share on other sites

Noob questions. Brand new to C#. I can already tell it's gonna be the "paperwork" (scaffolding/config/options) stuff that's going to kill me long before the actual coding part. Anyway, here's my situation...

My DLL gets found by KSP but generates this: Could not load file or assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies.

My observation is that when I load other people's projects, VS shows a bold solution node, and under there is always a child node called References, and under there are all the referenced assemblies. But, when I start a new VS project and pick Class Library (.NET Standard) I get a bold solution node, under which there is Dependencies, with children of Assemblies and SDK (no References node anywhere). Under SDK is a node referencing NetStandard.Library (2.0.3). Doesn't seem to be a way to delete this node or make my explorer look like other people's mods, so I must be doing something basic wrong.

Is this a wrong project type? Or ... ?

 

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

Could not load file or assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies.

But, when I start a new VS project and pick Class Library (.NET Standard)

I don't know what it looks like in Visual Studio, but you need to target .NET Framework, not ".NET Standard", which is a newer thing that Microsoft made up to confuse us. It might be worth comparing csproj files in a text editor.

Link to post
Share on other sites
13 hours ago, OrbitalManeuvers said:

Noob questions. Brand new to C#. I can already tell it's gonna be the "paperwork" (scaffolding/config/options) stuff that's going to kill me long before the actual coding part. Anyway, here's my situation...

My DLL gets found by KSP but generates this: Could not load file or assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies.

My observation is that when I load other people's projects, VS shows a bold solution node, and under there is always a child node called References, and under there are all the referenced assemblies. But, when I start a new VS project and pick Class Library (.NET Standard) I get a bold solution node, under which there is Dependencies, with children of Assemblies and SDK (no References node anywhere). Under SDK is a node referencing NetStandard.Library (2.0.3). Doesn't seem to be a way to delete this node or make my explorer look like other people's mods, so I must be doing something basic wrong.

Is this a wrong project type? Or ... ?

 

This article may help:
https://code-maze.com/differences-between-net-framework-net-core-and-net-standard/

 

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