Jump to content

problem with my parts and gui


Recommended Posts

I have part with plugin and all part have force. I can change this force in gui and when i change force it only one part have new force. How make parts list and how control all parts? I can't find this on wiki.


using UnityEngine;
using PluginTutorial.Extensions;
using KSP.IO;

namespace PluginTutorial
{
public class PluginTutorial : PartModule
{
private static Rect _windowPosition = new Rect();
private GUIStyle _windowStyle, _labelStyle;
private bool _hasInitStyles = false;
public float force = 10f;

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


public override void OnSave(ConfigNode node)
{
PluginConfiguration config = PluginConfiguration.CreateForType<PluginTutorial>();

config.SetValue("Window Position", _windowPosition);
config.save();
}

public override void OnLoad(ConfigNode node)
{
PluginConfiguration config = PluginConfiguration.CreateForType<PluginTutorial>();

config.load();
_windowPosition = config.GetValue<Rect>("Window Position");
}

public override void OnUpdate()
{
if (this.vessel == FlightGlobals.ActiveVessel)
{
this.rigidbody.AddRelativeForce(Vector3.up * force * FlightInputHandler.state.mainThrottle);
}
}


private void OnDraw()
{
if (this.vessel == FlightGlobals.ActiveVessel && this.part.IsPrimary(this.vessel.parts, this.ClassID))
{
_windowPosition = GUILayout.Window(10, _windowPosition, OnWindow, "This is a title", _windowStyle);

}
}
private void OnWindow(int windowId)
{
string sSila;

sSila = force.ToString();
GUILayout.BeginHorizontal();
GUILayout.Label(sSila, _labelStyle);
GUILayout.EndHorizontal();

if (GUI.Button(new Rect(25, 50, 30, 30), "+"))
force += 10;
if (GUI.Button(new Rect(75, 50, 30, 30), "-"))
force -= 10;

GUI.DragWindow();
}

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

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

_hasInitStyles = true;
}
}
}

Link to comment
Share on other sites

You want to control all parts of a specific vessel? Guess the usual way most modders would do it is by looping though vessel.parts, check whether it has your part module and do whatever they want with it. Ofc doing sth like this frequently does not make the game scale very well. Just imagine a vessel with 10k parts... well, code like this is one of the reasons we most likely won't have such large vessels.

Just using a static variable isn't good, since it can create issues with saveing/loading and you cannot have different values for different vessels.

Another approach could be to keep a static dictionary with VesselIds or even PartIds as index up to date and look up your object whenever you need it. Dict's aren't the fastest, but they pretty much do they job in the same speed no matter how many objects are in them.

Anyway, i think in your case it might be simpler to keep a ref to a single data-object per vessel in all your part modules. Sth like this:


public class TutDataObject{
public float force;
}
public class TutPartModule: PartModule{
TutDataObject tutData;
bool isPrimary = false;
void Start(){ // or awake?
tutData = vessel.parts.SelectMany(part => part.Modules.OfType<TutPartModule>()).Where(tutMod => tutMod.tutData != null).FirstOrDefault();
if( tutData == null ){
isPrimary = true;
tutData = new ...
}
}
void OnSave(...){
if(isPrimary){
//save data...
}
}
void OnLoad(...){
// load data if there is such a node
}
}

Ofc its not that easy... since a vessels can be split into multiple vessels, e.g. by staging and merged by e.g. docking. A way to handle such situtations would be to store the latestest VesselId in TutDataObject and check whether its up to date via the GameEvents.onVesselChange.

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