- Home /
RPGStatSystem tutorial problem with clases visibility?
I'm a beginner, I'm watching a series of RPG System Stat tutorials and in lesson 9, when the mentor refactored the project, everything stopped working, he switched to independent classes from MonoBehavior. I downloaded his final project from github, of course the code works. BUT does not work in test scripts, I spent some time and found out that there is a problem with RPGDefaultStat in RPGTestScript. when instantiating RPGDefaultStat we get null why ???? If I directly create a characteristic instead of an instance, let's say a characteristic, it is created, this interested me incredibly https://github.com/jkpenner/RPGSystemTutorial/tree/StatSystemEndPoint github and http://jkpenner.net/blog/2016/5/30/rpgsystem-09-stats-09-rpgstatcollection-v1 https://www.youtube.com/watch?v=n8DnGMovBBc&list=PL9v2wOnRXtUpJGOY2tTa8P5dSal9s0QSJ∈dex=9
RPGTestScript
public class RPGStatTest : MonoBehaviour {
private RPGStatCollection stats;
public RPGDefaultStats newStats;
void Start () {
stats = new RPGDefaultStats();
//newStats = new RPGStatCollection() as RPGDefaultStats;
var health = stats.CreateOrGetStat<RPGAttribute>(RPGStatType.Health);
health.StatName = "health";
health.StatBaseValue = 10;
var statTypes = Enum.GetValues(typeof(RPGStatType));
foreach (var statType in statTypes) {
RPGStat stat = stats.GetStat((RPGStatType)statType);
if (stat != null) {
Debug.Log(string.Format("Stat {0}'s value is {1}",
stat.StatName, stat.StatValue));
}
}
var health2 = stats.GetStat<RPGStatModifiable>(RPGStatType.Health);
health2.AddModifier(new RPGStatModBasePercent(1.0f)); // 200
health2.AddModifier(new RPGStatModBaseAdd(50f)); // 250
health2.AddModifier(new RPGStatModTotalPercent(1.0f)); // 500
health2.UpdateModifiers();
foreach (var statType in statTypes) {
RPGStat stat = stats.GetStat((RPGStatType)statType);
if (stat != null) {
Debug.Log(string.Format("Stat {0}'s value is {1}",
stat.StatName, stat.StatValue));
}
}
}
void ForEachEnum<T>(Action<T> action) {
if (action != null) {
var statTypes = Enum.GetValues(typeof(T));
foreach (var statType in statTypes) {
action((T)statType);
}
}
}
void DisplayStatValues() {
ForEachEnum<RPGStatType>((statType) => {
RPGStat stat = stats.GetStat((RPGStatType)statType);
if (stat != null) {
Debug.Log(string.Format("Stat {0}'s value is {1}",
stat.StatName, stat.StatValue));
}
});
}
}
RPGDefaultScript
public class RPGDefaultStats : RPGStatCollection {
protected override void ConfigureStats() {
PublicConfig();
}
public void PublicConfig()
{
var stamina = CreateOrGetStat<RPGAttribute>(RPGStatType.Stamina);
stamina.StatName = "Stamina";
stamina.StatBaseValue = 10;
var wisdom = CreateOrGetStat<RPGAttribute>(RPGStatType.Wisdom);
wisdom.StatName = "Wisdom";
wisdom.StatBaseValue = 5;
var health = CreateOrGetStat<RPGVital>(RPGStatType.Health);
health.StatName = "Health";
health.StatBaseValue = 100;
health.AddLinker(new RPGStatLinkerBasic(CreateOrGetStat<RPGAttribute>(RPGStatType.Stamina), 10f));
health.UpdateLinkers();
health.SetCurrentValueToMax();
var mana = CreateOrGetStat<RPGVital>(RPGStatType.Mana);
mana.StatName = "Mana";
mana.StatBaseValue = 2000;
mana.AddLinker(new RPGStatLinkerBasic(CreateOrGetStat<RPGAttribute>(RPGStatType.Wisdom), 50f));
mana.UpdateLinkers();
mana.SetCurrentValueToMax();
}
}
For my creation to work, I changed the visibility from protected in the base creation class to public and in my version from RPGTestScript it is created, but why do we get null when creating an RPGDefaultStat instance ?? how is it possible
Answer by logicandchaos · Feb 23, 2021 at 01:58 PM
if RPGDefaultStat is not a monobehaviour then you can not instantiate it, instantiate is a custom constructor only for monobehaviours and you have to use the new keyword. The new keyword is not used for monobehaviours because they use instantiate. No matter which way you create, it is creating an instance of the class. If it is null that is because the instance was not created or you did not create the reference.
Thanks! It's amazing. But RPGStatDefaultCollection monoBehaviour I thought if RPGDefaultStat inherits RPGStatDefaultCollection. I will know. Thanks
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
More elegant way to constantly update player health algorithms. 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can't call derived class function from list of base class. Ideas? 1 Answer