Jump to content

In-game part welding


Recommended Posts

Hello everyone,

I got inspired by johnsonwax's blog posts regarding part welding (making a single part out of multiple modules) but found it difficult to manually weld parts together due to scaling differences between parts. In addition, this may not be possible in the future anymore as current development of KSP indicates that attachment nodes will be defined in the model and no longer in the config.

Initially I made an external tool to automate welding by reading all the part configs, reading the .craft file created from saving a vessel and generating a new part.cfg with all the parts welded into a single part.

However, as some part functionality would invariably become lost (unable to control multiple docking ports, transfer fuel between sub-parts) and to make things more user-friendly I wanted to make this an in-game option by creating a single dynamic part which renders and has physics properties like a single part but still allows you to control each sub-part individually. I've made some progress as you can see but I am stuck on a couple points:

- Part classes in the editor are incomplete and seem to be only proxies so most of the properties are null, making it impossible to get the actual model for that part.

- I can not for the life of me figure out how to create a part at runtime, the GameDatabase class holds a list of parts and so does the partloader. You seem to be able to remove parts but not create new ones.

I've tried looking at other mods such as SubAssemblies but that one seems to generate a .craft ConfigNode list from the Editor's Vessel class and write that to disk which is not very helpful to me. Sure, I can also traverse the ConfigNodes to get the information I need but they are basically just really thin wrappers and only hold strings. Most other mods I've seen that have arbitrary content (such as KerbTown) render meshes directly and do not create actual parts.

I've found a related thread titled Drawing "Ghosted" parts - Cloning Part Mesh Only (No Physics/Logic) which seemed promising but the solution to use GetComponent<MeshFilter>() as per Unity's documentation does not work since Parts in the Editor have no MeshFilter as they are (I think) just proxies.

Any input would be appreciated.

PS. Anyone figured out how to attach a debugger to KSP so breakpoints can be set? I assume it's impossible since there are no symbols for KSP.exe

NWRsEi2.jpg

Link to comment
Share on other sites

Hey,

I am actually working on the same thing,

There is no free way (that I know of) to have a debugger attach the game, and i didn't try the paid one, the full version of VS may be able to do it.

I have a intensive use of the Debug.Log to display all the information i need.

For the part info, I use a mix between what is in the part classes, and the configNode of the gamedata base.

Either C#, Unity or KSP have parser for lot of the possible type to and from string.

Here is a screen of a very early version (like a month or two ago), when I could only weld the structural parts (I now manage ressources, modules etc...)

BLOmiwY.png

Link to comment
Share on other sites

Nice work there Ubiozur, I assume you are parsing the ConfigNode data and saving the welded result to a part.cfg file and reloading the database? It is what my external tool was doing (though reloading was a manual thing). It works but there are downsides.

I would like to avoid parsing and use internal game data directly and not need to reload. Additionally, I'll need some way to create a dynamic part so when for example you weld two docking ports together the hit detection when you click on it will still allow you to control each docking port individually.

A basic part weld would in theory cause them to be treated as a whole and ModuleDockingNode would only dock/undock both of them. Might need some experimentation because it might be possible to define multiple ModuleDockingNode's with different referenceAttachNode values. But with the new way of defining attachment nodes in the model file itself, it will still be required to dynamically create and/or adjust the necessary attachment nodes.

Link to comment
Share on other sites

There is no free way (that I know of) to have a debugger attach the game, and i didn't try the paid one, the full version of VS may be able to do it.

This might go a little off topic, but since you guys mentioned it:

I don't think attaching VS to KSP will help. Sure, you can see native code of stuff like mono.dll, but this bundled is responsible for loading and executing your C# code and VS does not really have a way to handle that. Kind of sad that it does not somehow uses the MS .net runtime while on a win... heard it has a much more advanced JIT compiler. There has to be a MS/VS way to use .NET from native apps and debug that :(

On the other hand, debugging your own assembly for a pure .net application is easy. With a little workaround i could even use the free version of VS to debug a mod for another game.

My next attempt was finding a way to debug native apps that use mono. The few official docs did not really help me, even more since they are for an outdated version of monodevelop and the new replacement didn't especially like me^^

The more interesting part is soft debugging. We don't have a debug-KSP-Build, so getting that to work would be tricky and would likely require use to manipulate KSPs embedded mono runtime. Still it seems like the most promising approach...

