# Monogame Drawing Text

I'm really new at XNA. I've been following some tutorials at gameprogrammingadventures.org but when it comes to conversations I don't really like how the author does it so I've been trying to understand how to create a rectangle over the game and writing text inside it. This is done very poorly but I just wanted to understand how to do the basics first and perfect it later. This is what I have so far:

public void CheckConversation(GameTime gameTime)
{
if(Xin.CheckKeyReleased(Keys.Space) || Xin.CheckKeyReleased(Keys.Enter))
{
foreach (string s in map.Characters.Keys)
{
ICharacter c = CharacterManager.Instance.GetCharacter(s);
float distance = Vector2.Distance(player.Sprite.Center, c.Sprite.Center);

if (Math.Abs(distance) < 72f)
{
DialogueBox dialogue = new DialogueBox(gameRef, "Text here");
dialogue.Draw();
}
}
}
}


The "game" itself runs on a GamePlayState class. And this is some code for the DialogueBox class:

public DialogueBox(Game1 game, string text)
{
TextBox = new Rectangle(1, 1, 300, 200);
Text = text;
gameRef = game;
}
public void Draw()
{
SpriteBatch spriteBatch =gameRef.SpriteBatch;
spriteBatch.Begin(
SpriteSortMode.Deferred,
BlendState.AlphaBlend,
SamplerState.PointClamp,
null, null, null,
null);
spriteBatch.Draw(background, TextBox, Color.White);
spriteBatch.DrawString(font, text, new Vector2(1, 1), Color.White);
spriteBatch.End();
}


But I can't get it to draw anything on screen. I know for sure the dialogue.Draw call is executed (game crashes if I load a non-existing texture) and I'm going crazy. Like I said, this is probably some stupidly easy question but I can't really figure it out. Thanks for any help!

• You're saying that your game runs in a different class than the main class, have you made sure that there's a reference to the Draw() method as well? Usually I just use the spritebatch from the Game1 class as a parameter when defining the Draw() method as well. – Steven Mar 13 '18 at 13:46
• Isn't that what I'm doing with spriteBatch = gameRef.Spritebatch? In these tutorials I've been following, the Game1 Draw function is just base.Draw(gameTime). – Mike Mar 14 '18 at 21:51
• Well, it wouldn't have been my way, but as long as it's clear enough for you, then you do you. – Steven Mar 15 '18 at 7:25

I do not know if this makes any difference but the second argument of the DrawString method is supposed to be a type string, not a type text. But I do not know if they make any difference.

• My bad, I didn't even knew Text was a type. It's really this: private string text; public string Text { get { return text; } set { text = value; } } – Mike Mar 14 '18 at 21:55

Are you positive that the dialogue.Draw() method is really being called?

Like you said here

I know for sure the dialogue.Draw call is executed (game crashes if I load a non-existing texture)

You claim that it crashes if you load a non-existent texture but that isn't proof at all that Draw is getting called. That is just proof that your constructor is getting called because you load your textures in the constructor and that type of thing would fail at load, not when they are first run. A more proper way of testing if you are running the draw function is to use a Console.Write() or something like that.

I would also check if the problem is that you might never be closing this distance that you check for before calling Draw().

 if (Math.Abs(distance) < 72f)
{
DialogueBox dialogue = new DialogueBox(gameRef, "Text here");
dialogue.Draw();
}


I would test this out by simply removing the if statement here and seeing if it runs now.