I got a portion of code to hold game variables.
The Var
struct is a simple one:
struct Var{
/// <summary>
/// Name of variable. Unchangable
/// </summary>
public string Name { get; private set; }
/// <summary>
/// Value of variable. Can be changed via <see cref="Change(string)"/>.
/// </summary>
public string Value { get; private set; }
public Var(string s, string value) {
this.Name = s;
this.Value = value;
}
public void Change(string v) {
new Debug("b", $@"Changing value from ""{Value}"" to ""{v}""", Debug.Importance.ERROR);
this.Value = v;
new Debug("b", $@"Result: ""{Value}""", Debug.Importance.ERROR);
}
}
(new Debug(string, string, Debug.Importance)
is a function that simply calls Conosle.WriteLine()
without using System
in each file)
class GameVars
is a list of those.
I use an indexer for getting a Var
using a variable.
public string this[string s] {
get => vars.Find(e => e.Name == s).Value;
set {
new Debug("a", $@"Attempt to change value of ""{s}"" to ""{value}"".", Debug.Importance.ERROR);
if( vars.Find(e => e.Name == s).Name != null )
vars.Find(e => e.Name == s).Change(value);
else
vars.Add(new Var(s, value));
new Debug("a", $@"Result: ""{this[s]}""", Debug.Importance.ERROR);
}
}
And when in GameVars.LoadFromFile(string s)
I use
this[ident] = value;
new Debug("", $"When do i Fire? {this[ident]}", Debug.Importance.ERROR);
if( this[ident] != value )
throw new System.Exception("Could not change value!");
I get following output:
[ERROR]( a ) Attempt to change value of "version" to "beta 0.0.0.1".
[ERROR]( b ) Changing value from "beta 0.0.0..1" to "beta 0.0.0.1"
[ERROR]( b ) Result: "beta 0.0.0.1"
[ERROR]( a ) Result: "beta 0.0.0..1"
[ERROR]( ) When do i Fire? beta 0.0.0..1
Exception thrown: 'System.Exception' in ITW.exe
An unhandled exception of type 'System.Exception' occurred in ITW.exe
Could not change value!
Why isn't the value changing?
I tried changing everything to public and nothing helped. I checked everything with Console.WriteLine()
and there is nothing to override this value. It just does not change to new one.