Jump to content

Enable a tech node based on building level / How to remove the Parent node from an RDNode


Recommended Posts

I'm trying to make a tech tree with all the science parts grouped in a couple of nodes, so unlocking them with science doesn't make a lot of sense to me, I thought I could use CustomBarnKit to have more building levels or just use Komplexity and tie the science parts to the R&D building level or something like that. I know ContractConfigurator can also unlock tech so that's another option, to tie them to contracts that autocomplete when you accept them, with requirements being achieved orbit, visited the Mun, etc., but I still want to put my code out here in case it helps anyone.

You can say this is a solution to this question: Tying tech tree nodes to VAB level?, but I'd rather just enable the nodes, not outright unlock them if that's an option.

I found here how to list the RDNodes and went on the ProtoRDNode route.

Excuse my code, I've barely touched C# before so I'm sure it can be optimized.

[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
    public class Hello : MonoBehaviour
    {
        public void Start()
        {
          	// not sure what else to tie the update to, it should be whenever a building is upgraded
            RDController.OnRDTreeSpawn.Add(OnRDTreeSpawn);

            void OnRDTreeSpawn(RDController rd)
            {
                int rdLevel = 0;

                // get the current level of the RD facility, but we could use various building levels with various .cfg values if we wanted
                foreach (UpgradeableFacility facility in GameObject.FindObjectsOfType<UpgradeableFacility>()) 
                {
                    if (facility.name == "ResearchAndDevelopment")
                    {
                        rdLevel = facility.FacilityLevel;
                    }
                }
				
              	// this should get all the nodes from the tech tree config
                ConfigNode techConfigNode = ConfigNode.Load(KSPUtil.ApplicationRootPath + "GameData/ModuleManager.TechTree");
                techConfigNode = techConfigNode.GetNode("TechTree");
                foreach (ConfigNode rdConfigNode in techConfigNode.GetNodes("RDNode"))
                {
                    // find the nodes in the config which have the rdTier custom value declared in the .cfg file
                    if (!string.IsNullOrEmpty(rdConfigNode.GetValue("rdTier")))
                    {
                        // need the id to match the config node to the actual tree node in-game
                        String tieredNodeId = rdConfigNode.GetValue("id");
						
                      	// then we iterate the in-game nodes for every tieredNodeId we find
                        foreach (ProtoRDNode pnode in AssetBase.RnDTechTree.GetTreeNodes())
                        {
                            if (pnode.tech.techID == tieredNodeId)
                            {
                                Debug.Log(pnode.tech.techID + " matches " + tieredNodeId);
								
                              	// only unlocking if our building level is greater or equals to the required tier in the .cfg file
                                if (rdLevel >= Int16.Parse(rdConfigNode.GetValue("rdTier")))
                                {
                                    
                                  	// This researches the actual node without user input and it works
                                  	// but since my function is tied to tech tree load, RD building has to be closed and opened again to update
                                    // ResearchAndDevelopment.Instance.UnlockProtoTechNode(pnode.tech); 
                                    
                                  	pnode.tech.state = RDTech.State.Available; // This doesn't seem to work

                                    // some tech trees make permanent locked nodes for informational purposes by making them their own parent
                                    // getting rid of the parent node should make it available, but none of this works
                                    pnode.parents = null;
                                    rdConfigNode.RemoveNodes("Parent");
                                    pnode.Save(rdConfigNode);
                                   
                                }
                            }
                        }

 

 

Edited by IC-Adel
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...