Torque X & Dynamic C# Script Compiling/Instancing
by Johnathon · in Torque X 2D · 05/09/2009 (1:27 am) · 10 replies
I had at one point wrote a nice little script engine in C# 2005 that worked with XNA allowing me to write C# scripts and compile them on the fly while the game was running similar to how Torque is compiled and executed. The engine could find a supplied script name and return it to me as a instanced Type allowing me to use a single method (object o = obj.InvokeMethod("MethodName") to invoke what ever public method I wanted. I also had helper methods in place to build a list of available methods that existed, along with editing and viewing property information of the compiled script during runtime.
The setup worked well, but I dropped the XNA project and just recently stripped the XNA portion of the code out and adjusted the script system to run soley on Windows based .NET applications.
Then I started thinking about it and wondered if I re-implemented (which I am planning on doin as a plug-in to the current sytem) XNA support for the Script system called DotScript.
My question for you guys is, if it works just fine using standard XNA and C# 2008, in theory do you think it would work within Torque X as well?
Providing 3rd party users with a scripting envirnment to write small plug-ins and add-ons to the game would be nice, without requiring them to download Visual Studio Express, and it gives me better control over what framework components they have access to, by limiting what libraries are referanced by the script engine.
The Script engine also allows for optionaly compiling scripts without defining a class name for it similar to how just batch files work, and the script engine wraps a class and namespace around the supplied code to generate a C# script for compiling. Simplifying things for the 3rd party user who is not used to OOP and dealing with namespaces/using statements/classes ect.
Any thoughts?
You can find the current build of the Script Engine at http://dotscript.codeplex.com/
Not to promote my work, but rather get an opinion if you think it's something that would work and run with Torque X.
Thanks for any opinions and technical input on it.
The setup worked well, but I dropped the XNA project and just recently stripped the XNA portion of the code out and adjusted the script system to run soley on Windows based .NET applications.
Then I started thinking about it and wondered if I re-implemented (which I am planning on doin as a plug-in to the current sytem) XNA support for the Script system called DotScript.
My question for you guys is, if it works just fine using standard XNA and C# 2008, in theory do you think it would work within Torque X as well?
Providing 3rd party users with a scripting envirnment to write small plug-ins and add-ons to the game would be nice, without requiring them to download Visual Studio Express, and it gives me better control over what framework components they have access to, by limiting what libraries are referanced by the script engine.
The Script engine also allows for optionaly compiling scripts without defining a class name for it similar to how just batch files work, and the script engine wraps a class and namespace around the supplied code to generate a C# script for compiling. Simplifying things for the 3rd party user who is not used to OOP and dealing with namespaces/using statements/classes ect.
Any thoughts?
You can find the current build of the Script Engine at http://dotscript.codeplex.com/
Not to promote my work, but rather get an opinion if you think it's something that would work and run with Torque X.
Thanks for any opinions and technical input on it.
#2
I will be implementing a XNA based version of DotScript that will allow for dyamically loading the script assemblies, instancing Types and invoking methods during runtime. I have actually already done this in a seperate project that DotScript was based off of, and so re-implementing it wont take much time. It has never been tested on a 360 but I researched MSDN and found that the methods I use to dynamically instance objects on the compact framework was a supported method.
However compiling the scripts on the compact framework is not a supported feature due to framework limitations, and thus the reason for pre-compiling them on the PC into an assembly prior to placing on the 360 will be required.
I'm hoping this is something I can implement into Torque X sometime down the road.
As far as how DotScript compares to CS-Script, I have never heard of CS-Script so I'm not sure. After doing a quick peak at it, it looks like they work pretty much the same. DotScript however supports Visual Basic.NET scripts along with C# and a future implementation will allow for Python and Ruby script support once I can get IronPython and IronRuby worked into it. All of which will be fully supported on the 360 thanks to all of the compilers generating the same CIL code.
Lastly, you are not forced to write traditional C# or VB code either, DotScript supports a Hybrid compiler that allows you to take the following code.
and write
The Hybrid compiler will insert the supplied code into a C# or VB class and then compile it. Which makes 3rd party scripting a little less of a headache. You can control the namespace that all of the scripts will reside within that way as well.
Anyway, enough discussion on DotScript from me lol, where you able to use CS-Script for your Torque X project on a PC? Was it simple to implement? Once I can get a XNA implementation built I will download the Torque X builder demo and see if I can't get it worked into the engine.
05/09/2009 (3:11 pm)
Yes, at the moment precompiling of the scripts is implemented. You can tell DotScript to compile the scripts at runtime and generate a pre-compiled assembly for use later. At the moment DotScript Referances mscorlib.dll, System.dll, DotScript.dll and System.Windows.Forms.dll. I am planning on removing the referance to System.Windows.Forms.dll in the next release, and once that is done the compiled script assembly will be fully xbox360 compatible.I will be implementing a XNA based version of DotScript that will allow for dyamically loading the script assemblies, instancing Types and invoking methods during runtime. I have actually already done this in a seperate project that DotScript was based off of, and so re-implementing it wont take much time. It has never been tested on a 360 but I researched MSDN and found that the methods I use to dynamically instance objects on the compact framework was a supported method.
However compiling the scripts on the compact framework is not a supported feature due to framework limitations, and thus the reason for pre-compiling them on the PC into an assembly prior to placing on the 360 will be required.
I'm hoping this is something I can implement into Torque X sometime down the road.
As far as how DotScript compares to CS-Script, I have never heard of CS-Script so I'm not sure. After doing a quick peak at it, it looks like they work pretty much the same. DotScript however supports Visual Basic.NET scripts along with C# and a future implementation will allow for Python and Ruby script support once I can get IronPython and IronRuby worked into it. All of which will be fully supported on the 360 thanks to all of the compilers generating the same CIL code.
Lastly, you are not forced to write traditional C# or VB code either, DotScript supports a Hybrid compiler that allows you to take the following code.
using System;
using System.Windows.Forms;
class Script
{
public void HelloWorld()
{
MessageBox.Show( "Hello World!");
}
}and write
public void HelloWorld()
{
MessageBox.Show( "Hello World!");
}The Hybrid compiler will insert the supplied code into a C# or VB class and then compile it. Which makes 3rd party scripting a little less of a headache. You can control the namespace that all of the scripts will reside within that way as well.
Anyway, enough discussion on DotScript from me lol, where you able to use CS-Script for your Torque X project on a PC? Was it simple to implement? Once I can get a XNA implementation built I will download the Torque X builder demo and see if I can't get it worked into the engine.
#3
DotScript is an actualy library that you add to your .NET project and use from within your project with no extrenal executables being called or used.
An example is with
This Tutorial
05/09/2009 (3:16 pm)
Hmm, after doing some more looking at the tutorials page of CS-Script it looks like it's all command prompt based? Similar to Batch scripting?DotScript is an actualy library that you add to your .NET project and use from within your project with no extrenal executables being called or used.
An example is with
This Tutorial
#4
Here is a quick script I had made to test CS-Script:
http://torque.pastebin.com/m73f4127f
Here is the Torque component that called it:
http://torque.pastebin.com/m116ed6b6
It worked very well. The only problem, of course, is that the CS-Script library couldn't run on the XBox360 .NET framework. I will definitely try out DotScript. It would save me the trouble of developing a simple scripting language for my project.
05/09/2009 (4:22 pm)
CS-Script can be used to run scripts via the command line, yes. They also allow you to reference CSScriptLibrary.dll and use it in a host environment.Here is a quick script I had made to test CS-Script:
http://torque.pastebin.com/m73f4127f
Here is the Torque component that called it:
http://torque.pastebin.com/m116ed6b6
It worked very well. The only problem, of course, is that the CS-Script library couldn't run on the XBox360 .NET framework. I will definitely try out DotScript. It would save me the trouble of developing a simple scripting language for my project.
#5
Designing a script setup for Windows Applications is not quiet as tricky as a Game script system, as you need to update ever instance during each game loop, or sort through them and determine which scripts need updating during the loop.
05/09/2009 (4:29 pm)
Nice, I will post on the home page of DotScript when the XNA version of it is ready and available for download. I still need to look at how I am going to go about managing the scripts for XNA. How did you do it with CS-Script? Did all of your scripts inherit from an XNA component? Do you think they need access to the Game class? or simply run has stand-alone scripts that XNA will manage from within the game.Designing a script setup for Windows Applications is not quiet as tricky as a Game script system, as you need to update ever instance during each game loop, or sort through them and determine which scripts need updating during the loop.
#6
I haven't looked further into CS-Script since I was kind of hindered by the compact framework problem.
05/09/2009 (6:56 pm)
I didn't do anything really fancy. CS-Script defaults to allowing the script access to all loaded assemblies of the host application. So I just wrote my scripts like they came from my program and had CS-Script call the static RunScript() method of the script it was running every game loop.I haven't looked further into CS-Script since I was kind of hindered by the compact framework problem.
#7
DotScript is still an infant and has a lot of work to do still, but hopefully with some good feedback from everyone using it, I'll be able to build a solid script system.
05/09/2009 (7:18 pm)
Nice, that's something that I will look into implementing, an optional ability for the scripts to access it's host applications assemblies.DotScript is still an infant and has a lot of work to do still, but hopefully with some good feedback from everyone using it, I'll be able to build a solid script system.
#8
I'm not sure how this is implemented, but from the sounds of it (i.e, object o = obj.InvokeMethod("MethodName")) it seems very reflection heavy and thus could be a bit of a performance killer, something I'm very cautious about in a graphics intensive application.
Do you have any metrics on how performance was effected on a general application?
05/10/2009 (6:26 am)
This sounds cool, but I'm not sure if it would be very practical in a graphics application.I'm not sure how this is implemented, but from the sounds of it (i.e, object o = obj.InvokeMethod("MethodName")) it seems very reflection heavy and thus could be a bit of a performance killer, something I'm very cautious about in a graphics intensive application.
Do you have any metrics on how performance was effected on a general application?
#9
I had several discussions on the Creators Club website for XNA regarding reflection in XNA and the performance cost, and majority felt that as long as the script updates where ran on a seperate thread, the performance impact would not be noticable. If you're making the next Unreal or Quake quality game, then maybe it wouldn't be the best route, but for simpler games such as Marble Blast of even a normal FPS it wouldnt be very noticable. Of course a lot of that comes down to actually having people use it in a graphics application so that I can work on the optimizations for it, and get feed back on how to improve/change things with it, as I currently can't sit and work on a Torque X project.
Sometimes, performance is ok to be hit, for simplifying things. A game like Oblivion with thousands of NPC's and quests and items ect, would be easy to manage with scripts rather than hard-coding it all and re-compiling the game when you want to make a balancing change to gear, or adjust a quest ect.
05/10/2009 (10:41 am)
I personally have not used it extensively in a Windows Application yet, and yes it is pretty reflection heavy. I have made heavy use of reflection in prior applications of mine and so I know that it will not be a problem.I had several discussions on the Creators Club website for XNA regarding reflection in XNA and the performance cost, and majority felt that as long as the script updates where ran on a seperate thread, the performance impact would not be noticable. If you're making the next Unreal or Quake quality game, then maybe it wouldn't be the best route, but for simpler games such as Marble Blast of even a normal FPS it wouldnt be very noticable. Of course a lot of that comes down to actually having people use it in a graphics application so that I can work on the optimizations for it, and get feed back on how to improve/change things with it, as I currently can't sit and work on a Torque X project.
Sometimes, performance is ok to be hit, for simplifying things. A game like Oblivion with thousands of NPC's and quests and items ect, would be easy to manage with scripts rather than hard-coding it all and re-compiling the game when you want to make a balancing change to gear, or adjust a quest ect.
#10
i highly, highly, highly doubt performance will be an issue. If it worked with dynamically loaded TorqueScript, i can't see C# being slower, even if reflection is used
05/25/2009 (11:42 am)
i've downloaded and look forward to using it. i teach an AI course and one assignment is to write AIs for a tournament game i wrote in TGB. Students drop their text files in a directory, select their AI inside the game from a drop down and play. i haven't ported the game to TX2D in part because i hadn't figured out how to do the same in TX2Di highly, highly, highly doubt performance will be an issue. If it worked with dynamically loaded TorqueScript, i can't see C# being slower, even if reflection is used
Torque Owner William McDonald
Suckerfree Games
I can't use CS-Script on the XBox360 since it relies on functionality that the 360's .NET framework doesn't have. I did notice you have a section on the site you linked to that talks about pre-compiling scripts to be run on the 360. I am interested in that because I couldn't find a similar feature with CS-Script. Is that feature currently implemented?