Maybe what I want to do is just the wrong way of going about it. But i am trying to write a game debug printer thing. It is to write values to the screen in a sprite font for various things i tell the debugger to care about. My debugger class is simple , but it seems that in c# things are getting passed by value not reference. So I turn to the ref keyword, which seems to work okay if I pass in the right data type. The issue is that I have this method signature:
Dictionary<String, object> debugStrings = new Dictionary<String, object>();
...
...
public void addVariable(String index, ref object obj) //i think it needs to be a reference, not so sure though.
{
debugStrings.Add(index, obj);
}
This adds to the dictionary a variable to ultimately print to the screen along with a key for it to be known as.
The problem though arises when I try to use the above method:
debugPrinter.addVariable("myrotationvalue", ref this.Rotation);
as per the link in the comment I changed the code above to:
this.Rotation = 4;
object c = (object)this.Rotation;
this.Rotation = 20;
level.dbgd.addVariable("playerrot", ref c);
//always prints 4 out, i guess it still is not passing by reference?
So it doesn't give an error but always prints out 4. Not sure how I will ever get the reference to work.
Again on another edit took the references out:
this.Rotation = 20;
level.dbgd.addVariable("playerrot", this.Rotation);
this.Rotation = 4; //should draw to screen 4, doesn't draws 20
I apparently this is harder than I thought it would be a simple little fun class to work up before I hit the hay, ha ha ha... this is not boding well for the monday to come.
Full class:
namespace GameGridTest.GameGridClasses.helpers
{
public class DebugDrawer : DrawableGameComponent
{
Dictionary<String, object> debugStrings = new Dictionary<String, object>();
int currentX;
int currentY;
VictoryGame _game;
private SpriteBatch spriteBatch;
private SpriteFont spriteFont;
public DebugDrawer(VictoryGame game) : base(game)
{
_game = game;
currentX = _game.Window.ClientBounds.Width - 400; //draw the debug stuff on right side of screen
currentY = 5;
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
spriteFont = _game.Content.Load<SpriteFont>("fonts\\helpers\\fpsFont");
}
public void addVariable<T>(String index, AbstractDebugHandler<T> obj) //i think it needs to be a reference, not so sure though.
{
debugStrings.Add(index, obj);
}
public void removeVariable(String index)
{
debugStrings.Remove(index);
}
protected override void UnloadContent()
{
_game.Content.Unload();
}
public override void Update(GameTime gameTime)
{
}
public override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
foreach (object obj in debugStrings) //draw all the values
{
spriteBatch.DrawString(spriteFont, obj.ToString(), new Vector2(currentX, currentY), Color.White);
currentY += 30;
}
currentY = 5;
spriteBatch.End();
}
}
public abstract class AbstractDebugHandler<T>
{
public AbstractDebugHandler(T obj)
{
InnerObject = obj;
}
public T InnerObject { get; private set; }
public abstract string GetDebugString();
}
public class ThisDebugHandler: AbstractDebugHandler<object>{
public ThisDebugHandler(object innerObject) : base(innerObject){
}
public override GetDebugString(){
return InnerObject.Rotation; //??
}
}
}