🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Choosing a scripting language for a .NET game?

Started by
7 comments, last by nickwinters 20 years, 2 months ago
I''m trying to pick a scripting language for the game. The game is written in C# and MDX. The scripting language needs to be very easy to implement, and can call functions within the class that calls it. C like syntax is preferred, but we can live without it. Any recommendations? Perl isn''t an option, since all programmers on the team dislike it. Thanks. -Nick
Advertisement
Isn''t there a part of the .net framework that can dynamically compile C# code? Because that means you can use C# as your scripting language, which can only be an advantage.
Yes, C# serves as a very good scripting language. Look at the System namespace (.Framework? .Reflection?) There is a function called Compile(); just search MSDN.
C#, JScript, <any .NET-compatible language>.

[Edit: Link gone wrong.]

[edited by - Oluseyi on April 7, 2004 6:15:49 PM]
If you want a dynamically typed language, there''s always JScript.NET.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Jscript looks the most promising, but I can''t find a tutorial for running it like a scripting language. I want something simple like

JScript jscript=new JScript(filename of script);jscript.functionInScript(); 


Maybe it''s a problem with the way my game is designed, but currently, all units are stored in an XML file, and there''s a place in the XML file where I tell it which script to load. JScript will just contain the AI code.

With this current design, is Jscript feasable? Or is it possible to use JScript with a small change to my design?

Thanks.

-Nick
Take a look at Microsoft.JScript.JScriptCodeProvider. That should serve as a starting point. The System.Reflection classes are also useful.

You can also use the built-in scripting stuff in .NET. This is very poorly documented, unfortunately:

///<author>Arild Fines</author>///<date>27.04.2002</date>using System;using Microsoft.JScript.Vsa;using Microsoft.JScript;using Microsoft.Vsa;using System.Text;using System.Reflection;using System.Windows.Forms;namespace RogueChat.Gui{	/// <summary>	/// this is a singleton class representing the GUI scripting engine	/// </summary>	public class ScriptEngine 	{        /// <summary>        /// constructor made private to avoid external instantiation        /// </summary>		private ScriptEngine()		{            Initialize();					}        /// <summary>        /// sole access point for the script engine        /// </summary>        /// <returns>the only instance of the script engine</returns>        public static ScriptEngine Get()        {            if ( instance == null )                instance = new ScriptEngine();            return instance;        }        /// <summary>        /// adds a block of code to the engine, compiles it and restarts the script engine        /// </summary>        /// <param name="name">the name of the block to add</param>        /// <param name="block">the source block itself</param>        public void AddCodeBlock( string name, string block )        {            //stop it if its running             if ( engine.IsRunning )                engine.Reset();            //get the code item from the engine            IVsaCodeItem item = (IVsaCodeItem)engine.Items.CreateItem( name, VsaItemType.Code,                 VsaItemFlag.Module );             item.SourceText = block;            //recompile and restart the engine            engine.Compile();            engine.Run();        }        /// <summary>        /// creates an instance of a class type in the script engine        /// </summary>        /// <param name="className">the name of the class to instantiate</param>        /// <returns>an object of the requested class</returns>        public object CreateInstance( string className )        {            if ( !engine.IsCompiled )            {                engine.Compile();                engine.Run();            }            return engine.Assembly.CreateInstance( rootNamespace + "." + className );        }        /// <summary>        /// initializes the engine        /// </summary>        private void Initialize()        {            //standard init stuff            engine = new VsaEngine( true );            engine.RootMoniker = moniker;            engine.Site = new VsaSite( this );            engine.InitNew();            engine.RootNamespace = rootNamespace;            engine.Name = rootNamespace;            //set some options            engine.SetOption( "alwaysGenerateIL", true );            engine.SetOption( "autoRef", true );            engine.SetOption( "print", true );            //give the script engine access to the same assemblies as this            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();            foreach( Assembly asm in assemblies )                engine.Items.CreateItem( asm.Location, VsaItemType.Reference, VsaItemFlag.None );                                }        /// <summary>        /// class to represent the vsa site used        /// </summary>        private class VsaSite : IVsaSite        {             public VsaSite( ScriptEngine engine )            {                this.engine = engine;            }            #region Implementation of IVsaSite            public object GetEventSourceInstance(string itemName, string eventSourceName)            {                return null;            }            public object GetGlobalInstance(string name)            {                return null;            }            public void Notify(string notify, object info)            {                                        }            public bool OnCompilerError(Microsoft.Vsa.IVsaError error)            {                MessageBox.Show( String.Format( "Error on line {0}: {1}\n{2}", error.Line,                    error.Description, error.LineText ), "Yuo si teh suck" );                return true;            }            public void GetCompiledState(out byte[] pe, out byte[] debugInfo)            {                pe = null;                debugInfo = null;                        }                    #endregion            private ScriptEngine engine;        }        #region private data        private static ScriptEngine instance = null;        private IVsaEngine engine;        private const string moniker = "roguechat.gui://script";        private const string rootNamespace = "RogueChat.Gui.Scripts";        #endregion	}}



--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Wow, thanks for that code, it has helped me so much I can't even describe it in words. It's always the same, once you got the basic functions working, it is a lot easier to understand what is going on and add those things that you still miss.

Thanks!

EDIT: Hmm, btw, I have been wondering if network messages could be scripts as well.. Is this doable with this approach? I am just thinking that since each message would have to be compiled, this would probably be a little bit too slow.. is that correct? or is it a stupid idea anyway to use a scripting language in such cases?

[edited by - Wuntvor on April 15, 2004 5:50:01 PM]
anything you compile in .NET creates it`s own assembly, in-memory or on disk. And assemblies can not be unloaded from a running application.
That being said, having to compile every command it receieves would have an incredible overhead, and would cause the application to consume more and more memory
for commands, you`d have to write your own parser and use reflection to invoke code.

This topic is closed to new replies.

Advertisement