XNA 4.0 Scripting

posted in XXChester's Blog
Published April 09, 2011
Advertisement
[size="3"][font="Calibri"]

[size="3"][font="Calibri"][heading]XNA 4.0 Scripting[/heading][/font]


[subheading]Objectives of this journal:[/subheading][/font][font="Calibri"][size="3"]
[/font][font="Calibri"][size="3"][font="Calibri"][size="3"][font="Calibri"][size="3"][font="Calibri"][size="3"][font="Calibri"][size="3"][font="Calibri"][size="3"][font="Calibri"][size="3"][font="Calibri"][size="3"][/font][/font][/font][/font]
-Give you a starting point for command based scripting for your XNA game.
[/font][font="Calibri"][size="3"]-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.[/font][/font][/font][font="Calibri"][size="3"]

[/font][/font][size="3"][font="Calibri"][subheading]Notes:[/subheading]
[/font][font="Calibri"][size="3"][font="Calibri"][size="3"][/font]
-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.
[/font][font="Calibri"][size="3"]-[/font][font="Calibri"][size="3"]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.[/font]
[font="Calibri"][size="3"]-[/font][font="Calibri"][size="3"]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. [/font]
[font="Calibri"][size="3"]-[/font][font="Calibri"][size="3"]You can download a sample solution at the end of the article to see it in action.[/font]
[font="Calibri"][size="3"]-[/font][font="Calibri"][size="3"]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().[/font]
[font="Calibri"][size="3"]-[/font][font="Calibri"][size="3"]There is also a video included to show it in action[/font]
[font="Calibri"][size="3"]
[font="Calibri"][size="3"]
[/font]
[font="Calibri"][size="3"][/font][subheading]Back history:[/subheading][/font]
[font="Calibri"][size="3"][font="Calibri"][size="3"][/font]
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.
[/font]
[size="3"][font="Calibri"]

[subheading]Quick, what is command based scripting:[/subheading][/font]
[font="Calibri"][size="3"][font="Calibri"][size="3"][/font]
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.
[/font]
[size="3"][font="Calibri"]
[subheading]Technique:[/subheading]
[/font][font="Calibri"][size="3"]-[/font][font="Calibri"][size="3"]Have a system of receiving input running either externally or internally, in its own thread.[/font]
[font="Calibri"][size="3"]-[/font][font="Calibri"][size="3"]Take the commands input to said system and send them to a parser.[/font]
[font="Calibri"][size="3"]-[/font][font="Calibri"][size="3"]The parser determines if we are running a high level command such as getValue or help or a command against a registered object.[/font]
[font="Calibri"][size="3"]-[/font][font="Calibri"][size="3"]If the command is high level, it simply executes[/font]
[font="Calibri"][size="3"]-[/font][font="Calibri"][size="3"]If the command is against a registered object, we first have to find our registered object via the reference name specified on creation.[/font]
[font="Calibri"][size="3"]-[/font][font="Calibri"][size="3"]Once we find the object, we pass the registered object the input and it is parsed again to determine if it is a method or property.[/font][size="3"]
[font="Calibri"][size="3"][font="Arial"]-[/font]Then it is further parsed to determine if parameters were specified such as: test.Position = Vector2(10,10)[/font][font="Calibri"][size="3"]-[/font][font="Calibri"][size="3"]Once a command and its parameters (if any) are found, the command is executed.[/font]
[font="Calibri"][size="3"]-[/font][font="Calibri"][size="3"]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.[/font]
[font="Calibri"][size="3"]-[/font][font="Calibri"][size="3"]Now that you have your scripting system in place, just wrap everything about scripting in like the below example to ensure it only runs on WINDOWS and in DEBUG mode;[/font][font="Calibri"][size="3"]
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));

#if WINDOWS

#if DEBUG

// register objects

ScriptManager.getInstance().registerObject(this.text, "text");

Thread consoleInputThread =

new Thread(new ThreadStart(listenForInput));

consoleInputThread.Start();

this.running = true;

#endif

#endif

}



#if WINDOWS

#if DEBUG

private void listenForInput() {

string input;

do {

Console.Write(">>");

input = Console.ReadLine();

ScriptManager.getInstance().handleInput(input);

} while (running);

}

#endif

#endif



[size="3"][font="Calibri"][subheading]Programmers speak:[/subheading][/font]
[font="Calibri"][size="3"][font="Calibri"][size="3"][/font]
The way I achieved all of the above technique is with some basic string manipulation and via the magic of Reflection (System.Reflection).
[/font]
[font="Calibri"][size="3"][subheading]Video:[/subheading][/font]
[color="#800080"]

[/color]
[media]
[/media]
[font="Calibri"][size="3"]

[subheading]Example program download (Written in Visual Studio Express 2010 with Game Studio 4):[/subheading][/font]
Due to permission problems I am unable to attach a rar/zip file to the blog entry so unfortunately I have to use RapdiShare\MediaFire to host the solution for now until I can get in touch with admins to get this resolved.
Example


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



**EDIT** Fixed the link and changed to mediafire because not only did my link become broken, rapdishare actually deleted my file as well. I am still working with admins to figure out why I cannot just attach the source here to avoid the third party software. Also please note that I deleted the old .rar I had saved off my desktop so this one is a bit different because it has some enhancements (such as automatic closing of the window on destruction) and the start of some rework(should all be commented out). This example should however still work. I also embedded the YouTube video.

Thanks again.

[/font]
0 likes 7 comments

Comments

Gaiiden
who'd you contact for the upload issue? If no one yet I can take a look at it before we bump it up to Mike
April 13, 2011 06:33 PM
XXChester
I reported it via feedback...I wasn't sure how else to get ahold of an admin. The report ID is listed below;

[b]Report ID #1273:[/b]
Thank you for taking the time to read my blog and looking into this issue.
April 13, 2011 06:58 PM
Dementia
The rapidshare link isn't working. It's showing up in my browser as the version with the ellipsis (...) in it.
April 14, 2011 12:31 PM
XXChester
Thank you for the information Dementia, I will re-upload it tonight when I get home from work (as I don't have the files here). Hopefully Gaiiden can figure out my permission's issue soon so I don't have to rely on RapidShare.
April 14, 2011 01:26 PM
XXChester
Link should be fixed, sorry again about that. Please read the **EDIT** blurb as there are some small changes.
April 14, 2011 08:37 PM
XXChester
This past weekend I have implemented a generic object translator which is working like a charm and a much better approach to object translation. I will write a new article (and reference this one) soon to show how to do generic object translations.
April 18, 2011 02:54 PM
XXChester
Here is the much overdue journal entry about my [url="http://www.gamedev.net/blog/908/entry-2250559-generic-object-translator/"]Generic Object Translator[/url] hope you enjoy it.

August 30, 2011 03:43 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement