Jump to content

C# public bool ConsumeEC(double elapsed)


Recommended Posts

simple method/function/sub for a mod (and hopefully other mods I'm working with)

call ConsumeEC(TimeWarp.fixedDeltaTime)

called from OnUpdate and OnFixedUpdate

to check to see if enough EC to consume; if not return false;

if there is enough EC to consume, consume it; return true.

It doesn't seem to be working - ie everything is running fine - except it isn't consuming EC.

 

 

  public override void OnFixedUpdate()
        {
            Log.dbg("OnFixedUpdate");
            if (BiomaticIsEnabled && HighLogic.CurrentGame.Parameters.CustomParams<Options>().UseEC)
            {
                if (ConsumeEC(TimeWarp.fixedDeltaTime) == false)
                {
                    ScreenMessages.PostScreenMessage(Localizer.Format("#Biomatic_PM_EC01")); // "Electric Charge Depleted. Stopping Biomatic Scanning."
                }
            }

            base.OnFixedUpdate();
        }

        public override void OnUpdate()
        {
            Log.dbg("OnUpdate");
            if (!BiomaticIsEnabled) return;
            if (BiomaticIsEnabled && HighLogic.CurrentGame.Parameters.CustomParams<Options>().UseEC)
            {
                if (ConsumeEC(TimeWarp.fixedDeltaTime) == false)
                {
                    ScreenMessages.PostScreenMessage(Localizer.Format("#Biomatic_PM_EC01")); // "Electric Charge Depleted. Stopping Biomatic Scanning."
                }
            }

            //base.OnFixedUpdate();

            base.OnUpdate();
        }
       
        public bool ConsumeEC(double elapsed)
        {
            Log.dbg("ConsumeEC : elapsed: {0}", elapsed);
            double ec = 0, amount = 0;
            if (CheatOptions.InfiniteElectricity == true) { Log.dbg(String.Format("CheatOptions.InfiniteElectricity({0})", CheatOptions.InfiniteElectricity)); return true; }
            else foreach (Part part in ActiveVessel.parts)
                    foreach (PartResource res in part.Resources)
                        if (res.resourceName == "ElectricCharge" && res.amount > 0)
                        {
                            Log.dbg(String.Format("part {0}.{1}:{2}]", part.name, res.resourceName, res.amount));
                            ec += res.amount;  // tally total EC available on ship
                            Log.dbg(String.Format("total EC available {0} ]", ec));
                        }

            amount = ECresourceConsumptionRate * TimeWarp.fixedDeltaTime;
            Log.dbg("EC available: {0} / Consumption Rate: {1} / fixedDeltaTime {2}", ec, ECresourceConsumptionRate, TimeWarp.fixedDeltaTime);
            // if not enough EC to power, then SHut.It.Down
            if (ec < amount) return false;

            //? compute consumption
            //? don't forget to consume the EC needed to power this beast
            part.RequestResource(ElectricChargeID, amount);
            return true;
        }

 

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