Jump to content

The official unoffical "help a fellow plugin developer" thread


Recommended Posts

Hi again,

For some reason, this code doesn't seem to be working.

public override void OnFixedUpdate(){

print ("This should say something...");

if (CompressAir == true) {

print ("Should be compressing air...");

part.RequestResource("Oxidizer", -10);

}

}

I think that should spam the log with at least one message every FixedUpdate. But, it doesn't, even when it's OnDraw. The KSPEvent buttons are able to print things to the log, but why can't I do this OnDraw or OnFixedUpdate?

Thanks in advance.

You've left out a few details, can you copy paste your entire code window into a new post? Put them inside {code} blocks will make it easier for us to parse it. Quote my post to see the correct square brackets to use.

-insert code here

From the limited description there could be a couple things wrong.

-Do you inherit at least the monobehavior class? The 'print' command is inherited through that.

-Do you add your OnDraw method to the rendering manager so it gets called?

But really, the information you've given us is lacking to really tell what is going wrong.

D.

Edited by Diazo
Link to comment
Share on other sites

Hi again,

For some reason, this code doesn't seem to be working.

Since this seems to be for a partmodule, did you activate the module? OnUpdate and (I think) OnFixedUpdate only run when the module has been activated. Normally this is through staging, or just putting this.part.force_activate() in the OnStart code.

Link to comment
Share on other sites

Thanks for the quick response,

Debug.Log("SPAM"); inside OnFixedUpdate didn't work either. Print("example") worked for the KSPEvent buttons, and part.RequestResource("Oxidizer", -10); worked also. I need to know how to get debug and part.RequestResource working in the OnFixedUpdate loop. Maybe the entire part module code would be helpful?

 using System;
using UnityEngine;
using KSP.IO;

namespace MarsDirect{

public class AirCompressor : PartModule{

//declares variables

public bool CompressAir = false;

//these variables aren't used yet
public String CurrentPlanet = "";
public float OxygenRatio = 0;
public float CarbonDioxideRatio = 0;
public float Oxygen = 1;
public float CarbonDioxide = 0;


//button to start compressing
[KSPEvent(guiActive = true, guiName = "Start Compressing", active = true)]
public void StartCompression(){
CompressAir = true;
print ("CompressAir = true");
}


//button to stop compressing
[KSPEvent(guiActive = true, guiName = "Stop Compressing", active = true)]
public void StopCompression(){
CompressAir = false;
print ("CompressAir = false");
}

//I think this should be doing something to the log, but it doesn't.
public override void OnFixedUpdate(){
Debug.Log ("SPAM");
if (CompressAir == true) {
print ("Should be compressing air...");
part.RequestResource("Oxidizer", -10);
}
}
}
}

Link to comment
Share on other sites

Okay, the code is never running because you never assign the partModule to a part.

Take a part (you can use one of squads default parts, just make a backup of the part.cfg first) and make sure it has the following:


MODULE
{
name = AirCompressor
}

MODULE is a typical field, any of the existing part.cfgs can show you where it goes. It is okay for a part to have multiple MODULEs.

Then when you place that part in the editor, or fly that ship in flight mode, the AirCompressor class will run. Note that it will run once for each part placed that has this part module attached.

I'm not sure FixedUpdate will run in the editor though, I'm pretty sure Update does however.

D.

Link to comment
Share on other sites

EDIT: Answered, see answer in the code block, you have to attach the line transform as a child to the root part transform.

Okay. I am trying to draw a line using the line renderer but I can't figure out how to point it in the direction I want.

The following are my constants:


TWR1CoM = TWR1Vessel.findWorldCenterOfMass(); //find vessel center of mass as Vector3 in world co-ords
TWR1Up = (TWR1CoM - TWR1Vessel.mainBody.position).normalized; //find Vector 3 line from vessel center of mass to current sphere of influnce center of mass (kerbin for my test)
TWR1ControlUp = (TWR1Vessel.rootPart.transform.up); //the local co-rds up of the root part in world co-ords, this is what points up for a crew capsule if you don't turn it with WASD keys
TWR1OffsetVert = Vector3.Angle(TWR1Up, TWR1ControlUp); //angle betweeen part local up and world up, in degrees. <- this works, I print it and it updates correctly for how far off vertical my vessel is

So, that all works. I am now trying to setup a way to switch between crew capsule rockets and airplane cockpits (which are rotated 90° from each other).

