• We have updated our Community Code of Conduct. Please read through the new rules for the forum that are an integral part of Paradox Interactive’s User Agreement.

Zarine

Field Marshal
134 Badges
Feb 28, 2007
4.827
689
  • Europa Universalis IV: Res Publica
  • Gettysburg
  • Hearts of Iron III
  • Heir to the Throne
  • Europa Universalis III Complete
  • Knights of Pen and Paper +1 Edition
  • Leviathan: Warships
  • The Kings Crusade
  • Magicka
  • Majesty 2
  • Majesty 2 Collection
  • March of the Eagles
  • Europa Universalis III Complete
  • For the Motherland
  • Victoria: Revolutions
  • Rome Gold
  • Semper Fi
  • Sengoku
  • Ship Simulator Extremes
  • Sword of the Stars
  • Supreme Ruler: Cold War
  • The Showdown Effect
  • Victoria 2
  • Victoria 2: A House Divided
  • Victoria 2: Heart of Darkness
  • Hearts of Iron IV: No Step Back
  • Crusader Kings II: Sword of Islam
  • Arsenal of Democracy
  • Hearts of Iron II: Armageddon
  • Cities in Motion
  • Cities in Motion 2
  • Crusader Kings II
  • Crusader Kings II: Charlemagne
  • Crusader Kings II: Legacy of Rome
  • Crusader Kings II: The Old Gods
  • Crusader Kings II: Rajas of India
  • Crusader Kings II: The Republic
  • Crusader Kings II: Sons of Abraham
  • Crusader Kings II: Sunset Invasion
  • A Game of Dwarves
  • Commander: Conquest of the Americas
  • Darkest Hour
  • East India Company Collection
  • Europa Universalis III
  • Europa Universalis III Complete
  • Divine Wind
  • Europa Universalis IV
  • Europa Universalis IV: Art of War
  • Europa Universalis IV: Conquest of Paradise
  • Europa Universalis IV: Wealth of Nations
[Coding] Problem accessing values - Solved

Somehow I have difficulties accessing some values with my current code.
It is compiling but when I run it, it stops in the middle of the execution and I have no idea why.

I hope someone could help me find the problem.

Here is the code :
Code:
        public class modSlider : UISlider
        {
            public UILabel _label;

            public void setSlider(UIPanel panel)
            {
                this.maxValue = 100;
                this.minValue = 0;
                this.stepSize = 1;
                this.value = 50;

                var input = (UILabel)panel.AddUIComponent(typeof(UILabel));
                input.position = this.position + new Vector3(110, 0, 0);
                input.text = this.value.ToString();

                _label = input;
            }

            protected override void OnValueChanged()
            {
                //this._label.text = this.value.ToString();
                DebugOutputPanel.AddMessage(ColossalFramework.Plugins.PluginManager.MessageType.Message, "current value: " + this.value.ToString());
                //DebugOutputPanel.AddMessage(ColossalFramework.Plugins.PluginManager.MessageType.Message, "text value: " + this._label.text);
            }
        }

So I have this UISlider derived class with my nice fonction to set them up and create an associated UILabel and filling it with the current value of the slider.
And I store the UILabel within the UISlider derived class for easy access. All this is working like a charm.

Then I wanted to add that when the value changes, the text associated change has well. And I ran into my problem.
Whenever I try to read or write in this._label.text, the script stops (I know that because some other modifications are not visible in the game). Removing any of the commented line cause the issue.
And I have no clue what is wrong with this part of the code.

Any idea ?


Ok, after many test I found the issue. Somehow it was trying to access the data before _label was even initialized causing it to be null or something like that.

Corrected code is
Code:
        public class modSlider : UISlider
        {
            public UILabel _label;

            public void setSlider(UIPanel panel)
            {
                var input = (UILabel)panel.AddUIComponent(typeof(UILabel));
                _label = input;

                this.maxValue = 100;
                this.minValue = 0;
                this.stepSize = 1;
                this.value = 50;

                _label.position = this.position + new Vector3(110, 0, 0);
                _label.text = this.value.ToString();
            }
        }
 
Last edited:
yah.. and you don't even need var input

Code:
public class modSlider : UISlider
        {
            public UILabel _label;

            public void setSlider(UIPanel panel)
            {
                maxValue = 100;
                minValue = 0;
                stepSize = 1;
                value = 50;

                _label = (UILabel)panel.AddUIComponent(typeof(UILabel));
                _label.position = this.position + new Vector3(110, 0, 0);
                _label.text = this.value.ToString();
            }
        }