Click or drag to resize

ILinkSourceCheckCanLinkTo Method

Verifies if a link between the parts can be successful.

Namespace:  KASAPIv2
Assembly:  KAS-API-v2 (in KAS-API-v2.dll) Version: KAS API v2
Syntax
C#
bool CheckCanLinkTo(
	ILinkTarget target,
	bool checkStates = true,
	bool reportToGUI = false,
	bool reportToLog = true
)
Request Example View Source

Parameters

target
Type: KASAPIv2ILinkTarget
The target to connect with.
checkStates (Optional)
Type: SystemBoolean
Tells if the source and target states needs to be checked. This check can be disabled when checking for a "theoretical" possibility of the link. However, keep in mind that before doing the actual link, the full check will be performed.
reportToGUI (Optional)
Type: SystemBoolean
If true then the errors will be reported to the UI letting the user know that the link cannot be made.
reportToLog (Optional)
Type: SystemBoolean
If true then the errors will be logged to the logs as warnings. Disabling of such a logging makes sense when the caller code only needs to check for the possibility of the link (e.g. when showing the UI elements). If reportToGUI set to true then the errors will be logged regardless to the setting of this parameter.

Return Value

Type: Boolean
true if the link can be made.
Examples
// Connects two parts assuming the source and the target parts own exactly one link module.
// Does not attempt the link if it's obstructed to avoid a GUI error message.
public static bool ConnectPartsWithCheck(Part srcPart, Part tgtPart) {
  var source = srcPart.FindModuleImplementing<ILinkSource>();
  var target = tgtPart.FindModuleImplementing<ILinkTarget>();
  if (source == null || target == null || source.cfgLinkType != target.cfgLinkType) {
    Debug.LogError("Source and target cannot link");
    return false;
  }
  if (!source.CheckCanLinkTo(target, reportToLog: false)) {
    Debug.Log("Link is obstructed. Silently cancel the action");
    return false;
  }
  if (!source.StartLinking(GUILinkMode.API, LinkActorType.API) || !source.LinkToTarget(target)) {
    Debug.LogError("Linking failed");
    source.CancelLinking();
    return false;
  }
  return true;
}
See Also