Jump to content

[RESOLVED] How To Determine Variable State from VAB/SPH to Launchpad?


Recommended Posts

EDIT: See next post.

I have a part with a bool value which enables/disables the renderer for a component. I can set it in the VAB/SPH and the component disappears and reappears. However, when I go to the launchpad, the part is created with the invisible component as visible.

I've looked through some other people's code to determine how they do it. No joy.

using System.Collections;

using UnityEngine;

namespace RenderTest

{

public class RenderTest : PartModule

{

private bool blueVisible = true;

private Component compBlue;

public override void OnStart(PartModule.StartState state)

{

compBlue = part.FindModelComponent<Component>("blue");

}

[KSPEvent(active = true, guiActive = true, guiActiveEditor = true, guiName = "Invisible")]

public void ClickRed()

{

blueVisible = ! blueVisible;

Debug.Log("VAB: " + blueVisible);

Events["ClickRed"].guiName = (blueVisible? "Invisible" : "Visible");

compBlue.renderer.enabled = (blueVisible? true : false);

}

public void onLoad()

{

Debug.Log("onLoad: " + blueVisible);

compBlue.renderer.enabled = (blueVisible ? true : false);

}

}

}

Edited by Apollo13
Link to comment
Share on other sites

The code now looks like this. However, the value of "blueVisible" is not maintained between the VAB and Launchpad. That is, in the VAB, I set it to False. Upon going to the Launchpad, it's True again. Yet, the Attribute state(Events["ClickRed"].guiName) is maintained.

sing System.Collections;

using UnityEngine;

namespace RenderTest

{

public class RenderTest : PartModule

{

private bool blueVisible = true;

private Component compBlue;

public override void OnStart(PartModule.StartState state)

{

compBlue = part.FindModelComponent<Component>("blue");

Debug.Log("state = " + state);

Debug.Log("OnStart " + blueVisible);

compBlue.renderer.enabled = blueVisible;

}

[KSPEvent(active = true, guiActive = true, guiActiveEditor = true, guiName = "Invisible")]

public void ClickRed()

{

blueVisible = ! blueVisible;

Debug.Log("ClickRed: " + blueVisible);

Events["ClickRed"].guiName = (blueVisible? "Invisible" : "Visible");

compBlue.renderer.enabled = blueVisible;

}

}

}

Edited by Apollo13
Link to comment
Share on other sites

"isPersistent" was the final piece of the puzzle. THANK YOU.

Final code:

using System.Collections;

using UnityEngine;

namespace RenderTest

{

public class RenderTest : PartModule

{

[KSPField(isPersistant = true, guiActive = false)]

private bool blueVisible = true;

private Component compBlue;

public override void OnStart(PartModule.StartState state)

{

compBlue = part.FindModelComponent<Component>("blue");

compBlue.renderer.enabled = blueVisible;

}

[KSPEvent(active = true, guiActive = false, guiActiveEditor = true, guiName = "Invisible")]

public void ClickRed()

{

blueVisible = ! blueVisible;

Events["ClickRed"].guiName = (blueVisible? "Invisible" : "Visible");

compBlue.renderer.enabled = blueVisible;

}

}

}

It's strange that I needed to use a KSP extension. There should be a Unity method to maintain component states across scene changes. I'l research this further.

@m4v: tried to give you a forum Rep point; no can do. So, my gratitude is all I can offer.

Edited by Apollo13
Link to comment
Share on other sites

There should be a Unity method to maintain component states across scene changes. I'l research this further.

As far as I am aware, this does not work because it is not the same object in the Editor and Flight.

After saving to the .craft file, everything in the Editor scene is deleted and destroyed and then once the flight scene loads it creates the vessel from the .craft file.

Therefore anything you want to carry over has to save to the .craft file somehow and the easiest way to do that is save the value via the KSPField as you have done.

D.

Edited by Diazo
Link to comment
Share on other sites

DontDestroyOnLoad(gameObject); ?

Edit : err, I did not read the thread properly. This won't work for a partmodule.

It's not a exactly a unity problem, it's the way KSP load and unload ship.

Edited by sarbian
Link to comment
Share on other sites

As far as I am aware, this does not work because it is not the same object in the Editor and Flight.

After saving to the .craft file, everything in the Editor scene is deleted and destroyed and then once the flight scene loads it creates the vessel from the .craft file.

Therefore anything you want to carry over has to save to the .craft file somehow and the easiest way to do that is save the value via the KSPField as you have done.

That is good info which will be used in many mods to come. Thank you. Added a forum Rep point to you as well.

Link to comment
Share on other sites

Thank you for the rep.

I struggled through pretty much exactly the same question myself in order to get my action group mod to work between the editor and flight mode, you have to write the data to a partModule and let KSP handle the save/loading via persistence or you have to write your own data storage methods.

D.

Link to comment
Share on other sites

I have not actually tested this, but I believe the KSPFields not being set public is only an issue if an external source (such as another mod, or another instance of your mod on another part) tries to reference it via the partModule.GetField("YourField"); method as to the best of my knowledge the partModule.Fields list is both public and automatically populated with all KSPFields on your partModule regardless of whether they are set to public or private.

So if an external source finds makes a call to the KSPField it knows exists because of its presence in the partModule.Fields list, but that KSPField is set to private, I expect an error of some sort would be thrown. I'm not sure what type, but it would stop the code execution the same as a nullRef error as it is almost that anyway.

D.

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