Jump to content

The official unoffical "help a fellow plugin developer" thread


Recommended Posts

Having an issue trying to create a config setting for controls.

Any C#/Unity gurus out there who have any idea why the following code gives me a 'Enum.TryParse - method not found' exception at runtime?


KeyCode reviewData;
if (Enum.TryParse(cfg.GetValue("storedDataKey"), out reviewData)
{
return reviewData;
}

It's inside a method that is called during flight scene Awake(), to set the keybinding. Visual studio has no problems, it compiles just fine. I get the error in game however.

Link to comment
Share on other sites

Anyone know how I can go about accessing the biome name where a vessel is located?

The solution to the problem in Rhidian's answer is that you have to report lat and long in radians not degrees. So you need something like:


public void BiomeCheck()
{
print(FlightGlobals.currentMainBody.BiomeMap.GetAtt(vessel.latitude * Mathf.Deg2Rad, vessel.longitude * Mathf.Deg2Rad).name);
}

That will return the correct biome, though you won't get anything for the KSC, just shores (maybe there is some specific area that will return KSC, but I didn't find it).

Link to comment
Share on other sites

Chronothan: which framework level are you compiling for?

MrHappyFace: HighLogic.CurrentGame?

I am embarrassed to admit this. But I have no idea what that means :blush:

I am working on this to learn C# and modding in general. This is my first actual project so I'm learning as I go. Basically, I'm becoming a master at restarting KSP.

Here is the full method:

       
public KeyCode checkKeys()
{
checkConfig();//Makes sure the config exists. Creates it if it doesn't.

KeyCode key;
if (!Enum.TryParse(cfg.GetValue("reviewDataKey"), out key))
{
cfg.SetValue("reviewDataKey", "Backslash");
cfg.Save("GameData/ForceIVA/IVAoptions.cfg");
Debug.Log("Make sure to use the list of keys to set the key! Reverting to backslash");
return KeyCode.Backslash;
}
else
{
return key;
}

}

I have two classes, the main ForceIVA class, and the ConfigUtil class that this method is located in. The ForceIVA class instantiates ConfigUtil and calls this method in Awake().

ForceIVA class itself is instantiated in at flight mode startup.

Link to comment
Share on other sites

ok what im i doing wrong?. C++ will not compile my code, it just gives me a list of errors.

I even tried to compile code from an open source mode, KSPI and it still gave me the errors

here is a list

2 7 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected nested-name-specifier before 'System'

2 7 G:\Games\kerbal space\ModulePrecooler.cpp [Error] 'System' has not been declared

3 1 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected ';' before 'using'

3 7 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected nested-name-specifier before 'System'

3 7 G:\Games\kerbal space\ModulePrecooler.cpp [Error] 'System' has not been declared

3 13 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected ';' before '.' token

3 13 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected unqualified-id before '.' token

4 7 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected nested-name-specifier before 'System'

4 7 G:\Games\kerbal space\ModulePrecooler.cpp [Error] 'System' has not been declared

4 13 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected ';' before '.' token

4 13 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected unqualified-id before '.' token

5 7 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected nested-name-specifier before 'System'

5 7 G:\Games\kerbal space\ModulePrecooler.cpp [Error] 'System' has not been declared

5 13 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected ';' before '.' token

5 13 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected unqualified-id before '.' token

6 7 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected nested-name-specifier before 'UnityEngine'

6 7 G:\Games\kerbal space\ModulePrecooler.cpp [Error] 'UnityEngine' has not been declared

9 40 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected class-name before '{' token

10 9 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected unqualified-id before '[' token

13 19 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected ':' before 'bool'

13 37 G:\Games\kerbal space\ModulePrecooler.cpp [Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]

14 19 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected ':' before 'ModuleResourceIntake'

14 19 G:\Games\kerbal space\ModulePrecooler.cpp [Error] 'ModuleResourceIntake' does not name a type

