Jump to content

Part, PartModule update functions


Recommended Posts

I've got some questions about the KSP update functions...

By convention, I've been setting up my KSP modules like this:

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


namespace KerbalFoundries
{


[KSPModule("SmoothSteering")]
public class SmoothSteering : PartModule
{
//Some stuff here
}
}

Which works fine. But then I started looking into the update functions, and I found this post in the Help A Fellow Dev Thread:

This is what I\'ve discovered/been using in lieu of any real documentation for the Part class. If you compile this and set is as the module for a part, you\'ll get a debug print (show using Alt-F2) as each member function runs. Note that the commented-out ones will spam the debug console if re-enabled.

[tt]

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using UnityEngine;

public class DebugPart: Part

{

// Called when part disconnects from vessel due to crash etc. - seems to be called first

// Also called when part is lowest part of a stack above a decoupler

protected override void onDisconnect()

{

print('DBG: onDisconnect');

base.onDisconnect();

}

// Called in VAB after onPartAttach() or any update of vessel in VAB

// Also called after onPack()

public override void onBackup()

{

print('DBG: onBackup');

base.onBackup();

}

// Called in VAB when copying a part for symmetric placement

protected override void onCopy()

{

print('DBG: onCopy');

base.onCopy();

}

// Called continually when part is active anywhere in VAB, after onPartStart()

protected override void onEditorUpdate()

{

//print('DBG: onEditorUpdate');

base.onEditorUpdate();

}

// Called in VAB when removing a part after onPartDelete()

// or in flight scene when destroying a part after onPartExplode()

// also after part goes out of range (>2.5km) of focussed ship

protected override void onPartDestroy()

{

print('DBG: onPartDestroy');

base.onPartDestroy();

}

// Does not seem to be reliably called - mostly when decoupler explodes

protected override void onDecouple(float breakForce)

{

print('DBG: onDecouple(' + breakForce + ')');

base.onDecouple(breakForce);

}

// Called at beginning of flight scene after onPartStart()

// also when part comes in range of focussed ship (<2.5km) after onPartStart()

protected override void onFlightStart()

{

print('DBG: onFlightStart');

base.onFlightStart();

}

// Called for launchpad start after onFlightStart()

protected override void onFlightStartAtLaunchPad()

{

print('DBG: onFlightStartAtLaunchPad');

base.onFlightStartAtLaunchPad();

}

// Called when loading the flight state, such as when part goes off-rails

public override void onFlightStateLoad(Dictionary<string, KSPParseable> parsedData)

{

print('DBG: onFlightStateLoad');

base.onFlightStateLoad(parsedData);

}

// Called when saving from VAB and during flight state save, such as when part goes on-rails

public override void onFlightStateSave(Dictionary<string, KSPParseable> partDataCollection)

{

print('DBG: onFlightStateSave');

base.onFlightStateSave(partDataCollection);

}

// Called when any vessel control input occurs, even automatic

// may be called during part placement before flight scene is properly setup

protected override void onCtrlUpd(FlightCtrlState s)

{

//print('DBG: onCtrlUpd(' + s + ')');

base.onCtrlUpd(s);

}

// Called when game is paused (ESC)

protected override void onGamePause()

{

print('DBG: onGamePause');

base.onGamePause();

}

// Called when game in unpaused

protected override void onGameResume()

{

print('DBG: onGameResume');

base.onGameResume();

}

// Called when part soft-lands on water

protected override void onPartSplashdown()

{

print('DBG: onPartSplashdown');

}

// Called when part goes on rails (>500m from focussed ship)

protected override void onPack()

{

print('DBG: onPack');

base.onPack();

}

// Called when part goes off-rails

protected override void onUnpack()

{

print('DBG: onUnpack');

base.onUnpack();

}

// Does not seem to be called

protected override void onPartTouchdown()

{

print('DBG: onPartTouchdown');

base.onPartTouchdown();

}

// Called in VAB when deleting a part, just before onPartDestroy()

protected override void onPartDelete()

{

print('DBG: onPartDelete');

base.onPartDelete();

}

// Called in VAB when attaching to another part - 'parent' gives attached part

protected override void onPartAttach(Part parent)

{

print('DBG: onPartAttach(' + parent + ')');

base.onPartAttach(parent);

}

// called in VAB when detaching part from vessel

protected override void onPartDetach()

{

print('DBG: onPartDetach');

base.onPartDetach();

}

// Initial call in VAB when picking up part

// Also called when part comes into range of focussed ship (<2.5km)

// And at initial part loading at program start

protected override void onPartAwake()

{

print('DBG: onPartAwake');

base.onPartAwake();

}

// Called when part is deactivated after onDisconnect()

protected override void onPartDeactivate()

{

print('DBG: onPartDeactivate');

base.onPartDeactivate();

}

// Called after explosion of part due to crash etc.

// Seems to be after explosion effects are applied

protected override void onPartExplode()

{

print('DBG: onPartExplode');

base.onPartExplode();

}

// Called in VAB after onPartAwake()

// Also in flight scene on vessel placement just before onFlightStart()

// and when part comes into range of focussed ship (<2.5km)

protected override void onPartStart()

{

print('DBG: onPartStart');

base.onPartStart();

}

// Called during initial part load at start of game

protected override void onPartLoad()

{

print('DBG: onPartLoad');

base.onPartLoad();

}

// Does not seem to be called

protected override void onPartLiftOff()

{

print('DBG: onPartLiftOff');

base.onPartLiftOff();

}

// called regularly during flight scene if on focussed vessel - after each frame render?

protected override void onPartUpdate()

{

//print('DBG: onPartUpdate');

base.onPartUpdate();

}

// called continuously during flight scene if on focussed vessel

protected override void onPartFixedUpdate()

{

//print('DBG: onPartFixedUpdate');

base.onPartFixedUpdate();

}

// called regularly during flight scene if in active stage - after each frame render?

protected override void onActiveUpdate()

{

//print('DBG: onActiveUpdate');

base.onActiveUpdate();

}

// called continously during flight scene if in active stage

protected override void onActiveFixedUpdate()

{

//print('DBG: onActiveFixedUpdate');

base.onActiveFixedUpdate();

}

// called for parts connected to something else after onPack()

protected override void onJointDisable()

{

print('DBG: onJointDisable');

base.onJointDisable();

}

// called for parts connected to something else after onUnpack()

protected override void onJointReset()

{

print('DBG: onJointReset');

base.onJointReset();

}

// called when stage part is on becomes active

protected override bool onPartActivate()

{

print('DBG: onPartActivate');

return base.onPartActivate();

}

}

