Jump to content

Referencing other mod's assemblies.


Recommended Posts

I'm trying to write an extension to another mod, but the only way I can reference the assembly and have it load is to place my dll in the same folder as the assembly I'm referencing. Is there any way I can reference it without it being in the same folder? When I tried to put my plugin in a different folder, it threw an exception and didn't function properly.

Link to comment
Share on other sites

One point of order is that this method only works if the mod you are depending on is present.

If you want your mod to be able to load and run without the mod you are referencing being present, you need to look into using Reflection.

While more complicated then the KSPAssemblyDependency method, it allows you mod to make calls to another mod, but still load if that mod is not present.

D.

Link to comment
Share on other sites

One point of order is that this method only works if the mod you are depending on is present.

If you want your mod to be able to load and run without the mod you are referencing being present, you need to look into using Reflection.

While more complicated then the KSPAssemblyDependency method, it allows you mod to make calls to another mod, but still load if that mod is not present.

D.

Diazo, might you be able to point me to an example of reflection being used? Don't quite understand the C# documentation with respect to how I would use it to say, grab variables from other partmodules.

Link to comment
Share on other sites

Getting a field reference (in this case, it gets cached), and then setting its value. Another example. Get the field reference (cached again), getting the value, setting the value.

Methods are similar. Get the method reference, and then invoke it.

Note: These are all aimed at FAR's wing or control surface modules

Edited by Crzyrndm
Link to comment
Share on other sites

Diazo, might you be able to point me to an example of reflection being used? Don't quite understand the C# documentation with respect to how I would use it to say, grab variables from other partmodules.

If you are looking at getting and setting KSPFields, Squad has done all the heavy lifting already.

bool valueBool = (bool)this.part.Modules["TheirPartModule"].Fields.GetValue("theirBool"); //assumes you are in a partModule on the same part as the partModule you want to reference, finds the first partModule named "TheirPartModule" and returns the "theirBool" value.
float valueFloat = (float)this.part.Modules["TheirPartModule"].Fields.GetValue("theirFloat"); //Finds the first partModule named "TheirPartModule" and returns the "theirFloat" value.

this.part.Modules["TheirPartModule"].Fields.SetValue("theirBool", true); //sets theirBool true
this.part.Modules["TheirPartModule"].Fields.GetValue("theirFloat", 1.5f); //sets theirFloat to 1.5

I make extensive use of this method here.

If you want to get/set a value that is not a KSPField, or execute methods in another partmodule, you have to do the reflection calls your self.

I use reflection extensively in my AGX mod, the code in AGX to receive reflection calls is found here in the External.cs file and the code needed in the other mod to make reflection calls can be found here. (Ignore the warning about being out of date, just make sure you are calling the correct method name as listed in External.cs.) Note that this example code makes extensive use of the "static" keyword to simply things, if you are trying to get/set/run a method in an instance of a class, things work a bit differently.

Hope that helps,

D.

Link to comment
Share on other sites

  • 2 weeks later...
Getting a field reference (in this case, it gets cached), and then setting its value. Another example. Get the field reference (cached again), getting the value, setting the value.

Methods are similar. Get the method reference, and then invoke it.

Note: These are all aimed at FAR's wing or control surface modules

Thanks for this! Really helped me with integrating FAR Support for my mod =)

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

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