Therefore I am going to use a line, but when I do the following on a line on the same test vessel (so on a crew capsule, not dealing with cockpits yet):


theLine = lineObj.AddComponent<LineRenderer>();
theLine.material = new Material(Shader.Find("Particles/Additive"));
theLine.SetColors(Color.red, Color.red);
theLine.SetWidth(1, 0);
theLine.SetVertexCount(2);
theLine.useWorldSpace = false;
//[b]Answer:[/b] to set the transform relative to the root part, you need to make the line transform a child and then reset the position
theLine.transform.parent = TWR1Vessel.rootPart.transform; //attach line transform to root part as chile
theLine.transform.localPosition = Vector3.zero; //reset origin point
theLine.transform.rotation = Quaternion.identity; //reset rotation
theLine.SetPosition(0, new Vector3(0, 0, 0));
theLine.SetPosition(1, TWR1ControlUp * 50); <- Note the same Variable here, this now works as expected after attaching as child.

This draws a line 90° off from where I expect.This now draws the line as I expect.

My question is that to me it looks like I am using the TWR1ControlUp variable the same way, why is it 90° off? Or is my setting of the rotation 2 lines before off?

D.

Edited by Diazo
Link to comment
Share on other sites

EDIT: Answered, see answer in the code block, you have to attach the line transform as a child to the root part transform.

Okay. I am trying to draw a line using the line renderer but I can't figure out how to point it in the direction I want.

The following are my constants:


TWR1CoM = TWR1Vessel.findWorldCenterOfMass(); //find vessel center of mass as Vector3 in world co-ords
TWR1Up = (TWR1CoM - TWR1Vessel.mainBody.position).normalized; //find Vector 3 line from vessel center of mass to current sphere of influnce center of mass (kerbin for my test)
TWR1ControlUp = (TWR1Vessel.rootPart.transform.up); //the local co-rds up of the root part in world co-ords, this is what points up for a crew capsule if you don't turn it with WASD keys
TWR1OffsetVert = Vector3.Angle(TWR1Up, TWR1ControlUp); //angle betweeen part local up and world up, in degrees. <- this works, I print it and it updates correctly for how far off vertical my vessel is

So, that all works. I am now trying to setup a way to switch between crew capsule rockets and airplane cockpits (which are rotated 90° from each other).

Therefore I am going to use a line, but when I do the following on a line on the same test vessel (so on a crew capsule, not dealing with cockpits yet):


theLine = lineObj.AddComponent<LineRenderer>();
theLine.material = new Material(Shader.Find("Particles/Additive"));
theLine.SetColors(Color.red, Color.red);
theLine.SetWidth(1, 0);
theLine.SetVertexCount(2);
theLine.useWorldSpace = false;
//[B]Answer:[/B] to set the transform relative to the root part, you need to make the line transform a child and then reset the position
theLine.transform.parent = TWR1Vessel.rootPart.transform; //attach line transform to root part as chile
theLine.transform.localPosition = Vector3.zero; //reset origin point
theLine.transform.rotation = Quaternion.identity; //reset rotation
theLine.SetPosition(0, new Vector3(0, 0, 0));
theLine.SetPosition(1, TWR1ControlUp * 50); <- Note the same Variable here, this now works as expected after attaching as child.

This draws a line 90° off from where I expect.This now draws the line as I expect.

My question is that to me it looks like I am using the TWR1ControlUp variable the same way, why is it 90° off? Or is my setting of the rotation 2 lines before off?

D.

The line's positions with setPosition() should always just be pointing it forward. Then set its direction with rotation and its position with localPosition.

Here's a 'DrawRay' substitute I'm working on right now (for the defunct Debug.DrawRay function)