16 16 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected ':' before 'override'

16 16 G:\Games\kerbal space\ModulePrecooler.cpp [Error] 'override' does not name a type

30 16 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected ':' before 'override'

30 16 G:\Games\kerbal space\ModulePrecooler.cpp [Error] 'override' does not name a type

38 16 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected ':' before 'override'

38 16 G:\Games\kerbal space\ModulePrecooler.cpp [Error] 'override' does not name a type

47 16 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected ':' before 'bool'

50 5 G:\Games\kerbal space\ModulePrecooler.cpp [Error] expected ';' after class definition

32 G:\Games\kerbal space\Makefile.win recipe for target 'ModulePrecooler.o' failed

and code, This is going to be a huge post (stolen from KSPI)

 

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

namespace PRPlugin {
class ModulePrecooler : PartModule {
[KSPField(isPersistant = false, guiActive = true, guiActiveEditor = true, guiName = "Precooler status")]
public string statusStr;

protected bool functional = false;
protected ModuleResourceIntake attachedIntake;

public override void OnStart(PartModule.StartState state) {
if (state == StartState.Editor) { return; }

foreach (AttachNode attach_node in part.attachNodes) {
if (attach_node.attachedPart != null) {
List<ModuleResourceIntake> mres = attach_node.attachedPart.FindModulesImplementing<ModuleResourceIntake>().Where(mre => mre.resourceName == "IntakeAir").ToList();
if(mres.Count > 0) {
attachedIntake = mres.First();
}
}
}
part.force_activate();
}

public override void OnUpdate() {
if (functional) {
statusStr = "Active.";
} else {
statusStr = "Offline.";
}
}

public override void OnFixedUpdate() {
functional = false;
if (attachedIntake != null) {
if (attachedIntake.intakeEnabled) {
functional = true;
}
}
}

public bool isFunctional() {
return functional;
}
}
}

what is going ON?

Link to comment
Share on other sites

To confirm, you have added the two dependencies you need so that the compiler knows where System and UnityEngine can be found?

These two dependencies are what I'm talking about:

KSP_INSTALL_FOLDER\KSP_Data\Managed\Assembly-CSharp.dll

KSP_INSTALL_FOLDER\KSP_Data\Managed\UnityEngine.dll

They need to be linked as References in your IDE.

D.

Edited by Diazo
Link to comment
Share on other sites

@tibus

The code you have posted isn't c++! It is C#, like most other mods you will find around the KSP forum. So make sure you are using a C# compiler (like Visual Studio Express for Windows Desktop) and create a C# project (Class Library) or use a C++ dev environment that can create CLI assemblies (VS 2008 can, CLR => class library) and follow the language specifics of that environment!

Link to comment
Share on other sites

@tibus

The code you have posted isn't c++! It is C#, like most other mods you will find around the KSP forum. So make sure you are using a C# compiler (like Visual Studio Express for Windows Desktop) and create a C# project (Class Library) or use a C++ dev environment that can create CLI assemblies (VS 2008 can, CLR => class library) and follow the language specifics of that environment!

WHAT?!

but it says C++ on my aptly named devC++ portable hmmm ill try it.

Link to comment
Share on other sites

Is is possible to transfer crafts between different saves of the game? Or create a new save with a plugin? I'm thinking of some ideas for a possible mod, and switching between saves would make or break it.

I havent done exactly this myself, but I'm pretty sure the save file format is a standard ConfigNode structure so yes you could do this. You'd want to be careful not to corrupt the save of course :), but should be possible.

Link to comment
Share on other sites

Okay, what I'm pretty sure is a simple question.

I want to check for the presence of another mod and run some code if it exists, but I don't actually call the mod itself so I don't want to make a dependency.

So, check to see if the mod's .dll file exists. Except I can't get this to co-operate.

Here's what I'm running:

