- Home /
Callling a variable and setting values only once, not every frame.
Hello. I feel this is a bit of a beginner question, but I couldn't wrap my head around it. I've tried using getter and setter for this, but I didn't manage to get it to work. I'm still new to a lot of coding aspects, so please bear with me on this.
What I am trying to do, is to give a character a set amount of stat points when that character reaches a new level. Then, naturally, what I want to happen is that the character only gets the stat points once, not every frame it is true that the character is that level.
My current code:
public class CharacterLevelScript : MonoBehaviour {
public int currentLevel;
public int currentExp;
public int statPoints;
public GameObject[] enemies;
void Start () {
currentLevel = 1;
}
void Update () {
List<GameObject> enemyList = new List<GameObject> ();
enemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemy in enemies) {
enemyList.Add(enemy);
}
foreach(GameObject enemy in enemyList) {
EnemyHealth eh = (EnemyHealth)enemy.GetComponent("EnemyHealth");
if(enemy.gameObject) {
if(eh.curhp <= 0f) {
currentExp += eh.xpGiven;
}
}
}
if(currentExp >= 0 && currentExp <=100) {
currentLevel = 1;
}
else if(currentExp >= 101 && currentExp <= 500) {
currentLevel = 2;
}
else if(currentExp >= 501 && currentExp <= 1000) {
currentLevel = 3;
}
What I've tried working with, but failed to get it to work:
public int currentLevel;
public int CurrentLevel {
get {
return currentLevel;
}
set {
statPoints += 5;
currentLevel = value;
}
}
I have a current code solution working, but I will not post this as an answer, as I feel it might be done even neater than this. Feel free to critique.
private bool level1 = false;
private bool level2 = false;
private bool level3 = false;
//Etc...
void Update() {
if(currentLevel == 2) {
if(!level2) statPoints += 5; level2 = true;
}else{level2 = false;}
if(currentLevel == 3) {
if(!level3) statPoints += 5; level3 = true;
}else{level3 = false;}
//Etc...
}
Answer by richyrich · Nov 18, 2014 at 01:35 AM
How about this...?
if (currentExp >= 0 && currentExp <= 100 && currentLevel != 1)
{
currentLevel = 1;
statPoints += 5;
}
else if (currentExp >= 101 && currentExp <= 500 && currentLevel != 2)
{
currentLevel = 2;
statPoints += 5;
}
else if (currentExp >= 501 && currentExp <= 1000 && currentLevel != 3)
{
currentLevel = 3;
statPoints += 5;
}
I likes this one better than the one I did in the comments to my question. Thanks!
Cheers mate. You could reduce code further though:
(currentExp >= 0 && currentExp <= 100 && currentLevel != 1)
change to
(currentExp > -1 && currentExp < 101 && currentLevel != 1)
Another issue with the code is that it runs every update. Ins$$anonymous$$d, when the player points increase, work out there and then how many points are needed for the next level. Then as the experience increases, you reduce the difference. When the difference is zero, you then make a call to the above code which should be in another function e.g. void PromoteCharacter().
Depending on the project complexity, number of levels etc. we could complicate things further if you like ;)
int[]pointsPerLevel = new int[10]{5,5,5,10,10,10,15,15,15,15};//assumes 10 levels
int[]playerAdvances = new int[10]{0,100,500,1000,2000,3000,4000,5000,6000,7000};
int currentLevel = 0;
int difference = 0;
int currentExp = 0;
//Do code that affects experience
int increase = whatever;
difference -=increase;
currentExp +=increase;
//Check
if (difference<=0 && currentLevel<9)
{
Promote();
}
Then in your promotion code, assu$$anonymous$$g a zero based level (if levels start at 0 not 1) to work out the points for the next level it would be as follows:
void Promote()
{
int nextLevel = currentLevel+1;
difference+ = playerJumps[nextLevel] - playerJumps[currentLevel];
statPoints += pointsPerLevel[nextLevel];
currentLevel = nextLevel;
}
Code above not tested, but you get the drift.
Answer by Kiwasi · Nov 18, 2014 at 03:19 AM
I know you have acepted an answer. But that's a really ugly way to do it.
Consider this code.
private int _currentLevel;
public int currentLevel {
get {
return _currentLevel;
}
set {
if (value > _currentLevel){
statPoints += 5 * (value - _currentLevel);
}
_currentLevel = value;
}
}
A few notes
Wasn't sure if level can go down and you can loose stats. Ignored this possibility.
Added checking that the new level is actually higher then the existing level before adding stats
Allowed the level to increase multiple levels in one step
Changed the access level of the backing variable to private. You never want a public backing variable
If statPoints is not changed anywhere else and is totally dependent on level you could define it via a formula instead of using increments
Hope this helps. Properties are really powerful and valuable. I'd hate to see you leave them behind because you couldn't get them to work.
Just reread your question and realised you want to do this based on experience ins$$anonymous$$d of level. The same principle applies. But you could do better with a separate class to handle experience and level.
I appreciate this answer. I will stick to the code which I gave the answer to. Because I felt it was simple enough to play with the variables. I want to be able to not have a static 5+ for the variable at every level. So, even though the code might be a bit repetitive and an overwork, it will do just fine for now. But, I really appreciate that you took the time to figure out a (possibly) better way! You're always helpful around here. :)
Answer by Addyarb · Nov 18, 2014 at 01:45 AM
Doesn't get much more simple than this, you don't have to define what happens for each level. If you want to double the needed exp to level up, just do so each time the code runs.
public int currentLevel;
public int currentExp;
public int statPoints;
public int statPointsToAdd;
public int expToLevelUp;
void Start () {
currentLevel = 1;
}
void Update(){
if(currentExp>=expToLevelUp){
currentExp=0;
statPoints+=statPointsToAdd;
currentLevel+=1;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613171807im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Need to use 2 different language scripts. 1 Answer
Basic C# Public Variable Help 3 Answers