Brandon McCulligh's Portfolio
XNA 4.0 Scripting
Posted Date: 4/9/2011
Featured At:
Original Post
Objective(s) of this journal:
- Give you a starting point for command based scripting for your XNA game.
- Receive feedback on the whole thing and especially alternatives to my ObjectTranslator, because it is extremely sloppy and I do not like it. It feels like a complete hack.

- I am in no way claiming this is the best way to do any of this, matter of fact I am positive the way my ObjectTranslator works is garbage, I encourage you to download the example solution and provide your feedback. I am simply stating it is possible and providing a rough example.
- This is a quick and dirty starting point written in a couple hours; its purpose is to give you a starting point that you can either take and write your own or build on to.
- This is a very BASIC implementation, meaning it is very limited such as it does not handle chained together method calls etc. I needed something with the ability to move elements around at runtime for design purposes and that is what this achieves.
- You can download a sample solution at the end of the article to see it in action.
- The sample you have to manually close the scripting window because I did not look into tying the rest of the tear down method to timeout the Console.ReadLine().
- There is also a video included to show it in action

Back history:
      A couple months ago I was just starting to fall into the rabbit hole in which they call scripting. Then I made the change to the XNA framework, and to my great disappointment after hours upon hours of searching, there was no scripting solution available for XNA. Even more so to my disappointment everyone was stating it was not possible, nor feasible sense you have to redeploy to the Xbox anytime you make a change anyway. This is true. Let me reiterate this thought, you CANNOT use this on your Xbox, however no one (that I could find) was talking about using scripting and more specifically command based scripting to make designing your game easier and faster. By this I mean the ability to access and alter objects on the fly so that you can quickly position them etc without having to stop, change, compile, and run all over again.

Quick, what is command based scripting:
      Command based scripting is essentially what it sounds like. You type in a command and it is parsed and executed. Basically it runs beside your main application (or inside it if you write it to be so) and it has access to methods and data within the program and is able to manipulates it.

- Have a system of receiving input running either externally or internally, in its own thread.
- Take the commands input to said system and send them to a parser.
- The parser determines if we are running a high level command such as getValue or help or a command against a registered object.
- If the command is high level, it simply executes
- If the command is against a registered object, we first have to find our registered object via the reference name specified on creation.
- Once we find the object, we pass the registered object the input and it parses it to determine if it is a method or property.
      - Then it is further parsed to determine if parameters were specified such as: test.Position = Vector2(10,10)
- Once a command and its parameters (if any) are found, the command is executed.
- Now you need to design error handling so that if you put in an invalid command it will not crash your game (defeats the purpose of on the fly changes) and it will provide useful feedback as to WHY the command was invalid.
- Now that you have your scripting system in place, just wrap everything about the script in like the below example to ensure it only runs on WINDOWS and in DEBUG mode;

            protected override void LoadContent() {
                  // Create a new SpriteBatch, which can be used to draw textures.
                  spriteBatch = new SpriteBatch(GraphicsDevice);
                  this.spriteFont = Content.Load("SpriteFont1");
                  this.text = new Test("Foo bar", new Vector2(100f,100f));
                  // register objects
                  ScriptManager.getInstance().registerObject(this.text, "text");
                  Thread consoleInputThread =
                        new Thread(new ThreadStart(listenForInput));
                  this.running = true;

            private void listenForInput() {
                  string input;
                  do {
                        input = Console.ReadLine();
                  } while (running);

Programmers speak:
      The way I achieved all of the above technique is with some basic string manipulation and via the magic of Reflection (System.Reflection).

Example program download (Written in Visual Studio Express 2010 with Game Studio 4):

      Thank you for reading my blog entry on XNA 4.0 Scripting and I hope you found it interesting, helpful and most importantly; understandable.
Thanks, XXChester


Powered by: Great White North Productions© Technologies