Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

battery kW not working properly in a script

Discussion in 'Programming (In-game)' started by Burkeybumkins, Mar 6, 2019.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Burkeybumkins Trainee Engineer

    Messages:
    2
    I'm currently writing a script for storing battery information.

    This is a rundown of what I'm doing:

    1. I'm taking the power information received from all my batteries via .DetailedInfo and splitting it into a matrix

    for (int i = 0; i < batteryCount; i++)
    {
    batteryInfo = batteries.DetailedInfo;
    var batteryInfoMatrix = batteryInfo.Split(' ' , '\n');

    This should split up all the words into individual strings

    2. After this I send the power output and the power label to a method to be reduced to their value in basic Watts.
    In this case these values would be 1.6 and kW.
    This is the method I use:

    static double WattToBasic (double d, string s) //Method 1.1 - reduces watt value to Watts
    {
    double Result = 0;
    double D = d;
    string S = s;

    if (S == "Kw" || S == "kWh") //This is where the bug is
    {
    Result = D * 1000;
    }
    if (S == "MW" || S == "MWh")
    {
    Result = D * 1000000;
    }
    else
    {
    Result = D;
    }

    return Result;
    }

    This method works for MW and MWh strings, but doesn't for kW for whatever reason. I have tried using a variation of uppercase and lowercase for "kw". When sending kW to this method it always goes to "else". I have no Idea why this is the case.

    I have tried to add S = S.ToUpper and S.ToLower to keep the letters in a uniform case and it works for megawatts, but not for kilowatts.

    My question is, why doesn't Kw work with this method? In what format is kW when it is drawn from DetailedInfo and why cant I manipulate it?

    I can submit all of my code if anyone wants, but it is about 300 lines and isn't fully completed.
     
  2. plaYer2k Master Engineer

    Messages:
    3,160
    You wrote "Kw" instead of the correct "kW". That may be the reason, even if you said that you did to lower or upper case comparisons. Though i didnt check the DetailedInfo in ages.

    Also there is a huge difference between "W" and "Wh". Watt [W] is the unit for power while Watthour [Wh] is the unit for energy.


    Overall tho, you should use the properties IMyBatteryBlock implements, despite its horrible name and avoid parsing the localized strings DetailedInfo supplies.
    Code:
    float CurrentStoredPower
    
    That is the Energy a battery has stored and what you are looking for.

    Thus
    Code:
    float TotalEnergy(IMyBatteryBlock[] batteryBlocks)
    {
    	float energy = 0f;
    	foreach(IMyBatteryBlock battery in batteryBlocks)
    	{
    		energy += battery.CurrentStoredPower;
    	}
    	return energy
    }
    
    The value should be in MWh i think, but i am not sure tbh.
     
    Last edited: Mar 6, 2019
    • Informative Informative x 1
  3. Burkeybumkins Trainee Engineer

    Messages:
    2
    Thanks! This is a great alternative solution to my question. I am new to space engineers and I'm still figuring out how their version of C# works. This shed some light on some things. Again, thanks!
     
Thread Status:
This last post in this thread was made more than 31 days old.