Need help understanding this lines of code

Hello, i’m new to monogame and i only have some basic knowledge about classes, i don’t understand these following lines of code :

_graphics = new GraphicsDeviceManager(this);, why do i need to create a new instance of “GraphicsDeviceManager” and what is the purpose of the “this” keyword?

_spriteBatch.DrawString(font, "Score : " + score, new Vector2(10, 10), Color.Black);, why do i have to create a new instance of the Vector2 class ?.

Thanks in advance !.

1 Like

Thanks.

this is the object that the code is inside of so in this case its the Game object (ie. the code is inside of the Game class).

DrawString code you’ve shown is a shortcut for:

Vector2 myVector = new Vector2(10, 10);
_spriteBatch.DrawString(font, "Score : " + score, myVector, Color.Black);

DrawString requires you to pass in a Vector2 object which will have had new called somewhere in the program.

1 Like

Hello - another thing to consider esp. if your spriteBatch.DrawString() call is being invoked in the main game Update() method is that you don’t want to evaluate "Score : " + score every frame as this would create a lot of garbage. Remember strings are immutable in C# and concatenating two strings every frame will create a third string that will need to be garbage collected.

Instead, store that text in a variable; as your score does not change every frame only update the score value and the text when the score changes. That way you will generate a lot less garbage.

Reference:

2 Likes

Thanks, so technically the Vector2 parameter in spritebatch.draw() needs to be instantiated so i can only do “new Vector2” in the arguments ?(correct me i’m wrong), or like you said in your example.

Thanks, so i have to do something like ’ String finalText = $“Score : {score}”; ’ , then instead of passing score in the arguments for the spritebatch.draw() i pass finalText? (correct me if i’m wrong).

Correct - esp. if the location of where score is drawn on screen does not change [wouldn’t have thought so!] then do Vector2 myVector = new Vector2(10, 10); once only e.g. in from Iniitalize() and use in _spriteBatch.DrawString() each frame

1 Like

Correct - if you check the link for “Remote Performance Monitor” above I had code sample that you could use something like:

public void Update()
{
if (playerKilledSomething)
{
scoreValue += 100;
scoreText = scoreValue.ToString();
}
}
public void Draw()
{
_spriteBatch.DrawString(font, scoreText, myVector, Color.Black);
}

1 Like

BTW: this is a simple example - as games get more complex you may like to channel all score activity through a ScoreManager class e.g. like one that can be found in 3D City

1 Like

yeah i’m having dificulties with classes and interfaces :frowning:

Thanks.

Okay, thanks.

Believe me, you don’t need interfaces at this stage.

1 Like

Why ?

Like Martenfur says, you don’t need interfaces at this stage. My code base uses them only because I wanted to integrate unit tests and you typically use interfaces to mock external dependencies as per rule “Depend on abstractions not concrete implementations”. Again, for your example this is overkill but just wanted to show how, for larger growing code bases, you would extract this code into its own class to help keep you code base clean

1 Like

So i need to be good at classes for now and worry about interfaces later ?.