[/tt]

Some of the stuff in there looks really helpful, but it's only accessible if you specify

 [I]MyClassName[/I] : Part 

instead of

 MyClassName : PartModule 

Ok, but then you lose a lot of the functionality from PartModule. So, how do I use this (if I can)?

The standard OnStart OnUpdate and OnFixedUpdate are usable, but you have to add a load of checks for which scene you're in and whether the vessel is currently focussed. It's not the biggest deal, but I reasoned that there must be a more elegant way of doing it through the KSP API.

Am I looking at this all wrong, and if not, what have I missed?

Thank you! I've managed to get further than I ever thought with what's been posted, I just get stuck and confused sometimes.

Link to comment
Share on other sites

PartModule is meant for modular code to be incorporated into Parts via MODULE{} nodes (in the config file) (i.e., ModuleDockingNode, ModuleDeployableSolarPanel, ModuleDecouple, ModuleAnchoredDecoupler, etc)

Part is the class used by the actual parts that are configured via the PART{} node in a config file; typically in files named part.cfg

You are not likely going to get much use out of extending the Part class from a practical standpoint and are most likely looking for PartModule instead.

Edit: Also, if you are looking for hooks for events, look into GameEvents. For example, if I wanted code to execute every time a ship comes out of TimeWarp I would attach it to GameEvents.onVesselGoOffRails.


GameEvents.onVesselGoOffRails.Add (MyFunction);

public void MyFunction(Vessel v)
{
}

Here's a list of GameEvents events

http://forum.kerbalspaceprogram.com/threads/36727-GameEvents-documentation?highlight=gameevents

Edited by Starwaster
Link to comment
Share on other sites

Thanks, there are one or two things in there that are going to be really helpful! How do I actually go about hooking into them, though? I need to re-run a particular routine when and undock even happens, for example.

Link to comment
Share on other sites

Well in the GameEvents list, there's

  • onPartUndock
  • onUndock
  • onSameVesselUndock

You're going to add either of them the same way:


// These typically are going to go somewhere like Start()
GameEvents.onPartUndock.Add(MyOnPartUndockFunction); // Registers a function that you create named MyOnPartUndockFunction
GameEvents.onUndock.Add(MyOnUndockFunction); // Registers a function that you create named MyOnUndockFunction

At the time that I write this, I have no idea what arguments either of those functions will be called with as I have never used them before. Someone else out there may or may not have documented it previously or else may have used it in their code.

However, with a little exploration it should be easy to see how they are to be used. When typing in the event you want to hook into, MonoDevelop gives information about it as I type. If you use VisualStudio there's probably something similar or there's the browser feature. (right click GameEvents in your code and go to its declaration). Also, if you try to compile without passing your function any arguments then the compile error should tell you what was expected. In the case of OnUndock, MonoDevelop tells me that EventData<EventReport> is expected so....


public void Start()
{
GameEvents.onUndock.Add(MyOnUndockFunction);
}

public void MyUndockFunction(EventReport report)
{
}

The autocomplete function tells you what fields report has. It shouldn't be too hard to figure out from that what data is being passed along.

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