Jump to content

Finding a part on another vessel in OnStart()


Recommended Posts

OK, this one is tricky.

So I've got a PartModule which is creating joints that acts as a trailer hitch. It stores the flightID of the part it gets hitched up to in persistence with the aim of finding that same part to run some checks against and create the joint with again.

Now, both vessels, tractor and trailer (for arguments sake, I'm not trying to create Farm Simulator here ;) ) stay as separate vessels, which is deliberate. What seems to be happening when the scene switches to flight is this:

If you select the tractor as the vessel to fly from the KSC:


foreach (Part pa in FindObjectsOfType(typeof(Part)) as Part[])
{
print(pa.flightID);
if (pa.flightID.Equals(uint.Parse(_flightID))) //seemed to have an issue with Uints as KSPFields, so I save _flightID as string and reparse. {
{

//truncated
}
}

prints the ID of the parts in the tractor vessel and nothing more. Looking further into the logs suggests that all the parts and part modules for the trailer get going after this point.

Selecting the trailer as the vessel to fly, prints the parts of both vessels, presumably because the tractor was second on the scene (as it were), and things work as expected.

So my question is this: Is there a point after which all vessels have been unpacked and are active, but BEFORE physics starts, and how might I tell when that point is? What I don't want to happen is for things to move about before the hitch joint is recreated, for obvious reasons.

Many thanks!

Edited by lo-fi
Link to comment
Share on other sites

(...) tractor and trailer (...)

for arguments sake, I'm not trying to create Farm Simulator here

Are you sure? :D
So my question is this: Is there a point after which all vessels have been unpacked and are active, but BEFORE physics starts, and how might I tell when that point is? What I don't want to happen is for things to move about before the hitch joint is recreated, for obvious reasons.

Packing & unpacking are the names of the processes when physics activate or deactivate I think.

What might help you:

GameEvents.onPartPack & onPartUnpack - Fired when a part comes into or leaves physics range/go on rails/leave rails

GameEvents.onVesselGoOnRails & onVesselGoOffRails - The same but with whole vessels

These are events. When you hook on them they'll call whatever method you like when the conditions are met.

Example:

void Start()
{
GameEvents.onVesselGoOffRails.Add(GoingOffRailsEvent);
}
...
void GoingOffRailsEvent(Vessel vessel)
{
// vessel is the vessel which is going to/is in the process of/was going off rails.
// But the fact that you are getting a fully initialized Vessel object indicates
// that physics were loaded just the very moment before.
}

It might be a good idea to hook on that event, apply your checks and then wait for the next fixed update (= physics frame) to see if your joint works.

Edited by *Aqua*
Link to comment
Share on other sites

Well, if someone wants to model some cows and sheep, why not? ;)

OK, that all makes sense. I'll hook into the events, set up a few debug lines and see how it goes. Looking at the log, onVesselGoOffRails (Log message saying vessels are unpacked) seems to be one of the last things that happens, and crucially after the trailer logs a load of stuff, implying its PartModules are active). Thank you!

EDIT: Indeed, that does the trick. Thanks!

Edited by lo-fi
Link to comment
Share on other sites

Well, if someone wants to model some cows and sheep, why not? ;)

OK, that all makes sense. I'll hook into the events, set up a few debug lines and see how it goes. Looking at the log, onVesselGoOffRails (Log message saying vessels are unpacked) seems to be one of the last things that happens, and crucially after the trailer logs a load of stuff, implying its PartModules are active). Thank you!

EDIT: Indeed, that does the trick. Thanks!

Make sure you add code to handle two craft becoming separated due to timewarp, because guaranteed there'll be someone who will try and use your hitch on orbit :P

Link to comment
Share on other sites

Make sure you add code to handle two craft becoming separated due to timewarp, because guaranteed there'll be someone who will try and use your hitch on orbit :P

Space train anyone? That's right, we're going to do something that should never be done and try to pull one craft with another using rope. What could go wrong?

Link to comment
Share on other sites

Thanks, that's a whole other thing to tackle. Still not sure how to handle that. :/

I would handle it by just checking if the crafts have drifted apart and if so don't create the joint upon existing timewarp. No idea what you're trying to accomplish :P. Don't overcomplicate things if you dont have to.

Link to comment
Share on other sites

it might be a good idea to store the "relative location" of the trailer vessel, if you do that, you can easely re-position it, even on the first physics frame and add the joint then..

at least that is what I think could work simpler and it would handle the drifting away problem just the same..

In my opinion, what you try to fight here is a race-car-condition..

It might not allways come up in the same order.. - so the first physics frame is still invisible to the user and surely not too early

I've been workin on a "soft docking" for my huge stations - ship stays there but is still seperate - I use forces for this and the results are not as good as hoped..

So I'm very intrested in your code if this get's released :-P

It might solve my problem too..

The seperate vessel thing is required for an NCI detail in case you wondered..

if they'd docked real, NCI would no longer work as it should..

Link to comment
Share on other sites

It may be a non starter as Zodius suggests, but this is something I'd really like to get to work. Relative position and orientation are easy, there are objects on hitch and corresponding coupling on the trailer that reveal that data easily. Snapping the trailer back to the correct position and orientation would be easy, though I'll possibly have to adjust the trailer velocity or hilarity will ensue. Might be a fun one to love stream, lots of cool stuff to show off.

Not sure if you saw in KF thread, I'm planning on making soft tie downs in the same fashion for attaching rovers to carrying craft. This might be even closer to what you're looking for! The current plan is to have a little part that you surface attach in various places to the rover or carrier. When commanded, they will raycast a certain distance and use the collision data to create a joint between themselves and what they hit - much the same as the hitch, but more forgiving of position and locks things tightly in place without docking. Could, of course, be integrated into another part with the addition of some extra transforms. All good fun!

Link to comment
Share on other sites

Also, one question:

GameEvents.onVesselGoOffRails.Add(VesselUnPack); seems to add a hook every time OnStart is called and results in it being added every time you scene change, for example. Is there a way to check if it's been added already? Or is it better form to remove it, and if so, where?

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