Click here to Skip to main content
15,947,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I want to make a XNA debugger for my personal use, just so I can track all variables on the screen (draw text on the screen). Now my problem is not related to XNA but to C# and how it works on this.

Ignore the draw text part (from the debugger). It should draw the text (actuall variable name=variable value).

I have a class debugger, and a method UPDATE. When I call the update method, I want to see on my screen the list of variables. Example:
var1=123
var2="hello world"
var3="another string"

I will only have strings and int. Ok, here is the debugger class:

public class Debugger
    {
        //sprite batch, this is from XNA, used for drawing
        private SpriteBatch batch;
        SpriteFont Font1;
        
        //this is my list of vars I want to draw on the screen, for debug purpose. I will add elements to this list from the method AddObj (I have 2 methods, one that receives int, one string)

        List<debugobjlist> list = new List<debugobjlist>();
        public Debugger(Player.PlayerScreen control) 
        {
            batch = new SpriteBatch(control.GraphicsDevice);
            Font1 = control.content.Load<spritefont>("Fonts/Normal");
        }

        public void AddObj(string var, ref string val)
        {
            DebugObjList li = new DebugObjList();
            li.val = new DebugRef(ref val);
            li.var = var;
            list.Add(li);
        }

        public void AddObj(string var, ref int val)
        {
            DebugObjList li = new DebugObjList();
            li.val = new DebugRef(ref val);
            li.var = var;
            list.Add(li);
        }

        public void DrawFPS()
        {
            //draw the image
            batch.Begin();
            //batch.DrawString(Font1, "game=" + control.stats.Game.phase.ToString(), new Vector2(10, 100), Color.Red);
            for (int i = 0; i < list.Count; i++)
            {
                batch.DrawString(Font1, list[i].var+"="+list[i].GetString(), new Vector2(10, 120+i*20), Color.Red);
            }
            batch.End();
        }
    }

 public class DebugObjList //this is the ellements from the list. Because I want to have both string and int, I used the object DebugRef to do that
    {
        public string var;
        public DebugRef val;
        public string GetString()
        {
            return val.GetString();
        }
    }

    public class DebugRef //This is the object that returns the value of the int or of the string
    {
        int val;
        string val2="";
        public DebugRef(ref int val)
        {
            this.val = val;
        }
        public DebugRef(ref string val)
        {
            this.val2 = val;
        }
        public string GetString()
        {
            if (val2 != "")
                return val2;
            return val.ToString();
        }
    }
</spritefont></debugobjlist></debugobjlist>


Now for example I want this. I have an object player, that has a variable age=10. I want to track this variable as the game goes on, so I add it to the debugger (the debugger object is created and it's static, just to ease things here)

public class Player
{
int age=10;
public Player()
{
Debugger.Add(ref age);
}

public void IfIPressEnter() //this is just a sample. When I press enter I want to change the player's age
{
this.age=20;
}
}


Now the problem is that the Debugger draws the same number as initialized, it draws 10 always. Even if I press enter and change the value of age to 20, the debugger still draws 10.

I am sure I am doing something wrong, with the "ref" and everything, but I don't know what.

Please help :D

Thanks!
Posted
Updated 5-Oct-10 9:58am
v2
Comments
Henry Minute 5-Oct-10 20:04pm    
Your code is a little confusing, in the lower snippet you use Debugger.Add(ref age) yet your Debugger class does not have an Add() method. It would be better if you pasted 'real' code rather than typing in an artificial example, which is what it looks like.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900