Jump to content

Why not work IsPrimary


Recommended Posts

Hello i have this source:

TutorialPlugin.cs:

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

namespace TutorialPlugin
{
public class TutorialPlugin : PartModule
{
private Rect _windowPosition = new Rect();
private GUIStyle _windowsStyle, _labelStyle;
private bool _hasInitStyles = false;

public override void OnStart(StartState state)
{
if (state != StartState.Editor)
if (!_hasInitStyles) InitStyles();
RenderingManager.AddToPostDrawQueue(0, OnDraw);
}

private void OnDraw()
{
if (this.vessel == FlightGlobals.ActiveVessel)
_windowPosition = GUILayout.Window(10, _windowPosition, OnWindow, "Hello v 0.0.1a Dev", _windowsStyle);
}

private void OnWindow(int windowId)
{
GUILayout.BeginHorizontal();
GUILayout.Label("hmmmmm", _labelStyle);
GUILayout.EndHorizontal();

GUI.DragWindow();
}

private void InitStyles()
{
_windowsStyle = new GUIStyle(HighLogic.Skin.window);
_windowsStyle.fixedWidth = 250f;

_labelStyle = new GUIStyle(HighLogic.Skin.label);
_labelStyle.stretchWidth = true;

_hasInitStyles = true;
}

}
}

PartExtensions.cs:

using System.Collections.Generic;
using UnityEngine;

namespace TutorialPlugin.Extensions
{
public static class PartExtensions
{
public static bool IsPrimary(this Part thisPart, List<Part> partsList, int moduleClassID)
{
foreach (Part part in partsList)
{
if (part == thisPart)
return true;
else
break;
}

return false;
}
}
}

Where is error, all code is on youtube tutorial ! why is not working ?

Link to comment
Share on other sites

Have you added the module "TutorialPlugin" to any parts?

EDIT

This looks like the sort of thing you probably want to run using the KSPAddon framework rather than a PartModule (part modules are good if you need multiple instances, KSPAddon is easier for single instances). See this simple plugin as an example

Edited by Crzyrndm
Link to comment
Share on other sites

yes, if i ihave only TutorialPlugin.cs with out line "using TutorialPlugin.Extensions" and remove file PartExtensions.cs then work. on part.cfg i add MODULE { name = TutorialPlugin }

Link to comment
Share on other sites

if i delete PartExtensions.cs work, if i add 2 same component erroor windows. PartExtensions.cs is bug fix on place few same component.

- - - Updated - - -

where i found normal documentation with examples as for php (php.net), but on KSP.

Link to comment
Share on other sites

Your main class has an argument missing in one of your if statements, I think you may have missed part of the video tutorials.

This original code:

 private void OnDraw()
{
if (this.vessel == FlightGlobals.ActiveVessel)
_windowPosition = GUILayout.Window(10, _windowPosition, OnWindow, "Hello v 0.0.1a Dev", _windowsStyle);
}

Should be this:


private void OnDraw()
{
if (this.vessel == FlightGlobals.ActiveVessel && this.part.IsPrimary(this.vessel.parts, this.ClassID))
_windowPosition = GUILayout.Window(10, _windowPosition, OnWindow, "Hello v 0.0.1a Dev", _windowsStyle);
}

The section you are missing is an addition to the IF statement:

this.part.IsPrimary(this.vessel.parts, this.ClassID)

Hope this helps, sorry if there are any typo's; go back to the video series and have a look, I've recently watched through them and know your code is incomplete.

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