Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | ihidanaka |
C#: Why if I declare a variable in the class body before _Ready and change its value in _Ready, in _PhysicsProcess it has the value it had when it was declared?
TimeSystem.cs
using Godot;
using System;
public class TimeSystem : Control
{
private float _ticks;
private float _intTime;
private float _seconds;
private float _minutes;
private float _minutesPrev;
private float _hours;
private float _hoursPrev;
private float _days;
private float _daysPrev;
private float _seasons;
private float _seasonsPrev;
private float _years;
private float _yearsPrev;
[Signal]
delegate void SecondsChanged(int seconds);
[Signal]
delegate void MinutesChanged(int minutes);
[Signal]
delegate void HoursChanged(int hours);
[Signal]
delegate void DaysChanged(int days);
public override void _Ready()
{
_ticks = (float)SaveSystem.getSaveData("save.save", "TimeSystem")["Ticks"].ToString().ToFloat();
}
public override void _Process(float delta)
{
base._Process(delta);
if (_minutes != _minutesPrev)
{
EmitSignal("MinutesChanged", _minutes);
_minutesPrev = _minutes;
}
if (_hours != _hoursPrev)
{
EmitSignal("HoursChanged", _hours);
_hoursPrev = _hours;
}
if (_days != _daysPrev)
{
EmitSignal("DaysChanged", _days);
_daysPrev = _days;
}
}
public override void _PhysicsProcess(float delta)
{
base._PhysicsProcess(delta);
_ticks += (delta * 60);
GD.Print(_ticks);
// EmitSignal("TicksChanged", _ticks);
_intTime = (int)(_ticks);
_seconds = (int)(_intTime % 60);
_minutes = (int)Math.Truncate((_intTime / 60) % 60);
EmitSignal("MinutesChanged", _minutes);
_hours = (int)Math.Truncate((_intTime / (60 * 60)) % 24);
EmitSignal("HoursChanged", _hours);
_days = (int)Math.Truncate((_intTime / (60 * 60 * 24)) % 10);
EmitSignal("DaysChanged", _days);
_seasons = (int)Math.Truncate((_intTime / (60 * 60 * 24 * 10)) % 4);
_years = (int)Math.Truncate((_intTime / (60 * 60 * 24 * 10 * 4)));
}
public Godot.Collections.Dictionary Save()
{
return new Godot.Collections.Dictionary()
{
{ "Filename", Filename },
{ "Parent", GetParent().GetPath() },
{ "Ticks", _ticks },
{ "Seconds", _seconds}, // Vector2 is not supported by JSON
{ "Minutes", _minutes },
{ "Hours", _hours },
{ "Days", _days },
{ "Seasons", _seasons },
{ "Years", _years },
};
}
}
getSaveData() from SaveSystem.cs
static public Godot.Collections.Dictionary getSaveData(string saveName, string key) {
Godot.File saveGame = new File();
if (!saveGame.FileExists("user://" + saveName))
return null; //
saveGame.Open("user://" + saveName, File.ModeFlags.Read);
Godot.Collections.Dictionary nodeData = new Godot.Collections.Dictionary((Godot.Collections.Dictionary)JSON.Parse(saveGame.GetAsText()).Result);
saveGame.Close();
return (Godot.Collections.Dictionary)nodeData[key];
}
If I declare variable and try assign value in _Ready:
public override void _Ready()
{
_ticks = (float)SaveSystem.getSaveData("save.save", "TimeSystem")["Ticks"].ToString().ToFloat();
}
in output I had two _ticks values: 84000(from save file) and 0 together:
84001
1
84002
2
84003
3
84004
4
84005
5
......
But if I assign a value when declaring, then I get what I need:
private float _ticks = (float)SaveSystem.getSaveData("save.save", "TimeSystem")["Ticks"].ToString().ToFloat();
public override void _Ready()
{
_ticks = (float)SaveSystem.getSaveData("save.save", "TimeSystem")["Ticks"].ToString().ToFloat();
}
84001
84001
84002
84002
84003
84003
84004
84004
84005
84005
But also, for some reason,I can see 2 outputs, as if I have 2 variables with different addresses in memory cells. Maybe someone can explain why this is happening and how in this case it is better to declare variables to set a value in _Ready and use it in _PhysicsProcess, because, in my opinion, assigning a value from saving in the body of the class, and not in _Ready, can bring problems in the future