It might be also possible to do it a Unity way. But for usual projects they appear to use a debug build (we don't have) and the soft debugger. Even then i haven't seen an easy way to start it without the unity solution. I also could not find any documentation on how to make and debug a mod for an unity game, what is kind of a shame for such an advanced game engine. I'm sure we are not the only community with those problems. Also sad that that there isn't any help from the KSP devs.

Anyone reading this might figured sth out i missed? Getting proper debugging to work would be awesome.

Link to comment
Share on other sites

In relation to debugging, I just tried UnityVS and it's no dice (Using full version of VS2012 btw). As far as I can tell Unity only supports soft debugging and since we have a release build and no project file this is simply not going to happen. I'm kinda surprised that someone (Unity) wants their framework so much locked down you can't even attach a simple debugger. I never had any problems doing that with native apps unless there was some anti-debugger code deliberately added for copyright reasons.

In any case, I found this Unity c# interpreter console which is better than nothing. At least you can try things out and do debug prints without having to reload KSP all the time.

Link to comment
Share on other sites

Nice work there Ubiozur, I assume you are parsing the ConfigNode data and saving the welded result to a part.cfg file and reloading the database? It is what my external tool was doing (though reloading was a manual thing). It works but there are downsides.

I would like to avoid parsing and use internal game data directly and not need to reload. Additionally, I'll need some way to create a dynamic part so when for example you weld two docking ports together the hit detection when you click on it will still allow you to control each docking port individually.

A basic part weld would in theory cause them to be treated as a whole and ModuleDockingNode would only dock/undock both of them. Might need some experimentation because it might be possible to define multiple ModuleDockingNode's with different referenceAttachNode values. But with the new way of defining attachment nodes in the model file itself, it will still be required to dynamically create and/or adjust the necessary attachment nodes.

Yeah I do use a mix of in game value and ConfigNode parsing, because not everything is available with the in game classes (well it's easier to get the cfg modules through the ConfigNode than through the PartModule variable).

I know it is possible for one part to have several docking module to several nodes. the hardest part is that the undock button don't tell which port it is.

Faark, it's not off topic since it's been asked by xtremeqg. Have anyone tried .NET Reflector, I have a friend who use that at his work when working with third party middleware.

Link to comment
Share on other sites

Okay, but the title isn't really perfect to later recover a discussion about debugging...

Reflector is a decompiler.

- Decompiling nearly never helps to fix bugs in your mods, since we have the source anyway.

- Decompiling is prohibited by the rules, so ofc non of us would ever do it.

- If someone would nonetheless, he would ofc use the free counterpart ILSpy instead^^

In any case, I found this Unity c# interpreter console which is better than nothing. At least you can try things out and do debug prints without having to reload KSP all the time.

Interesting, thought about creating sth like that myself. But you still don't have a trace and cannot access local scopes, what makes it not very helpful. I also thought about programmatically insert a bunch of calls to a custom debugger func, but creating sth like that would be a huge project by itself... all just to simulate a debugger :(

UnityVS looks interesting. Anyway, i wonder what would be necessary to make that mono runtime debug-able. Maybe all we need to do is to replace the mono.dll or build a wrapper for it?

Link to comment
Share on other sites

Anyway guys, I didn't know it was a decompiler and that's why I did offer that, but since it's one, and it's against the rules, let's not talk about any other decompiler possibilities.

Part.FindModelComponents<MeshFilter>();

humm I will have to try that and see if it's a better way to get the model info.

Link to comment
Share on other sites

I trust you are aware that the ingame debug log has a verbose mode. in settings.cfg set:

VERBOSE_DEBUG_LOG = True

I'm not entirely sure this is what you'd be looking for, but at least it's been a lot of help with tracking exceptions to their culprit methods at runtime for me.

oh, and regarding decompilers:

Planet+of+the+Apes+1968+Hear+No+Evil+See+No+Evil+Speak+No+Evil+Three+Wise+Monkeys+3+Body+Language+Nonverbal+Communication+Body+Language+Expert+Speaker+Expert+Speaker+Las+Vegas.jpg

Edited by JDP
Link to comment
Share on other sites

Part.FindModelComponents<MeshFilter>();

humm I will have to try that and see if it's a better way to get the model info.

I have already tried that, but parts in the Editor seem to be proxies and have no mesh as I stated on the start of this topic. Parts from GameDatabase might have them but since I cannot figure out how to index them properly (it is expecting an url and the current vessel's parts do not have that property or anything remotely related to allow construction of it) I have no way of obtaining the meshes other than going through the ConfigNodes.

So I had a little fun last night creating some multipart station trusses and performed a docking test near the runway...

rfHzfhv.png

Unfortunately it seems that there are some limitations to the game. When trying to actually drive the wheels glitch up and their power drain becomes NaN. Doing a quick time acceleration seems to fix it though. It think it is related to the Fustek Karmony modules having light animations and the engine not being able to handle multiple lights and/or animations on a single part. I tried replacing ModuleLight with ModuleAnimateGeneric which does allow the window lights to function properly but leaves some ugly toggle and state:locked garbage in the context menu. Removing the light definitions altogether caused the game to spew errors on every frame regarding NullPointers hrm.....

IOXmHbN.png

Yes, only 16 parts for a +50 tonne station (2 trusses, 8 wheels, 2 cupolas, 2 docking ports and 2 testing wheels stuck to the cupola to figure out that NaN issue). I left the tanks intentionally empty as it would be a total pig to launch fully fueled and doing a simple refueling run requires little effort.

z6rDgJZ.png

Somehow there are also periodic GUI glitches such as the break button and throttle indicator disappearing which might be shader or light related, I do not know. Suggestions are welcome as these issues are stopping a welding mod from becoming truly awesome.

Offtopic: The quote button seems to be broken so I just did it manually.

Edited by xtremeqg
Link to comment
Share on other sites

Offtopic: The quote button seems to be broken so I just did it manually.

Jop, since ajax is off. Well see what they do after the announced forum update. But you can middle-mouse it to open it in a new window.

In any case, I found this Unity c# interpreter console which is better than nothing. At least you can try things out and do debug prints without having to reload KSP all the time.

Did you successfully use it? It appears that it cannot load, since System.CodeDom, Microsoft.CSharp and maybe even more is missing in KSP's mono environment. We would have to provide them with the our mod as well... especially since KSP does not tell use many details about whats wrong... :(

Link to comment
Share on other sites

I have already tried that, but parts in the Editor seem to be proxies and have no mesh as I stated on the start of this topic.

If those parts had no meshes, their MeshRenderers wouldn't be rendering anything.

Build this and watch the chaos unfold:

using UnityEngine;

namespace MeshFilterEditorTest
{
[KSPAddon(KSPAddon.Startup.EditorAny, false)]
public class MessWithEditorMeshes : MonoBehaviour
{
public void Start()
{
GameEvents.onPartAttach.Add(NewPartAttached);
}

public void NewPartAttached(GameEvents.HostTargetAction<Part, Part> action)
{
if (action.host)
{
Debug.Log("Found action host: " + action.host.ConstructID);

if (EditorLogic.fetch.ship.parts.Count > 0)
{
Part sourcePart = PartLoader.Instance.parts[Random.Range(0, PartLoader.Instance.parts.Count - 1)].partPrefab;
MeshFilter sourceMf = sourcePart.FindModelComponent<MeshFilter>();
MeshFilter mf = action.host.FindModelComponent<MeshFilter>();

Debug.Log("Found mfs? " + (sourceMf != null ? "found sourceMf" : "") + (mf != null ? " found mf" : ""));
mf.sharedMesh = sourceMf.sharedMesh;
}
}
}
}
}

Link to comment
Share on other sites

Somehow there are also periodic GUI glitches such as the break button and throttle indicator disappearing which might be shader or light related, I do not know. Suggestions are welcome as these issues are stopping a welding mod from becoming truly awesome.

I'm running into this myself with a plugin I'm writing that controls the throttle.

If I set the throttle to 100 the throttle indicator vanishes. The throttle still works fine and behaves as expected, just the indicator is gone.

I then remembered that the max throttle is 1 for 100%, not 100 so setting the throttle to 10000% had issues unsurprisingly.

Try adding Mathf.Clamp() arguments around variables before passing them back to KSP might do the trick for what you are experiencing?

D.

Link to comment
Share on other sites

Try adding Mathf.Clamp() arguments around variables before passing them back to KSP might do the trick for what you are experiencing?

Well, the earlier prototype I made was just a part.cfg hack to see how far things could go. As I wrote before, there were several issues. Most notably animations were completely broken, which would be fine if I could simply disable them which I found out later I cannot. The bigger issues however are game-breaking:

- Removing certain PartModules (Lights) causing nullpointer exceptions which locked up the game entirely.

- Interface elements glitching and disappearing (related to animations I think).

- Basic functionality such as rover wheels becoming broken (I have no clue).

I do not know whether this is a limitation of Unity or KSP.

To get back to clamping float values, floats themselves are not entirely exact and some perfectly rounded numbers are impossible to represent as a float. Clamping them may still cause them to exceed their maximum value. In any case, I do not see how a thrust ratio of 10 or even 1000 could break things. Essentially your fuel usage would be through the roof and you'll have insane thrust for a couple seconds but that should be all. In my case, the test parts did not even have an engine nor fuel and I never raised the thrust above 0.

By the way, I found another rather game-breaking bug: When I attempted to dock some segments in space, several docking clamps malfunctioned (refused to dock) and one even became duplicated. I tried hacking the persistence file to get rid of the duplicate but I did not have much success with the newer format. My attempts would cause all context (right click) menus to stop functioning and in some cases half of the craft would warp several dozen kilometers in random directions or in the most extreme case parts would be randomly scattered around, including sub-parts (which should not happen since most of it was a single part).

I'm clueless at this point and pondering whether this mod will ever actually work for more than just merging a couple fuel tanks.

Sorry for my lack of responses, I've been rather busy and sick as well.

Link to comment
Share on other sites

im very much waiting intently for some form of culmination from you gentlemen. most of this stuff is greek to me and i wish i could help you.

im looking for a one click mod that will convert a craft file into 1 structural part while in the ksp universe. if thats too much to ask then just a one click mod that converts a craft file into 1 structural part then be able to launch as one part into the ksp universe.

part functionality is not necessary, only that it look exactly the same as the multiple part craft. how far away are you gentlemen from creating something magnificent? :)

Link to comment
Share on other sites

im very much waiting intently for some form of culmination from you gentlemen. most of this stuff is greek to me and i wish i could help you.

im looking for a one click mod that will convert a craft file into 1 structural part while in the ksp universe. if thats too much to ask then just a one click mod that converts a craft file into 1 structural part then be able to launch as one part into the ksp universe.

part functionality is not necessary, only that it look exactly the same as the multiple part craft. how far away are you gentlemen from creating something magnificent? :)

I have fixed a big bug on mine yesterday (been on it for weeks!)

A few tweaks, and I may release a playtest to hae a better view of what can't be welded!

im very much waiting intently for some form of culmination from you gentlemen. most of this stuff is greek to me and i wish i could help you.

im looking for a one click mod that will convert a craft file into 1 structural part while in the ksp universe. if thats too much to ask then just a one click mod that converts a craft file into 1 structural part then be able to launch as one part into the ksp universe.

part functionality is not necessary, only that it look exactly the same as the multiple part craft. how far away are you gentlemen from creating something magnificent? :)