[FONT=Menlo][COLOR=#444444] [/COLOR][COLOR=#009695]public[/COLOR][COLOR=#444444] [/COLOR][COLOR=#009695]class[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]RayRenderer[/COLOR]
[COLOR=#444444] {[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#009695]private[/COLOR][COLOR=#444444] [/COLOR][COLOR=#3363a4]LineRenderer[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444] = [/COLOR][COLOR=#3363a4]null[/COLOR][COLOR=#444444];[/COLOR]

[COLOR=#444444] [/COLOR][COLOR=#009695]public[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]RayRenderer[/COLOR][COLOR=#444444] ()[/COLOR]
[COLOR=#444444] {[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#3363a4]GameObject[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]obj[/COLOR][COLOR=#444444] = [/COLOR][COLOR=#009695]new[/COLOR][COLOR=#444444] [/COLOR][COLOR=#3363a4]GameObject[/COLOR][COLOR=#444444]( [/COLOR][COLOR=#f57c00]"[/COLOR][COLOR=#f57c00]Ray[/COLOR][COLOR=#f57c00]"[/COLOR][COLOR=#444444] );[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]obj[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]layer[/COLOR][COLOR=#444444] = [/COLOR][COLOR=#f57c00]1[/COLOR][COLOR=#444444];[/COLOR]
[COLOR=#444444] [/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#999988][I]//[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]Then[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]create[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]renderer[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]itself[/I][/COLOR][COLOR=#999988][I]...[/I][/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444] = [/COLOR][COLOR=#444444]obj[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]AddComponent[/COLOR][COLOR=#444444]< [/COLOR][COLOR=#3363a4]LineRenderer[/COLOR][COLOR=#444444] >();[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#999988][I]//[/I][/COLOR][COLOR=#999988][I]renderer[/I][/COLOR][COLOR=#999988][I].[/I][/COLOR][COLOR=#999988][I]transform[/I][/COLOR][COLOR=#999988][I].[/I][/COLOR][COLOR=#999988][I]parent[/I][/COLOR][COLOR=#999988][I] = [/I][/COLOR][COLOR=#999988][I]transform[/I][/COLOR][COLOR=#999988][I]; // ...[/I][/COLOR][COLOR=#999988][I]child[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]to[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]our[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]part[/I][/COLOR][COLOR=#999988][I]...[/I][/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]useWorldSpace[/COLOR][COLOR=#444444] = [/COLOR][COLOR=#3363a4]false[/COLOR][COLOR=#444444]; [/COLOR][COLOR=#999988][I]//[/I][/COLOR][COLOR=#999988][I] ...[/I][/COLOR][COLOR=#999988][I]and[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]moving[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]along[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]with[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]it[/I][/COLOR][COLOR=#999988][I] ([/I][/COLOR][COLOR=#999988][I]rather[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#999988][I]//[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]than[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]staying[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]in[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]fixed[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]world[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]coordinates[/I][/COLOR][COLOR=#999988][I])[/I][/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]transform[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]localPosition[/COLOR][COLOR=#444444] = [/COLOR][COLOR=#3363a4]Vector3[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]zero[/COLOR][COLOR=#444444];[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]transform[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]localEulerAngles[/COLOR][COLOR=#444444] = [/COLOR][COLOR=#3363a4]Vector3[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]zero[/COLOR][COLOR=#444444]; [/COLOR]
[COLOR=#444444] [/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#999988][I]//[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]Make[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]it[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]render[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]a[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]red[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]to[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]yellow[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]triangle[/I][/COLOR][COLOR=#999988][I], [/I][/COLOR][COLOR=#999988][I]1[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]meter[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]wide[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]and[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]2[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]meters[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]long[/I][/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]material[/COLOR][COLOR=#444444] = [/COLOR][COLOR=#009695]new[/COLOR][COLOR=#444444] [/COLOR][COLOR=#3363a4]Material[/COLOR][COLOR=#444444]( [/COLOR][COLOR=#3363a4]Shader[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]Find[/COLOR][COLOR=#444444]( [/COLOR][COLOR=#f57c00]"[/COLOR][COLOR=#f57c00]Particles/Additive[/COLOR][COLOR=#f57c00]"[/COLOR][COLOR=#444444] ) );[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]SetColors[/COLOR][COLOR=#444444]( [/COLOR][COLOR=#3363a4]Color[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]red[/COLOR][COLOR=#444444], [/COLOR][COLOR=#3363a4]Color[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]yellow[/COLOR][COLOR=#444444] );[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]SetWidth[/COLOR][COLOR=#444444]( [/COLOR][COLOR=#f57c00]1[/COLOR][COLOR=#444444], [/COLOR][COLOR=#f57c00]0[/COLOR][COLOR=#444444] ); [/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]SetVertexCount[/COLOR][COLOR=#444444]( [/COLOR][COLOR=#f57c00]2[/COLOR][COLOR=#444444] );[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]SetPosition[/COLOR][COLOR=#444444]( [/COLOR][COLOR=#f57c00]0[/COLOR][COLOR=#444444], [/COLOR][COLOR=#3363a4]Vector3[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]zero[/COLOR][COLOR=#444444] );[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]SetPosition[/COLOR][COLOR=#444444]( [/COLOR][COLOR=#f57c00]1[/COLOR][COLOR=#444444], [/COLOR][COLOR=#3363a4]Vector3[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]forward[/COLOR][COLOR=#444444] * [/COLOR][COLOR=#f57c00]2[/COLOR][COLOR=#444444] );[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#999988][I]//[/I][/COLOR][COLOR=#999988][I]renderer[/I][/COLOR][COLOR=#999988][I].[/I][/COLOR][COLOR=#999988][I]sortingOrder[/I][/COLOR][COLOR=#999988][I] = [/I][/COLOR][COLOR=#999988][I]1[/I][/COLOR][COLOR=#999988][I];[/I][/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#999988][I]//[/I][/COLOR][COLOR=#999988][I]renderer[/I][/COLOR][COLOR=#999988][I].[/I][/COLOR][COLOR=#999988][I]sortingLayerName[/I][/COLOR][COLOR=#999988][I] = "[/I][/COLOR][COLOR=#999988][I]GUI[/I][/COLOR][COLOR=#999988][I]";[/I][/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#999988][I]//[/I][/COLOR][COLOR=#999988][I]renderer[/I][/COLOR][COLOR=#999988][I].[/I][/COLOR][COLOR=#999988][I]material[/I][/COLOR][COLOR=#999988][I].[/I][/COLOR][COLOR=#999988][I]renderQueue[/I][/COLOR][COLOR=#999988][I] = [/I][/COLOR][COLOR=#999988][I]1[/I][/COLOR][COLOR=#999988][I];[/I][/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#999988][I]//[/I][/COLOR][COLOR=#999988][I]renderer[/I][/COLOR][COLOR=#999988][I].[/I][/COLOR][COLOR=#999988][I]material[/I][/COLOR][COLOR=#999988][I].[/I][/COLOR][COLOR=#999988][I]set[/I][/COLOR][COLOR=#999988][I];[/I][/COLOR]
[COLOR=#444444] }[/COLOR]

[COLOR=#444444] [/COLOR][COLOR=#009695]public[/COLOR][COLOR=#444444] [/COLOR][COLOR=#3363a4]void[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]DrawRay[/COLOR][COLOR=#444444]([/COLOR][COLOR=#3363a4]Transform[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]transform[/COLOR][COLOR=#444444], [/COLOR][COLOR=#3363a4]Vector3[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]origin[/COLOR][COLOR=#444444], [/COLOR][COLOR=#3363a4]Vector3[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]direction[/COLOR][COLOR=#444444], [/COLOR][COLOR=#3363a4]Color[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]color[/COLOR][COLOR=#444444])[/COLOR]
[COLOR=#444444] {[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]transform[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]parent[/COLOR][COLOR=#444444] = [/COLOR][COLOR=#444444]transform[/COLOR][COLOR=#444444];[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]SetPosition[/COLOR][COLOR=#444444] ([/COLOR][COLOR=#f57c00]0[/COLOR][COLOR=#444444], [/COLOR][COLOR=#3363a4]Vector3[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]zero[/COLOR][COLOR=#444444]);[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]SetPosition[/COLOR][COLOR=#444444] ([/COLOR][COLOR=#f57c00]1[/COLOR][COLOR=#444444], [/COLOR][COLOR=#3363a4]Vector3[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]forward[/COLOR][COLOR=#444444] * [/COLOR][COLOR=#f57c00]2[/COLOR][COLOR=#444444]);[/COLOR]

[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]transform[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]localPosition[/COLOR][COLOR=#444444] = [/COLOR][COLOR=#444444]origin[/COLOR][COLOR=#444444];[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]transform[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]rotation[/COLOR][COLOR=#444444] = [/COLOR][COLOR=#3363a4]Quaternion[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]LookRotation[/COLOR][COLOR=#444444] ([/COLOR][COLOR=#444444]direction[/COLOR][COLOR=#444444]);[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#999988][I]//[/I][/COLOR][COLOR=#999988][I]renderer[/I][/COLOR][COLOR=#999988][I].[/I][/COLOR][COLOR=#999988][I]transform[/I][/COLOR][COLOR=#999988][I].[/I][/COLOR][COLOR=#999988][I]rotation[/I][/COLOR][COLOR=#999988][I] = [/I][/COLOR][COLOR=#999988][I]Quaternion[/I][/COLOR][COLOR=#999988][I].[/I][/COLOR][COLOR=#999988][I]LookRotation[/I][/COLOR][COLOR=#999988][I]( [/I][/COLOR][COLOR=#999988][I]direction[/I][/COLOR][COLOR=#999988][I] );[/I][/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#3363a4]Color[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]color2[/COLOR][COLOR=#444444] = [/COLOR][COLOR=#444444]color[/COLOR][COLOR=#444444];[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]color2[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]r[/COLOR][COLOR=#444444] *= [/COLOR][COLOR=#f57c00]0.5f[/COLOR][COLOR=#444444];[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]color2[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]g[/COLOR][COLOR=#444444] *= [/COLOR][COLOR=#f57c00]0.5f[/COLOR][COLOR=#444444];[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]color2[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]b[/COLOR][COLOR=#444444] *= [/COLOR][COLOR=#f57c00]0.5f[/COLOR][COLOR=#444444];[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]SetColors[/COLOR][COLOR=#444444] ([/COLOR][COLOR=#444444]color2[/COLOR][COLOR=#444444], [/COLOR][COLOR=#444444]color[/COLOR][COLOR=#444444]);[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#999988][I]//[/I][/COLOR][COLOR=#999988][I]renderer[/I][/COLOR][COLOR=#999988][I].[/I][/COLOR][COLOR=#999988][I]tag[/I][/COLOR][COLOR=#999988][I] = "";[/I][/COLOR]
[COLOR=#444444] }[/COLOR]

[COLOR=#444444] [/COLOR][COLOR=#009695]public[/COLOR][COLOR=#444444] [/COLOR][COLOR=#3363a4]void[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]DrawRay[/COLOR][COLOR=#444444]([/COLOR][COLOR=#3363a4]Vector3[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]origin[/COLOR][COLOR=#444444], [/COLOR][COLOR=#3363a4]Vector3[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]direction[/COLOR][COLOR=#444444], [/COLOR][COLOR=#3363a4]Color[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]color[/COLOR][COLOR=#444444])[/COLOR]
[COLOR=#444444] {[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#999988][I]//[/I][/COLOR][COLOR=#999988][I]throw[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]new[/I][/COLOR][COLOR=#999988][I] [/I][/COLOR][COLOR=#999988][I]NotImplementedException[/I][/COLOR][COLOR=#999988][I]();[/I][/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]useWorldSpace[/COLOR][COLOR=#444444] = [/COLOR][COLOR=#3363a4]true[/COLOR][COLOR=#444444];[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]transform[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]localPosition[/COLOR][COLOR=#444444] = [/COLOR][COLOR=#444444]origin[/COLOR][COLOR=#444444];[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]transform[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]rotation[/COLOR][COLOR=#444444] = [/COLOR][COLOR=#3363a4]Quaternion[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]LookRotation[/COLOR][COLOR=#444444] ([/COLOR][COLOR=#444444]direction[/COLOR][COLOR=#444444]);[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#3363a4]Color[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]color2[/COLOR][COLOR=#444444] = [/COLOR][COLOR=#444444]color[/COLOR][COLOR=#444444];[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]color2[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]r[/COLOR][COLOR=#444444] *= [/COLOR][COLOR=#f57c00]0.5f[/COLOR][COLOR=#444444];[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]color2[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]g[/COLOR][COLOR=#444444] *= [/COLOR][COLOR=#f57c00]0.5f[/COLOR][COLOR=#444444];[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]color2[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]b[/COLOR][COLOR=#444444] *= [/COLOR][COLOR=#f57c00]0.5f[/COLOR][COLOR=#444444];[/COLOR]
[COLOR=#444444] [/COLOR][COLOR=#444444]renderer[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]SetColors[/COLOR][COLOR=#444444] ([/COLOR][COLOR=#444444]color2[/COLOR][COLOR=#444444], [/COLOR][COLOR=#444444]color[/COLOR][COLOR=#444444]);[/COLOR]
[COLOR=#444444] }[/COLOR]
[COLOR=#444444] }[/COLOR]
[/FONT]

Works like DrawRay except that it has an overloaded function, one that attaches to an object and the other for worldspace. (note that the second version is closer in intent to Debug.DrawRay but it is not complete in the example given, and duration is not implemented for either of them. Also, I was doing something different with color, trying to get it to fade. Still a WIP.

Edited by Starwaster
Commented out order sorting stuff; no idea if that even works that way or not.
Link to comment
Share on other sites

I'm almost ready with updating my mod, but I only need 1 more thing. I already have it working to active/deactive the struts via RMB while flying your craft, but now I want to add that function ALSO for EVA.

#region Actions

[KSPAction("Toggle")]
public void ToggleStrut(KSPActionParam param)
{
IsEnabled = !IsEnabled;
CheckHit();
}

[KSPAction("Activate")]
public void ActivateStrut(KSPActionParam param)
{
this.ActivateStrut();
}

[KSPAction("Deactivate")]
public void DeactivateStrut(KSPActionParam param)
{
this.DeactivateStrut();
}

#endregion

#region Events

[KSPEvent(guiActive = true, guiName = "Activate", active = true, guiActiveUnfocused = true, unfocusedRange = 2f)]
public void ActivateStrut()
{
IsEnabled = true;
CheckHit();
this.Events["ActivateStrut"].guiActiveEditor = false;
this.Events["DeactivateStrut"].guiActiveEditor = true;
}

[KSPEvent(guiActive = true, guiName = "Deactivate", active = false, guiActiveUnfocused = true, unfocusedRange = 2f)]
public void DeactivateStrut()
{
IsEnabled = false;
CheckHit();
this.Events["ActivateStrut"].guiActiveEditor = true;
this.Events["DeactivateStrut"].guiActiveEditor = false;
}

#endregion

Do I need to add 'externalToEVAOnly = true' to do that or something else?

Link to comment
Share on other sites

RDNode[] listnode = GameObject.FindObjectsOfType<RDNode>();

This always return an empty array.

I am in career mode, I am loading the plugin once in KSPAddon.Startup.SpaceCentre.

I have tried it inside the constructor and inside the Start() function.

Any idea how I can get the list of RDNode?

Link to comment
Share on other sites

RDNode[] listnode = GameObject.FindObjectsOfType<RDNode>();

This always return an empty array.

I am in career mode, I am loading the plugin once in KSPAddon.Startup.SpaceCentre.

I have tried it inside the constructor and inside the Start() function.

Any idea how I can get the list of RDNode?

RDNodes are the actual GOs that make up the tech tree. Is that what you're after? They only exist if the R&D window is open. Luckily there's an event that lets you know when that's about to happen, so you can use this:

[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
public class UbioCountNodes : MonoBehaviour
{
void Start() { GameEvents.onGUIRnDComplexSpawn.Add(() => StartCoroutine(CountNodes())); }

System.Collections.IEnumerator CountNodes()
{
yield return null;

Debug.Log("Counting RD nodes ...");
var nodes = GameObject.FindObjectsOfType<RDNode>();

if (nodes == null || nodes.Length == 0)
{
Debug.LogError("No RDnodes found!");
}
else Debug.Log("Found " + nodes.Length + " RD nodes!");
}
}

If you're instead trying to find out what the state of the player's tech tree is, you want to be working with ResearchAndDevelopment.PartTechAvailable/PartModelPurchased etc

Link to comment
Share on other sites

RDNodes are the actual GOs that make up the tech tree. Is that what you're after? They only exist if the R&D window is open. Luckily there's an event that lets you know when that's about to happen, so you can use this:

[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
public class UbioCountNodes : MonoBehaviour
{
void Start() { GameEvents.onGUIRnDComplexSpawn.Add(() => StartCoroutine(CountNodes())); }

System.Collections.IEnumerator CountNodes()
{
yield return null;

Debug.Log("Counting RD nodes ...");
var nodes = GameObject.FindObjectsOfType<RDNode>();

if (nodes == null || nodes.Length == 0)
{
Debug.LogError("No RDnodes found!");
}
else Debug.Log("Found " + nodes.Length + " RD nodes!");
}
}

If you're instead trying to find out what the state of the player's tech tree is, you want to be working with ResearchAndDevelopment.PartTechAvailable/PartModelPurchased etc

Yes I am trying to get them to see what can be down to make a good career integration for my welding tool.

I will give a try at your solution, thanks.

Link to comment
Share on other sites

Did somebody figure out already how to edit the recovery value of a part or a vessel?

If you try to change the part's cost in the partInfo the cost is changed for every present and future instance of the part apparently -.-

Link to comment
Share on other sites

This is a limitation of 0.24

There's a hacky hacky Majiir thought of, and I thought of trying use a module to force a part to use a new AvailablePart object customized for itself, but...that's about it.

Could you please detail Majiir's hack? :)

Brb, trying your solution right now.

Link to comment
Share on other sites

How do I get the ConfigNode data from .cfg into an INSTANCE of my Part?

I have a PartModule with this:


public override void OnLoad(ConfigNode node)
{
print("FlightDataRecorder: OnLoad");
print("FlightDataRecorder: " + node.ToString());
}

and a config like this:


MODULE
{
name = FlightDataRecorder
foo = bar
num = 42
BODY
{
name = kerbin
value = 1.0
}
SITUATION
{
name = landed
value = 0.0
}
SITUATION
{
name = flying
value = 1.0
}
SITUATION
{
name = prelaunch
value = 0.0
}
}

When the game first starts up and all the parts are being loaded, I see that fire and all the ConfigNode information is in there. However when I start a game, goin to the VAB and add my part, then go to the LaunchPad, that method fires again for that instance of the part, but NONE of my custom ConfigNode information is present.

Link to comment
Share on other sites

I think your issue is that you are nesting nodes inside one another.

I think this is what you are looking for:


public override void OnLoad(ConfigNode node)
{
print("FlightDataRecorder: OnLoad");
print("FlightDataRecorder: " + node.ToString());
ConfigNode bodyNode = node.GetNode("BODY");
print("FlightDataRecorder Body: " + bodyNode.ToString()); //<- Should print Kerbin
print("FlightDataRecorder Body Value: " + bodyNode.GetValue("value")); //<- Should print 1.0
}

Link to comment
Share on other sites

I think your issue is that you are nesting nodes inside one another.

I think this is what you are looking for:


public override void OnLoad(ConfigNode node)
{
print("FlightDataRecorder: OnLoad");
print("FlightDataRecorder: " + node.ToString());
ConfigNode bodyNode = node.GetNode("BODY");
print("FlightDataRecorder Body: " + bodyNode.ToString()); //<- Should print Kerbin
print("FlightDataRecorder Body Value: " + bodyNode.GetValue("value")); //<- Should print 1.0
}

No, the problem is the data doesn't even exist when the part instance is loaded with the actual craft. Its all gone. Even the non-nested primitive data values. It is all there on the first load during game load, and all gone during load of a part on an actual vessel.

Link to comment
Share on other sites

This should be such a basic thing done by tons of mods out there but I can't figure it out :( I must be doing something fundamentally wrong. I even tried storing the data in private variables on that initial load, but as expected since the game spawns off a new instance of my class when the part is added to a vessel, that does no good.

Link to comment
Share on other sites

How are you adding the config node? There are issues with the .addModule() command that mean you just can't simply use that.

At least there were in 0.23.5, I don't know if 0.24 changes how it behaves.

D.

Edited by Diazo
Link to comment
Share on other sites

Wait, you are either loading those values to a persistent KSPField, or saving them in the OnSave() routine correct?

One of KSP's quirks is that on scene load, it runs OnLoad, then almost immediately runs OnSave. If you are not saving the values back, I believe KSP null's the values out in the ConfigNode on that part and so next time it loads, it sees the null values and loads the defaults. (Note that anything in a persistent KSPField saves automatically for you, as long as you call OnSave.)

D.

That sounds like my exact problem then thanks!

On a sidenote, I was going to use KSPFields but I wasn't sure how to do that for complex data types like nested config nodes, which I would store internally as dicts. How would I do that?

Link to comment
Share on other sites

That sounds like my exact problem then thanks!

On a sidenote, I was going to use KSPFields but I wasn't sure how to do that for complex data types like nested config nodes, which I would store internally as dicts. How would I do that?

Heh, you are quick. Check the edit to my previous post for what I think is a more likely culprit.

Having said that, for KSP to save your data to the persistent file you must either:

a) Assign that field the KSPField attribute with persistent = true. Then keep this value updated as whatever value is in this field when OnSave is called will be saved. Any calculations done in OnSave will not save with this method. (My biggest headache with KSPFields).

B) Grab the data directly using node.GetValue() and node.SetValue() for loading and saving. This overrides KSP's automatic save/load of the KSPField. I prefer this method as I know exactly what is being loaded and saved.

For the first method, you are limited to certain data types. Notably double does not work, you have to use float.

For the second method, I only use strings and manually convert everything back and forth. It probably makes my save/load code more complex then it needs to be, but I know exactly what is going on this way.

D.

Link to comment
Share on other sites

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