Named parameters in when drawing sprites

Hi everyone,
I’m new to coding with Monogame, and in C# in general. I watched a fairly old tutorial on Youtube on getting started and they used named parameters when using the spritebatch.Draw() method. I never tried it specifically with the Draw method, but rather with the spritebatch.DrawString() method as I assumed it would be the same, but I get this error:

No overload for method ‘Drawstring’ takes 5 arguements

here is my code:

_spriteBatch.DrawString(
    spriteFont: font,
    text: endText,
    position: new Vector2(_graphics.PreferredBackBufferWidth / 2, _graphics.PreferredBackBufferHeight / 2),
    color: Color.Black,
    origin: new Vector2(font.MeasureString(endText).X / 2, font.MeasureString(endText).Y / 2)
    );

do named parameters no longer work, or am I doing something wrong on my end?
Thanks in advance!

No such combination of parameters exists (by default).

https://docs.monogame.net/api/Microsoft.Xna.Framework.Graphics.SpriteBatch.html

The closest is:
public void DrawString(SpriteFont spriteFont, string text, Vector2 position, Color color)

Leave out the origin.

I understand, however I need to specify the origin for the text to be centered without any of the other parameters that would normally be in-between. The specific tutorial for where I was first shown named parameters was here:

at 5:36

This tutorial is pretty old by now, so there is a chance an update has made it no longer work. Is it still possible to do this?

Named parameters allows you to “skip” parameters that have a default value. DrawString methods don’t have any default values.

You have to provide values for all the parameters of the chosen method overload.

Ie. if you want to use an origin, you will have to provide everything there (one of the overloads that has an origin):

public void DrawString(SpriteFont spriteFont, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth, bool rtl)

Example:

SpriteBatch.DrawString(font, “hello”, position, Color.Black, 0f, origin, 1f, SpriteEffects.None, 1f, false);

2 Likes

This gave me some ideas for my soon to be announced solution for beginners, so, my advice for now is, don’t get too hung up on one tutorial, fish around for a solution, it is the best way forward in coding.

EDIT

Oh…

Hi @Looke351, Welcome to the Community!

Happy Coding!

1 Like