Game Development Community

GUIText gives me a null reference [SOLVED]

by L P · in Torque X 2D · 06/23/2009 (11:14 pm) · 8 replies

I've been working with the different GUI elements, and I hit this strange bug.

GUIText text = new GUIText();
text.Style = textStyle;
text.Name = "Paused";
text.Text = "Game Paused";
text.Position = new Vector2(500, 500);
text.Visible = true;
text.Folder = baseFrame;

When I give the string to text.Text, it gives me a NullReferenceException. When running through the breakpoints, nothing is null. Hopefully I could get some assistance with this.

#1
06/24/2009 (6:24 am)
I'll tell you if you tell me how you're getting your game to pause. lol

two things maybe.
one, I assume that textstyle is being defined?

textStyle.FontType = "arial20";
textStyle.TextColor[0] = Color.White;
textStyle.SizeToText = true;

second, I'm not sure if this is always needed, but for my game I added this function to my HUD.cs file...
public override void OnRender(Vector2 offset, RectangleF updateRect)
{
base.OnRender(offset, updateRect);
// if you had a boolean variable called Game.paused, you could do things like:
if(Game.paused){
text.Visible = true;
}else{
text.Visible = false;
}
// or simply:
text.Visible = Game.paused;
}
Don't know if that helps.
#2
06/24/2009 (6:38 am)
That fixed it! I'd forgotten to define the textstyle. Serves me right for copy-pasting the code I used for the background image. Thanks!

As for the pausing:

protected override void Update(GameTime gameTime)
        {
            InputHelper.Update();

            if (!Paused)
                base.Update(gameTime);
            else if (InputHelper.IsNewPress(Buttons.Start) || 
                InputHelper.IsNewPress(Keys.Enter))
                PauseMenu.StopPause();

        }

*note: InputHelper is a class available in one of the Creators Club tutorial. I made a few modifications to it, and if anybody wants it I can post it.
#3
06/24/2009 (7:23 am)
is that override inside your Game.cs file, or elswhere?
#4
06/24/2009 (7:32 am)
Yes, it's in Game.cs
#5
06/24/2009 (3:52 pm)
Would you mind posting the InputHelper class?
#6
06/24/2009 (4:13 pm)
No problem. I'll post it on a new thread so more people see it.
#7
06/24/2009 (4:52 pm)
Many thanks
#8
06/24/2009 (5:28 pm)
Hey guys,
Any chance you guys might write a basic tutorial on TDN since GUI related knowledge is lacking around here? Adding this to community links.

Brian