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.

[Script Sample] How to get a ship mass

Discussion in 'Questions and Suggestions' started by botelhorui, Oct 1, 2014.

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

    Messages:
    53
    Just finished finding how to get mass from each block on a ship. Damm interfaces not working 100%.

    As you see on the code you need to be constrolling a ship.

    Code:
    namespace offshore{
        [MySessionComponentDescriptor(MyUpdateOrder.Simulation)]
        class account:MySessionComponentBase
        {
            protected override void UnloadData()
            {
                MyAPIGateway.Utilities.MessageEntered -= MessageEnteredHandler;
                base.UnloadData();
            }
            public override void LoadData()
            {
                MyAPIGateway.Utilities.MessageEntered += MessageEnteredHandler;
                base.LoadData();
            }
            private void MessageEnteredHandler1(string messageText, ref bool sendToOthers)
            {
                var ent = MyAPIGateway.Session.ControlledObject.Entity;
                if (messageText.StartsWith("mass") && ent is IMyControllableEntity && ent is IMyCubeBlock)
                {
                    var ship = (ent as IMyCubeBlock).CubeGrid;
                    var lst = new List<IMySlimBlock>();
                    ship.GetBlocks(lst);
                    float mass = 0f;
                    foreach (var sb in lst)                
                    try
                    {
                        var slimBuilder = sb.GetObjectBuilder(); //use this CubeBlockBuilder instead the one you get from a CubeBlock to find definitions
                        var def = MyDefinitionManager.Static.GetCubeBlockDefinition(slimBuilder);
                        mass += def.Mass;
                    }
                    catch (Exception e)
                    {
                        MyAPIGateway.Utilities.ShowMessage("error","Failed to process SlimBlock");
                    }            
                    MyAPIGateway.Utilities.ShowMessage("script",String.Format("Ship Mass = {0:F}",mass));    
                }
            }
        }
    }        
      
        
    
     
  2. moroder Apprentice Engineer

    Messages:
    170
    Thanks for contributing, and also good idea to introduce "[Script sample]" posts. Between the reference from the DLLs themselves and a few sample mods, I've found just looking at scripts others have made very helpful.

    BTW when catching exceptions, I usually (at least while developing) use this:

    Code:
    MyAPIGateway.Utilities.ShowNotification(string.Format("{0}", e.ToString()), 5000);
    
    ...so you get better error info. I also do this top-level since I understood we were supposed to never let an exception bubble up.
     
    Last edited by a moderator: Oct 2, 2014
  3. botelhorui Trainee Engineer

    Messages:
    53
    Yes! I could have used top-level catch but then I woulnd be able to continue processing.

    Also this is what I use with a file logger. Where I want to know exactly an exception happened.

    And since you cant create/throw exceptions we become very limited.
     
Thread Status:
This last post in this thread was made more than 31 days old.