if (KSP.IO.File.Exists<File>(KSPUtil.ApplicationRootPath + "GameData/KAS/Plugin/KAS.dll"))
{
print("KAS Detected!");
}
else
{
print("KAS Not Detected");
}

and it prints "KAS Not Detected" to the debug log so the code is executing, but I can't make it find the KAS.dll file.

Instead of KSPUtil.ApplicationRootPath + "GameData/KAS/Plugin/KAS.dll" I've also tried:

"KAS/Plugin/KAS"

"KAS/Plugin/KAS.dll"

trying to use the loader that catches images files and stuff and no go there either.

I'm 99% sure it's a simple syntax thing that I've got wrong but google is no help (I don't know the right term to be searching for) and the C# File.Exists command is different enough from the KSP.IO.File.Exists<T> command that references to that are not helping me.

So, anyone able to help please?

Thanks,

D.

Link to comment
Share on other sites

One of the reasons I tried to go with a File.Exists was so I would not have to worry about loaded or not.

Although I suppose doing a File.Exists check does not tell me if it loaded correctly or not, the file could be there and KAS has failed to load.

Type.GetType is probably a better idea. If I put it in my FlightScene Awake module I think I can guarantee that KAS is loaded (if it's going to load) by the time my Awake code runs.

Thanks for the idea Faark.

D.

edit: Hun. Apparently KSP 0.21 removed the System.IO restriction. I'll probably still go with the GetType method, but apparently System.IO.File.Exists would have worked. Source

Edited by Diazo
Link to comment
Share on other sites

I've used both file exists and the GetType option, they will both work. Personally for checking for a Mod I think Faark has the best idea, especially as you have no guarantee the player will install KAS into the same place :) . The assemblies all get loaded in the loading scene so they will be there when awake is called

Link to comment
Share on other sites

For reference, I used the AssemblyLoader.loadedAssemblies list.

More exactly:

foreach (AssemblyLoader.LoadedAssembly Asm in AssemblyLoader.loadedAssemblies)
{
if (Asm.dllName == "KAS")
{
TWR1KASDetect = true;
}
}

Detects if KAS is loaded or not.

Asm.name also exists, but while both are unlikely to change, I figure the .dllname is less likely to change then the .name

D.

Link to comment
Share on other sites

Hello all,

I'm really hoping I can find a Mac Modder out there that can fork this plugin..

I think the original idea was very cool, but unfortunately the creator is unable to made a Mac version of the plug in.

Can anyone help?

http://forum.kerbalspaceprogram.com/...l-%2804-Feb%29

Source code for the plugin

https://github.com/zitron-git/KSPSerialIO

Link to comment
Share on other sites

Minor issue, but has anyone figured out how to set the title for a module in the new extended right-click-to-display part description pane?

For example, all my custom modules automagically grab the title from the class name and it results in stuff like this:

s7jz.png

Now, I'd really prefer not to alter my mod's class naming convention just to alter the string that's displayed as a title, but so far I'm having no luck in changing it through any other method.

Also, as a secondary question, does anyone know the format codes to display parts of the GetInfo() string in green or what have you? You'll notice above that "Requires:" is written in the default white, but for all stock parts, it's displayed in green. For the sake of consistency, I'd like to be able to do so as well if possible.

EDIT: Figured out the second part. For reference, debug outputting the GetInfo() string for stock modules, gives stuff along these lines:

<b><color=#99ff00ff>Requires:</color></b>

From which the format codes can be extracted.

Still have no clue on the title part though :)

Edited by FlowerChild
Link to comment
Share on other sites

@flower

Looks like RT is doing it via a KSPModule attribute. Also AvailablePart seems to have an moduleInfos & messing around with that could offer some neat features as well.

Thanks Faark! Will take a look at the RemoteTech code base then.

EDIT: Yup, the following does the trick:


[KSPModule( "Life Support" )]
class BTSMModuleLifeSupport : PartModule
{
}

Thanks a million man. Would have taken me forever to figure that one out :)

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