![]() | ILinkSourceLinkToTarget Method (ILinkTarget) |
Namespace: KASAPIv2
bool LinkToTarget( ILinkTarget target )
The linking mode must be started via the StartLinking(GUILinkMode, LinkActorType) call for this method to succeed.
If the link has been established successfully, the source and the target parts become associated with each other.
The link conditions will be checked via CheckCanLinkTo(ILinkTarget, Boolean, Boolean, Boolean) before creating the link. If the were errors, they will be reported to GUI if the linking mode was started with actor Player. The linking mode won't be cancelled in case of the link failure.
// Connects two parts assuming the source and the target parts own exactly one link module. public static bool ConnectParts(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; } // GUILinkMode.API mode tells the implementation to not execute any user facing effects on the // link. See GUILinkMode for more details. if (!source.StartLinking(GUILinkMode.API, LinkActorType.API) || !source.LinkToTarget(target)) { // Here we can only fail due to the constraints. E.g. the link mode is not supported, or the // joint module doesn't give the green light. Debug.LogError("Linking failed"); source.CancelLinking(); return false; } Debug.LogFormat("Established link with part: id={0}", source.linkPartId); return true; }