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.

Change the Color of an Object Using the Modding API?

Discussion in 'Modding API' started by lance13c, Jan 9, 2015.

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

    Messages:
    2
    Hello everyone, recently I have been learning and playing around with the new modding API. I was wondering if anyone knew how to color blocks in the game by using the modding API, such as changing the color of a sensor or light armor block with the API? The closest thing I found was the disco ship script, but it is using a CubeGrid and I was wondering if I could just change one specific object's color. This color would be the same as using the "p" tool in space engineers to paint a block. Any help would be appreciated.
     
  2. midspace Senior Engineer

    Messages:
    2,224
    To color individual cube, you will still have to use the same cubeGrid.ColorBlocks() method, but use the block position to set the individual block.

    Code:
    cubeGrid.GetBlocks(blocks);
    foreach (var block in blocks)
    {
        // use the block position to set individual blocks.
        cubeGrid.ColorBlocks(block.Position, block.Position, VRageMath.Color.Yellow.ToHsvColor());
    }
    
    ...
    
    public static class Extensions
    {
        public static Vector3 ToHsvColor(this VRageMath.Color color)
        {
            var hsvColor = color.ColorToHSV();
            return new Vector3(hsvColor.X, hsvColor.Y * 2f - 1f, hsvColor.Z * 2f - 1f);
        }
        public static VRageMath.Color ToColor(this Vector3 hsv)
        {
            return new Vector3(hsv.X, (hsv.Y + 1f) / 2f, (hsv.Z + 1f) / 2f).HSVtoColor();
        }
    }
    
     
  3. lance13c Trainee Engineer

    Messages:
    2
    This was helpful but I'm still having trouble with setting one block. I am now able to set all the blocks in the world to a different color with this code.
    Code:
    var cubeGrid = (Sandbox.ModAPI.IMyCubeGrid)Entity;
    cubeGrid.ColorBlocks(cubeGrid.Min,cubeGrid.Max,VRageMath.Color.Green.ToHsvColor());
    
    How would I get a block like sensor. I have a sensor in another class, but I don't know how I could transfer that sensor data into my cube grid class.

    Sensor Class:
    Code:
    namespace TestScript1
    {
        [MyEntityComponentDescriptor(typeof(MyObjectBuilder_SensorBlock))]
        public class EvilSensor : MyGameLogicComponent
        {
            bool toggleColor = false;
    
            MyObjectBuilder_EntityBase m_objectBuilder = null;
            static String[] OreNames;
             Sandbox.ModAPI.IMySensorBlock Sensor;
             Sandbox.ModAPI.IMyUtilities Util;
             Sandbox.ModAPI.IMyCubeGrid cubeGrid;
            public override void Close()
            {
                Sensor.StateChanged -= sensor_StateChange;
            }
            public override void Init(MyObjectBuilder_EntityBase objectBuilder)
            {
                if(OreNames == null)
                {
                    MyDefinitionManager.Static.GetOreTypeNames(out OreNames);
                }
                //cubeGrid = (Sandbox.ModAPI.IMyCubeGrid)Entity;
     
                m_objectBuilder = objectBuilder;
                Sensor = Entity as Sandbox.ModAPI.IMySensorBlock;
                Sensor.StateChanged += sensor_StateChange;
            }
            
            public override MyObjectBuilder_EntityBase GetObjectBuilder(bool copy = false)
            {
                //List<IMyCargoContainer>()
                return m_objectBuilder;
                 
            }
            void sensor_StateChange(bool obj)
            {
                //cubeGrid.ColorBlocks(Sensor.Position, Sensor.Position, VRageMath.Color.Red.ToHsvColor());
                //We want to spawn ore and throw it at entity which entered sensor
                var c = VRageMath.ColorExtensions.ColorToHSV(VRageMath.Color.FromNonPremultiplied(1, 255, 1, 255));
                if (toggleColor)
                {
                    //Sensor.SetColorMaskForSubparts(VRageMath.Color.Turquoise);
                    //Sensor.UpdateVisual();
                    toggleColor = false;
                }
                else
                {
                    //Sensor.SetColorMaskForSubparts(VRageMath.Color.Red);
                    //Sensor.UpdateVisual();
                    toggleColor = true;
                }
                //MyAPIGateway.Entities.GetEntityByName("Sensor").
                MyAPIGateway.Utilities.ShowNotification(string.Format("Test Display {0}", toggleColor), 1000, MyFontEnum.Blue);
                MyObjectBuilder_FloatingObject floatingBuilder = new MyObjectBuilder_FloatingObject();
                floatingBuilder.Item = new MyObjectBuilder_InventoryItem() { Amount = 100, Content = new MyObjectBuilder_Ore() { SubtypeName = "Stone"} };
                floatingBuilder.PersistentFlags = MyPersistentEntityFlags2.InScene; // Very important
                floatingBuilder.PositionAndOrientation = new MyPositionAndOrientation()
                {
                    Position = (Sensor.WorldMatrix.Translation + Sensor.WorldMatrix.Forward * 1.5f), //Spawn ore 1.5m in front of the sensor
                    Forward = Sensor.WorldMatrix.Forward.ToSerializableVector3(),
                    Up = Sensor.WorldMatrix.Up.ToSerializableVector3(),         
                };
                var floatingObject = Sandbox.ModAPI.MyAPIGateway.Entities.CreateFromObjectBuilderAndAdd(floatingBuilder);
            } ........
    
    cubeGrid class:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Sandbox.Common;
    using Sandbox.Common.Components;
    using Sandbox.Common.ObjectBuilders;
    using Sandbox.Definitions;
    using Sandbox.Engine;
    using Sandbox.Game;
    using Sandbox.ModAPI;
    using Sandbox.ModAPI.Ingame;
    using Sandbox.ModAPI.Interfaces;
    
    namespace TestScript1
    {
        [MyEntityComponentDescriptor(typeof(MyObjectBuilder_CubeGrid))]
        public class CubeGridColor : MyGameLogicComponent
        {
            List<Sandbox.ModAPI.IMySensorBlock> sensors;
            MyObjectBuilder_EntityBase n_objectBuilder = null;
            Sandbox.ModAPI.IMyCubeGrid cubeGrid;
            public override void Close()
            {
            
            }
            public override void Init(MyObjectBuilder_EntityBase objectBuilder)
            {
                n_objectBuilder = objectBuilder;
                //cubeGrid = Entity as Sandbox.ModAPI.IMyCubeGrid;
                //var cubeGrid = (Sandbox.ModAPI.IMyCubeGrid)Entity;
                //cubeGrid.GetBlocks(sensors);
                //foreach (var sensor in sensors)
               //{
                    //cubeGrid.ColorBlocks(sensor.Position, sensor.Position, VRageMath.Color.Red.ToHsvColor());
                //}
                Entity.NeedsUpdate |= MyEntityUpdateEnum.EACH_100TH_FRAME;
            }
            public override MyObjectBuilder_EntityBase GetObjectBuilder(bool copy = false)
            {
                //List<IMyCargoContainer>()
                return n_objectBuilder;
            }
            public override void MarkForClose()
            {
            }
            public override void UpdateAfterSimulation()
            {
            }
            public override void UpdateAfterSimulation10()
            {
            }
            public override void UpdateAfterSimulation100()
            {
            }
            public override void UpdateBeforeSimulation()
            {
            }
            public override void UpdateBeforeSimulation10()
            {
            }
            public override void UpdateBeforeSimulation100()
            {
                var cubeGrid = (Sandbox.ModAPI.IMyCubeGrid)Entity;
                //var GridTerminalSystem = (Sandbox.ModAPI.Ingame.IMyGridTerminalSystem)Entity;
                List<Sandbox.ModAPI.Ingame.IMyTerminalBlock> sensorBlocks = new List<Sandbox.ModAPI.Ingame.IMyTerminalBlock>();
                //var cubeBlock = (Sandbox.ModAPI.Ingame.IMyCubeBlock)Entity;
                //cubeBlock.colo
                //GridTerminalSystem.GetBlocksOfType<Sandbox.ModAPI.Ingame.IMySensorBlock>(sensorBlocks);
                //Sandbox.ModAPI.Ingame.IMyGridTerminalSystem.GetBlocksOfType    <Sandbox.ModAPI.IMySensorBlock>(sensorBlocks);
                //GridT
                //cubeGrid.ColorBlocks(cubeGrid.Min, cubeGrid.Max, VRageMath.Color.Green.ToHsvColor());
            }
    

    Is it possible to combine these classes? Should I be using a completely different way to get a sensor from the world or platform I'm standing on? Any help would be appreciated.
     
Thread Status:
This last post in this thread was made more than 31 days old.