If you suggest it to the dev, it will be locked because it's on the what not to suggest list. So soon for me.

Link to comment
Share on other sites

good stuff m80 and very quick response! heres another: say i want to manually convert a craft to one structural part without a mod. i know that this works by retrieving multiple models from anywhere in the ksp directory but i dont know what data will be accepted under this structure.

Capture.png

how much of that is accepted by straight copy paste into the MODEL { structure?

Link to comment
Share on other sites

good stuff m80 and very quick response! heres another: say i want to manually convert a craft to one structural part without a mod. i know that this works by retrieving multiple models from anywhere in the ksp directory but i dont know what data will be accepted under this structure.

Capture.png

how much of that is accepted by straight copy paste into the MODEL { structure?

I think to manually do it from a craft file is actually harder.

You will need the position (pos), and the Euler rotation (you have to convert the quaternion rot to a set of 3 Euler angle in degree).

For a manual version, I would do everything directly from the source of the cfg, and check in game that the rotation is good. and I will definitely take that pos to avoid making the maths myself.

Link to comment
Share on other sites

so will the model { structure only accept pos =, rot = and attrot =?

which value exactly from my picture is the "quaternion rot" and where can i go to learn how to it convert to.... whatever "3 euler angles in degrees" is XD. im sorry im a pleb for reals.

Edited by praise the suuun
Link to comment
Share on other sites

Here is what the model should look like

MODEL
{
model = urlpart/model
position = posx,posy,posz
scale = scx,scy,scz
rotation = rotx,roty,rotz
}

you can get posx, posy, posz with the pos= in the craft file.

scx, scy, scz will most likely be 1.

For the rotation however

you have quatx, quaty, quatz, quatw with the rot=

You can use this to know how to convert and get rotx, roty, rotz.

For most things however, you will most likely use angle like 90° and 180° so I would suggest to just think about the rotation angle yourself and play with the value.

attRot, I am not sure what it is, but I think it the rotation of the the attachment or due to it's attachment.

And for info:

Euler angles are angle for each axis.

Degree is what we commonly use (0° to 360°, etc...), as opposed to radian which is used by Pi and lot of program.

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