[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 :
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
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: