StackOverflowException:请求的操作导致堆栈溢出
StackOverflowException: The requested operation caused a stack overflow
我用 C# 和界面在 Unity2d 中编写了一些代码。
LevelingSystem.cs 被放入空的游戏对象中。
我收到错误:
WhosebugException: The requested operation caused a stack overflow.
PlayerCap.get_actExp () (at Assets/Scripts/player/PlayerCap.cs:17)
PlayerCap.get<message truncated>
CapLevel.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
interface ICapLevel
{
float multiplierA { get; set; }
float multiplierB { get; set; }
float multiplierC { get; set; }
float multiplierD { get; set; }
int lvlCap { get; set; }
List<double> expCap { get; set; }
float actExp { get; set; }
void GenerateExpPerLvl();
float RequiredExp(int level);
}
PlayerCap.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCap : ICapLevel
{
public float multiplierA { get { return multiplierA; } set { multiplierA = 280f; } }
public float multiplierB { get { return multiplierB; } set { multiplierB = 100f; } }
public float multiplierC { get { return multiplierC; } set { multiplierA = 1.15f; } }
public float multiplierD { get { return multiplierD; } set { multiplierD = 2.3f; } }
public int lvlCap { get { return lvlCap; } set { lvlCap = 210; } }
public List<double> expCap { get { return expCap; } set { expCap = new List<double>(); } }
public float actExp { get { return actExp; } set { actExp = 0f; } }
public void GenerateExpPerLvl()
{
Debug.Log("implementation successful");
for (int expLevel = 1; expLevel <= lvlCap; expLevel++)
{
expCap.Add(RequiredExp(expLevel - 1));
}
}
public float RequiredExp(int level)
{
double formulaRounded;
var formula = multiplierB * (Mathf.Pow(multiplierC, level - 1)) + multiplierA * (Mathf.Pow(level, multiplierD));
if (formula < 1000)
{
if ((formula % 10) == 0)
{
formulaRounded = formula;
return (float)formulaRounded;
}
else
{
formulaRounded = Math.Round((formula / 1000), 1, MidpointRounding.AwayFromZero);
}
}
else
{
formulaRounded = Math.Round((formula / 1000), 0, MidpointRounding.AwayFromZero);
}
return (float)formulaRounded * 1000;
}
}
LevelingSystem.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelingSystem : MonoBehaviour
{
//interface implementation
private PlayerCap _pCap;
private ICapLevel _ICap;
private List<double> _pExpCap;
private int pLvl;
private float _pActExp;
public void Awake()
{
_pCap = new PlayerCap();
_ICap = _pCap;
}
private void Start()
{
_pActExp = _ICap.actExp;
_ICap.GenerateExpPerLvl();
_pExpCap = _ICap.expCap;
}
public void AddExp(float amount)
{
_pActExp += amount;
StartCoroutine(CapLevelCheck(amount));
}
private IEnumerator CapLevelCheck(float amount)
{
while (true)
{
if (_pActExp >= _pExpCap[pLvl])
{
_pActExp -= (float)_pExpCap[pLvl];
AddLevel(1);
} else
{
Debug.Log("You Get " + amount + " Experience and you have " + pLvl + " Lvl. *Click noice!");
break; //otherwise loop will do in infinite
}
yield return 0;
}
}
public void AddLevel(int amount)
{
pLvl += amount;
}
public int GetActualLevel()
{
return pLvl;
}
}
我尝试了一些事情,比如在方法 GenerateExpPerLvl() 中摆脱掉 for,错误消失了,所以我怀疑的是列表变量,但也许我错了。
感谢您的帮助:)
问题出在这里:
public float actExp { get { return actExp; } set { actExp = 0f; } }
// ^ Problem here & ^ same problem here
您在自己的 getter 和 setter 中调用 属性。它会创建一个无限循环...(每次调用 actExp
它都会调用自己)
相反,您应该只写(如果访问器内部没有逻辑):
public float actExp { get; set; }
我用 C# 和界面在 Unity2d 中编写了一些代码。 LevelingSystem.cs 被放入空的游戏对象中。
我收到错误:
WhosebugException: The requested operation caused a stack overflow.
PlayerCap.get_actExp () (at Assets/Scripts/player/PlayerCap.cs:17)
PlayerCap.get<message truncated>
CapLevel.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
interface ICapLevel
{
float multiplierA { get; set; }
float multiplierB { get; set; }
float multiplierC { get; set; }
float multiplierD { get; set; }
int lvlCap { get; set; }
List<double> expCap { get; set; }
float actExp { get; set; }
void GenerateExpPerLvl();
float RequiredExp(int level);
}
PlayerCap.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCap : ICapLevel
{
public float multiplierA { get { return multiplierA; } set { multiplierA = 280f; } }
public float multiplierB { get { return multiplierB; } set { multiplierB = 100f; } }
public float multiplierC { get { return multiplierC; } set { multiplierA = 1.15f; } }
public float multiplierD { get { return multiplierD; } set { multiplierD = 2.3f; } }
public int lvlCap { get { return lvlCap; } set { lvlCap = 210; } }
public List<double> expCap { get { return expCap; } set { expCap = new List<double>(); } }
public float actExp { get { return actExp; } set { actExp = 0f; } }
public void GenerateExpPerLvl()
{
Debug.Log("implementation successful");
for (int expLevel = 1; expLevel <= lvlCap; expLevel++)
{
expCap.Add(RequiredExp(expLevel - 1));
}
}
public float RequiredExp(int level)
{
double formulaRounded;
var formula = multiplierB * (Mathf.Pow(multiplierC, level - 1)) + multiplierA * (Mathf.Pow(level, multiplierD));
if (formula < 1000)
{
if ((formula % 10) == 0)
{
formulaRounded = formula;
return (float)formulaRounded;
}
else
{
formulaRounded = Math.Round((formula / 1000), 1, MidpointRounding.AwayFromZero);
}
}
else
{
formulaRounded = Math.Round((formula / 1000), 0, MidpointRounding.AwayFromZero);
}
return (float)formulaRounded * 1000;
}
}
LevelingSystem.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelingSystem : MonoBehaviour
{
//interface implementation
private PlayerCap _pCap;
private ICapLevel _ICap;
private List<double> _pExpCap;
private int pLvl;
private float _pActExp;
public void Awake()
{
_pCap = new PlayerCap();
_ICap = _pCap;
}
private void Start()
{
_pActExp = _ICap.actExp;
_ICap.GenerateExpPerLvl();
_pExpCap = _ICap.expCap;
}
public void AddExp(float amount)
{
_pActExp += amount;
StartCoroutine(CapLevelCheck(amount));
}
private IEnumerator CapLevelCheck(float amount)
{
while (true)
{
if (_pActExp >= _pExpCap[pLvl])
{
_pActExp -= (float)_pExpCap[pLvl];
AddLevel(1);
} else
{
Debug.Log("You Get " + amount + " Experience and you have " + pLvl + " Lvl. *Click noice!");
break; //otherwise loop will do in infinite
}
yield return 0;
}
}
public void AddLevel(int amount)
{
pLvl += amount;
}
public int GetActualLevel()
{
return pLvl;
}
}
我尝试了一些事情,比如在方法 GenerateExpPerLvl() 中摆脱掉 for,错误消失了,所以我怀疑的是列表变量,但也许我错了。
感谢您的帮助:)
问题出在这里:
public float actExp { get { return actExp; } set { actExp = 0f; } }
// ^ Problem here & ^ same problem here
您在自己的 getter 和 setter 中调用 属性。它会创建一个无限循环...(每次调用 actExp
它都会调用自己)
相反,您应该只写(如果访问器内部没有逻辑):
public float actExp { get; set; }