Skip to main content
GameDev.net gamedev.net
🔒 Locked

C# scripting

Started by Krum110487 Sep 22, 2010 at 9:22 AM 4 replies 2.5k views
Original Post
Krum110487
Krum110487
Hello, I would first like to point out, that I am new on the gaming scene, but I have been programming for quite some time.

I know that C# scripting is possible, as I have seen examples of it, although they are somewhat confusing to me and leave out key ideas.

Here is what I am in need for and why:

1) I need a scripting system for low level users to script game aspects.
2) The language needs to be completely managed so it is not too technical
3) I would like if the language was compatible with XNA and XBOX (meaning they must be compiled)

thus I have deduced that I need C# for these needs.

now, I have seen plenty of scripting systems that require you to include, create classes...etc which is a bit too hard for what I will require for my user-base to do.

I would love if it is possible to create a system to where the script can look along the lines of...

class Map_142{    public override void OnEnter()    {        Player.ShowMessage("You entered map" + this.Map.Number + "!");    }}


I imagine that Map_142 would have to extend another class to work correctly.

Mind you, I am just giving an example, I wouldn't mind it if there has to be a parameter passed or some inheritance as I can do this automatically, but as long as it is close to this format, that would be awesome.

I know what I want, but I don't have the first clue of how to make a scripting language in C# to work in this manner. I will in worst case scenario use a language like lua and sacrifice XBOX compatibility if need be.

I have been looking around, but I can't seem to find a clear cut example of what I am looking for. I am just sorta lost when it comes to C# and scripting.

any help is appreciated!
Thank you for your time!
jpetrie
jpetrie
There's nothing special about it; the basic implementation is very straightforward and is no different from writing any other C# code:

The "scripts" are just classes that extend some base "script" class (or implement some interface). You then load the assembly containing the script classes dynamically via something like Assembly.Load, find all the types that extend your base class / implement your IScript interface via GetTypes(), and then create instances via Activator.CreateInstance.

Note that a language being managed does not mean it is nontechnical, and C# probably isn't terribly well-suited to being used as a scripting mechanism for entirely nontechnical users. Remember, scripting just means that you're doing something to drive changes externally -- the authors of your C# scripts will still need to be reasonably competent C# programmers.

Also note that you won't be able to easily hot-reload scripts without putting them in another appdomain, since you cannot remove a loaded assembly from an appdomain, only tear the domain down.
Krum110487
Krum110487
Quote:
Original post by jpetrie
There's nothing special about it; the basic implementation is very straightforward and is no different from writing any other C# code:

The "scripts" are just classes that extend some base "script" class (or implement some interface). You then load the assembly containing the script classes dynamically via something like Assembly.Load, find all the types that extend your base class / implement your IScript interface via GetTypes(), and then create instances via Activator.CreateInstance.

Note that a language being managed does not mean it is nontechnical, and C# probably isn't terribly well-suited to being used as a scripting mechanism for entirely nontechnical users. Remember, scripting just means that you're doing something to drive changes externally -- the authors of your C# scripts will still need to be reasonably competent C# programmers.

Also note that you won't be able to easily hot-reload scripts without putting them in another appdomain, since you cannot remove a loaded assembly from an appdomain, only tear the domain down.


This clears a couple things up for me. I understand the concept mostly, it is just implementing it that for some reason is losing me.

Lets create a more tangible example so I can fully understand how I could implement it!


Lets say I have this hard-coded into my program.
public interface IMap{   string MapName   {      get;       set;    }   ...}public class Map : IMap{   private string MapNameVal;   string MapName   {      get      {          Return MapNameVal;      }      set      {          MapNameVal=MapName;      }}



Then when a map is loaded I will instantiate the Map_1 (not 100% sure on this one need to investigate Activator.CreateInstance) which will load the main() to run on load.

For other functions to be called I would hard code something like...
Sudo code:
if(player_leaves_map){    Map_1.LeaveMap()}



Then I can interact with it with the following "script".
public class Map_1 : Map{    public static void main()    {         MapName = "test";    }    public void LeaveMap()    {         MapName = "stupid face"    }}


is this the idea, of course I am simplifying it, but I am researching at the moment, and I want to be able to create my classes accordingly.

if you see a fundamental problem with what I am suggesting please yell at me and tell me why.

Thank you for your quick response.
iliak
iliak
You can have a look at my Script & ScriptInterface classes.

If you have questions, feel free to ask !
- Iliak -
[ ArcEngine: An open source .Net gaming framework ]
[ Dungeon Eye: An open source remake of Eye of the Beholder II ]
Krum110487
Krum110487
Quote:
Original post by dilyan_rusev
Hi!

I would recommend boo or iron python.

If it were strict c#, I would most probably used compiler services to dynamically build an assembly and load it in memory.

A search in google found a sample on building c# programs with API dynamically.

EDIT:
Of course, MSDN has even better example on code providers.

Dido


thank you for the suggestion, although my question to you is, does xbox support it, the only one I have found to work on xbox is C# itself (for obvious reasons) and only if they are built, because I know that dynamically built scripts are impossible on the xbox due to not having certain API (I think reflect?).

but if I am wrong please correct me.

Thank you.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.