Hi, I’m new to the forums, I’ve messed around with unity for quite some time but haven’t done anything serious yet.
I’m trying to build up my knowledge with C# and would like some verification on if my understanding is on track or not.
I’m checking if my code is correct ( I’m writing it from scratch to test my understanding)
this code SHOULD add 75 points to the int “score” when the “r” key is pressed and display the new updated score on the console.
int score = 0;
{
if(Input.GetKeyDown(KeyCode.R))
{
score=Add75points(score);
Debug.Log(score);
}
int Add75points (int scorebeforeadd)
{
int ret;
ret = scorebeforeadd+75;
return ret;
}
Did you try it? Does it work the way you want it to?
One thing that sticks out is your add points method seems backwards. Typically you’d have a single AddPoints method that takes the number of points to add and returns the new score - not distinct methods for every possible number that take the current score.
I’m at work, so I can’t test the code until I get home later today.
I’m not sure I understand how to accomplish having a single method to add a dynamic number of points.
Would the best way to do this be to change the code so I have two variables feeding into the function, one for the current point value, and one for the points to be added? something like:
int score = 0;
{
if(Input.GetKeyDown (Keycode.R))
}
score=Addpoints (score, addxpoints);
Debug.Log(score);
}
int Addpoints (int scorebeforeadd, pointstoadd)
{
int ret;
ret = scorebeforeadd+pointstoadd;
return ret;
}
and if so, what would be the best way of declaring the value of “addxpoints” for different actions?
Typically you’d have some class that is responsible for managing score - so passing the current score in becomes unnecessary.
public class ScoreKeeper
{
public int Score { get; private set; }
public void AddPoints(int points)
{
Score += points;
}
}
ScoreKeeper scoreKeeper;
void Awake()
{
scoreKeeper = new ScoreKeeper();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
scoreKeeper.AddPoints(75);
Debug.Log("Current score is: " + scoreKeeper.Score);
}
}
That is just beautiful. I am still learning, so I’ll definitely have to play around with it, but it is very helpful to see how someone with more experience would accomplish this. Thank you.
Here’s a score-keeping pattern I use all the time:
// in ScoreTable.cs:
public static class ScoreTable
{
public static int DestroyEnemy = 100;
public static int ReachGoal = 200;
public static int ConquerFlag = 1000;
}
Here is where the game’s global score would be:
// in GameState.cs:
public static class GameState
{
public static int Score;
public static void StartGame()
{
Score = 0;
}
}
Then when I wanted to actually award these scores, I would write:
void Update()
{
if (Input.GetKeyDown( KeyCode.D))
{
GameState.Score += ScoreTable.DestroyEnemy;
}
if (Input.GetKeyDown( KeyCode.R))
{
GameState.Score += ScoreTable.ReachGoal;
}
if (Input.GetKeyDown( KeyCode.C))
{
GameState.Score += ScoreTable.ConquerFlag;
}
}
Now when you want to change the score table, the numbers are all in one file. Later you could read those numbers from a disk file, or calculate them based on the difficulty setting, or retrieve them from a website and implement “Double Scoring Weekend” for your game simply by changing a file you are hosting somewhere.
Anyone who wants access to the score can get it with GameState.Score.
When you start a new game, call GameState.StartGame();
1 Like
I like that - except I would make all the score values const 