I added a “console” in my game, in which I can type commands, and receive a response (it’s based on this). I need to be able to access objects that I instantiate in my Game.cs in my console class. I can’t pass them to the constructor, since I don’t know how much there will be once the “engine” is done.
I tried using a method to add the objects to a Dictionnary<string, object> but I can’t access the properties.
What I’d like to be able to do:
Game1.cs
TileMap tileMap = new TileMap();
BakuConsole console;
...
console = new BakuConsole(this, Content.Load<SpriteFont>("ConsoleFont"));
console.AddRef("tileMap", tileMap);
BakuConsole.cs
public void AddRef(/* some args */) {
// halp!
}
public void Execute(string input) {
switch (input)
{
case "some --command":
console.WriteLine(g.ToString());
// execute stuff, with an object that was added via AddRef()
console.WriteLine("");
console.Prompt(prompt, Execute);
break;
default:
console.WriteLine("> " + input + " is not a valid command.");
console.WriteLine("");
console.Prompt(prompt, Execute);
break;
}
}
I hope I am clear enough. Thanks!
Edit:
I just don’t want my constructor to grow too big in case I add more types:
TileMap tileMap = new TileMap();
OtherType1 ot1 = new OtherType1();
OtherType2 ot2 = new OtherType2();
OtherType3 ot3 = new OtherType3();
OtherType4 ot4 = new OtherType4();
OtherType5 ot5 = new OtherType5();
IronPython does exactly what I want to do, and does it via Globals.Add(“string”, object”). However, I can’t seem to find it in the source (of IronPython).
From what you described, you don’t actually need a dictionary, you need several properties on some object, probably directly on
BakuConsole;Then, you can set it like this:
And then when you use it, you won’t have any problems with accessing properties.