Godot Version 4.2.1 Mono (C#)
Question
I want to create classes to store game session and game information:
Constants
Session Statistics
Session state
And I have now written the first two classes that I inherited from RefCounted and wondered if it would be better to inherit from GodotObject (Object). Here are the two classes I wrote:
SessionStats.cs
using Godot;
namespace Game.Data
{
public partial class SessionStats: GodotObject
{
/// <summary>
/// Called when the value of money, experience or level is changed
/// so that the GUI will request the new information to display correctly.
/// </summary>
[Signal] public delegate void ChangeValuesEventHandler();
/// <summary>
/// Called each time the level is changed to bring up the new skill selection window.
/// </summary>
/// <param name="_previousLevel"></param>
/// <param name="_currentLevel"></param>
[Signal] public delegate void ChangeLevelEventHandler(int _previousLevel, int _currentLevel);
private int _currentMoney = 0;
private int _currentExperience = 0;
private int _currentLevel = 1;
public int CurrentMoney
{
get
{
return _currentMoney;
}
set
{
_currentMoney = value;
EmitSignal(SignalName.ChangeValues);
}
}
public int CurrentExperience
{
get
{
return _currentExperience;
}
set
{
_currentExperience = value;
int bufferLevel = CurrentLevel;
for (int i = bufferLevel; ;i++)
{
if (_currentExperience > bufferLevel * 50)
{
_currentExperience -= bufferLevel * 50;
bufferLevel += 1;
}
else
{
break;
}
}
if (bufferLevel > CurrentLevel)
{
CurrentLevel = bufferLevel;
}
EmitSignal(SignalName.ChangeValues);
}
}
public int CurrentLevel
{
get
{
return _currentLevel;
}
set
{
EmitSignal(SignalName.ChangeLevel, _currentLevel, value);
_currentLevel = value;
}
}
public int GetMoney()
{
return CurrentMoney;
}
public int GetExperience()
{
return CurrentExperience;
}
public int GetLevel()
{
return CurrentLevel;
}
/// <summary>
/// Adds money to the current money.
/// </summary>
/// <param name="what">
/// Number of money to be added.
/// </param>
public void AddMoney(int what)
{
CurrentMoney += what;
}
/// <summary>
/// Adds XP to the current experience.
/// </summary>
/// <param name="what">
/// Number of XP to be added.
/// </param>
public void AddExperience(int what)
{
CurrentExperience += what;
}
/// <summary>
/// Adds level to the current level.
/// </summary>
/// <param name="what">
/// Number of levels to be added.
/// </param>
public void AddLevel(int what)
{
CurrentLevel += what;
}
}
}
Constants.cs:
using Godot;
namespace Game.Data
{
public partial class Constants : GodotObject
{
public const short limitHealth = short.MaxValue;
public const short limitSpeed = short.MaxValue;
public const short limitDamage = short.MaxValue;
}
}