1. This forum is obsolete and read-only. Feel free to contact us at support.keenswh.com

Object reference not set to an instance of an object

Discussion in 'Programming (In-game)' started by andreychano, Jun 24, 2020.

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

    andreychano Trainee Engineer

    Messages:
    2
    using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using VRageMath;
    using VRage.Game;
    using Sandbox.ModAPI.Interfaces;
    using Sandbox.ModAPI.Ingame;
    using Sandbox.Game.EntityComponents;
    using VRage.Game.Components;
    using VRage.Collections;
    using VRage.Game.ObjectBuilders.Definitions;
    using VRage.Game.ModAPI.Ingame;
    using SpaceEngineers.Game.ModAPI.Ingame;
    using Sandbox.Game.Replication;

    public sealed class Program2 : MyGridProgram
    {
    //

    //

    IMyTextPanel LCD;
    IMySolarPanel SolarPanel;
    float MaxOutput, MinOutput;
    float CurrentOutput;
    IMyReflectorLight Light;
    Color greencolor = new Color(0, 255, 0);
    Color yellowcolor = new Color(255, 255, 0);
    Color orangeredcolor = new Color(255, 120, 0);
    Color redcolor = new Color(255, 0, 0);




    //methods
    Program2()
    {
    MinOutput = 0;
    SolarPanel = GridTerminalSystem.GetBlockWithName("SolarPanel") as IMySolarPanel;
    LCD = GridTerminalSystem.GetBlockWithName("LCD") as IMyTextPanel;
    Light = GridTerminalSystem.GetBlockGroupWithName("Light") as IMyReflectorLight;
    Runtime.UpdateFrequency = UpdateFrequency.Update10;
    }

    void Main(string argument)
    {

    MaxOutput = SolarPanel.MaxOutput;
    CurrentOutput = SolarPanel.CurrentOutput;
    LCD.WriteText("Output:" + MaxOutput + "mW",false);
    LCD.WriteText("\n Total use:" + CurrentOutput * 1000 + "kW", true);
    if (SolarPanel.MaxOutput > MaxOutput) MaxOutput = SolarPanel.MaxOutput;
    if (SolarPanel.MaxOutput < MinOutput) MaxOutput = SolarPanel.MaxOutput;
    LCD.WriteText("\n Max Output:" + MaxOutput * 1000 + "kW", true);
    LCD.WriteText("\n Min Output:" + MinOutput *1000 + " W", true);


    float FULL = 0.12f;
    float FULL2 = 0.1f;
    float HALF = 0.065f;
    float HALF2 = 0.03f;

    if (MaxOutput > FULL)
    {
    Light.SetValueColor("Color", greencolor);
    }
    else if (MaxOutput > FULL2)
    {
    Light.SetValueColor("Color", yellowcolor);
    }
    else if (MaxOutput > HALF)
    {
    Light.SetValueColor("Color", orangeredcolor);
    }
    else if (MaxOutput > HALF2)
    {
    Light.SetValueColor("Color", redcolor);
    }
    else if (MaxOutput < HALF2)
    {
    Light.SetValueColor("Color", redcolor);
    }
    }
    public void Save()
    {

    }
    //-----------------------------processes


    //-----------------------------------
    //end
    }




    PLS help me to make it works
     
  2. Byful

    Byful Trainee Engineer

    Messages:
    3
    Don't have a lot of practice with space engineers api but it appears that your trying to set an action into a group that hasn't been referenced yet thus throwing that error code. You first need to add the blocks in the Light block group into a List variable. Also C# has build in Color codes that you can access, typing in "Color." will bring up a list of available colors. Still learning programming so I'm sure someone here will find an error within my code but this works when changing the lights color. Note this will change all lights in that "Light" group, if you wanna specific interiors lights(IMyInteriorLight) or spotlights(IMyReflectorLight) then just change it in List declaration. You can attempt the script again with this code. Replace the floats up to the end of the Main method. Hopefully this helps.


    float FULL = 0.12f, FULL2 = 0.1f, HALF = 0.065f, HALF2 = 0.03f;

    IMyBlockGroup lightGroup = GridTerminalSystem.GetBlockGroupWithName("Light");
    if (lightGroup == null)
    {
    Echo("Light Group not found");
    return;
    }
    List<IMyLightingBlock> lightList = new List<IMyLightingBlock>();
    lightGroup.GetBlocksOfType(lightList);
    foreach (var block in lightList)
    {
    if (MaxOutput > FULL)
    {
    block.SetValueColor("Color", Color.Green);
    }
    else if (MaxOutput > FULL2)
    {
    block.SetValueColor("Color", Color.Yellow);
    }
    else if (MaxOutput > HALF)
    {
    block.SetValueColor("Color", Color.Orange);
    }
    else if (MaxOutput > HALF2)
    {
    block.SetValueColor("Color", Color.Red);
    }
    else if (MaxOutput < HALF2)
    {
    block.SetValueColor("Color", Color.Red);
    }
    }
     
Thread Status:
This last post in this thread was made more than 31 days old.