Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!






XNA 4.0 Scripting

Posted by XXChester, 09 April 2011 · 2,420 views

XNA Sripting XNA XNA Gaming

[heading]XNA 4.0 Scripting[/heading]

Objectives 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.

Notes:


-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.

Technique:


-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 is parsed again 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 scripting 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<SpriteFont>("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


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).

Video:



http://www.youtube.com/watch?v=IVPyJgt9Ro4


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


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.






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
I reported it via feedback...I wasn't sure how else to get ahold of an admin. The report ID is listed below;

Report ID #1273:
Thank you for taking the time to read my blog and looking into this issue.
The rapidshare link isn't working. It's showing up in my browser as the version with the ellipsis (...) in it.
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.
Link should be fixed, sorry again about that. Please read the **EDIT** blurb as there are some small changes.
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.
Here is the much overdue journal entry about my Generic Object Translator hope you enjoy it.

December 2014 »

S M T W T F S
 123456
78910111213
14151617181920
212223 24 252627
28293031   

Recent Comments

PARTNERS