Jump to content

When ec < 1 then retract radiators


Recommended Posts

Hi all

So I gots me a problem ... I'm trying to figure out how to have a part that uses ModuleDeployableRadiator retract the radiators if ec < 1

I have a part that uses ModuleDeployableRadiator and I want to have it so that if the EC gets below 1 then then retract animation is fired

Any help is appreciated

Link to comment
Share on other sites

Just now, linuxgurugamer said:

Basicely, you need to write some C# code.

made a new module derived from ModuleDeployableRadiator, and in the new module you can watch the ec, etc

This is what I am asking about ... I have little knowledge concerning C# and was hoping someone could point me in the right direction

Link to comment
Share on other sites

7 hours ago, linuxgurugamer said:

Im just curious, why retract the radiators if the EC is below a certain number?

I have been working on energy shields (Another joint effort from DCK Inc and SM Industries) and use the ModuleDeployableRadiator to deploy and retract them as it is set up to draw ec when deployed

I would like to have them be retracted when the ec gets low to simulate failing shields due to lack of power

I'm part way there (I think) but am hoping to find that last piece to the puzzle so I can move to hooking into the Radar Warning Receiver in BDAc so that when it goes off the shields are triggered

This is what I have so far:

Spoiler

using System.Collections.Generic;

namespace DCKShields
{
    public class DCKShields : PartModule
    {
        [KSPField] public double resourceDrain = 0;

        public override void OnStart(StartState state)
        {
            
        }

        void OnDestroy()
        {
            GameEvents.onVesselCreate.Remove(OnVesselCreate);
        }

        void OnVesselCreate(Vessel v)
        {

        }

        public void retractShields()
        {
            Part root = EditorLogic.RootPart;
            if (!root)
                return;            // find all ModuleDeployableRadiator modules on all parts
            List<ModuleDeployableRadiator> shieldParts = new List<ModuleDeployableRadiator>(200);
            foreach (Part p in EditorLogic.fetch.ship.Parts)
            {
                shieldParts.AddRange(p.FindModulesImplementing<ModuleDeployableRadiator>());
            }
            foreach (ModuleDeployableRadiator shieldPart in shieldParts)
            {
                shieldPart.Retract();
            }
        }

        public override void OnFixedUpdate()
        {
            if (resourceDrain <= 0)
            {
                return;
            }

            double drainAmount = resourceDrain * TimeWarp.fixedDeltaTime;
            double chargeAvailable = part.RequestResource("ElectricCharge", drainAmount, ResourceFlowMode.ALL_VESSEL);
            if (chargeAvailable < 1.0f)
            {
                retractShields();
            }
        }
    }

}

I did mention that I don't know what I am doing, yes?

l1TxbR0.png

Edited by DoctorDavinci
Link to comment
Share on other sites

You will need more references in your code. Put 'using UnityEngine;' to let it use unity. 

Your variables arent being set, so resourceDrain is always going to be zero. You will need a Δelectricity. 

Otherwise, it looks ok. Once you have it written, run it throuh a compiler for testing. 

E: I'll ping @DoctorDavinci

Edited by Benjamin Kerman
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...