I am looking to make a game where the player can access an in game terminal and then use this terminal to write AI logic in a programming language such as C++ or C#, then they can compile the code and apply the AI to NPC characters.

Of course if the code failed to compile error free then it would not be applied to the NPC. Mostly the code would be function calls with a few basic constructs available like int, float, bool etc.

My question is how could I achieve this in say Unity or another game engine. Would I need to use a scripting language such as LUA or Python to create the AI or could I be done in real time on a separate thread in Unity somehow?

  • \$\begingroup\$ You can do it like similary like they do it in Unity. Use codedom to generate .net assembly from C# code. Then just run it, and inside code use yield keyword for actions that should take some time (but should not stop game) \$\endgroup\$
    – Kikaimaru
    Commented Aug 20, 2013 at 9:07
  • 1
    \$\begingroup\$ Are you sure you want to make the AI completely programmable? In theory it would be kind of cool but it would require lots of inside knowledge on how the game and Unity works (like property names, packages, etc.) Also in theory people could write an 'AI' which is just a script that deletes system32 files or something. You might want to consider making a customizable (set predefined behaviour when condition is met like, low hp = avoid conflict and search for health pack) AI rather than a user programmed AI. \$\endgroup\$ Commented Aug 20, 2013 at 18:35
  • \$\begingroup\$ Also bear in mind if you made AI's shareable you could just be creating a vehicle for spreading computer viruses. \$\endgroup\$ Commented Aug 20, 2013 at 18:36
  • \$\begingroup\$ @BenjaminDangerJohnson yes well i was thinking of using something to possibly block the use of any libraries I don't want them using (since coding will be done in game). The scripts would be scanned before they were run to check and see if they are using anything that could be used badly, something similar to a whitelist system. \$\endgroup\$ Commented Aug 21, 2013 at 1:12
  • \$\begingroup\$ @Kikaimaru that is an interesting idea if I am understanding you correctly. Are you saying that I could create the ability for a player to create C# scripts then compile these single scripts into assembly and run them as normal? Would I call such code in the Update cycle to determine movement and behaviours? \$\endgroup\$ Commented Aug 21, 2013 at 1:19

3 Answers 3


That shouldn't be too much of a problem with an interpreted language like LUA or Python. I know about a LUA binding that's ready and available in the Unity Asset Store.

It seems there are other bindings available as well. Just search the Asset Store in the "Scripting/Integration" category. Here's one for python that I found.

  • \$\begingroup\$ From what I can see so far the Python binding will be more appropriate for me, so if I did go for that option would I be able to call .py code in my update cycle to for example move an NPC around and look for enemies? \$\endgroup\$ Commented Aug 20, 2013 at 6:49
  • \$\begingroup\$ @MatthewPigram I haven't used this plugin, but I don't see why this shouldn't work. You just feed the code into the interpreter and it will be executed. You might have to expose some methods in python that allow meaningful NPC programming (eg. some way to query the world and get enemies and their positions etc.) \$\endgroup\$
    – bummzack
    Commented Aug 20, 2013 at 6:55
  • \$\begingroup\$ JavaScript might be a good choice as well. \$\endgroup\$
    – danijar
    Commented Aug 20, 2013 at 15:03
  • \$\begingroup\$ @danijar I had not considered that lol, it would probably be really awesome too since unity directly uses it as a coding language it supports. \$\endgroup\$ Commented Aug 21, 2013 at 1:20
  • \$\begingroup\$ @MatthewPigram I used Google's V8 engine to embed Javascript into my game for scripting. I was pretty easy. But since Unity already supports that, you might be able to use the built in engine. By the way, the same might apply for C#. I would use the built in scripting interpreters if possible. \$\endgroup\$
    – danijar
    Commented Aug 21, 2013 at 8:13

You could embed a simple language such as forth or a minimal lisp, and run it's execution step by step like any other in-game activity. Cute idea, but somehow I can't imagine that your hacker/hero, frantically debugging while the monsters loom, would be very interesting game play.

It does remind me of some puzzle games, where you have to position agents in certain ways to automate the actions to a desired effect.

  • \$\begingroup\$ That's what would make it so fun having an intense hack while the enemy looms ever closer! Multiplayer would be great pitting coding skills against one another to see whose AI can outperform, of course it would be a big challenge having an online mode. \$\endgroup\$ Commented Aug 20, 2013 at 6:04
  • \$\begingroup\$ would an interpretive based language such as python work? that would be really awesome \$\endgroup\$ Commented Aug 20, 2013 at 6:09
  • \$\begingroup\$ the key thing is that the embedded language has to be inside (and fully under control of) the game. You would't want your in-game program to delete real files, for example. \$\endgroup\$
    – ddyer
    Commented Aug 20, 2013 at 17:20
  • \$\begingroup\$ true, I would probably block all libraries excepts the ones that would be useful for the game (which really would be very basic libraries, vectors and matrix calculations would be as complicated as things would get, the rest would just be container classes). \$\endgroup\$ Commented Aug 21, 2013 at 1:22
  • \$\begingroup\$ trying to make a language safe by subtracting everything that's dangerous is a losing game. For in-game use you need something specialized with particular knowledge of game objects and nothing else. \$\endgroup\$
    – ddyer
    Commented Aug 21, 2013 at 5:24

You can use CodeDom to compile C# code into assembly, and then run this assembly inside your code.

If this code would run in same thread as your game you have to make sure that the code that they write will not block your game code. You can use this by using yield, that will generate state machine on behind.

So you could have something like this:

public interface IAction {
  boolean IsFinished();
  void Update(GameTime gameTime);

public class Wait : IAciton {
  private TimeSpan waitingTime;
  public Wait(TimeSpan waitingTime) { this.waitingTime = waitingTime; } 
  boolean IsFinished() { return this.waitingTime <= 0; }
  void Update(GameTime gameTime) { this.waitingTime - gameTime.ElaspedGameTime; }

and then users would have to write code like this:

public MyMonster : Brain {
  public IEnumerable<IAction> Think() {
     while (IsAlive) {
        yield Wait(TimeSpan.FromSeconds(10));
        yield Suicide();

Then you would process it like this:

public AISystem {
   public void Update() {
      foreach (Brain brain in Brains) {
          var enumerator = brain.Think().GetEnumerator();
          if (enumerator.Current.IsFinished()) {

Now even if there is infinite loop, the code will not block your code. And users can write logic "sequentially".

As you can see this is essentially what unity does in its Behaviors

(I wrote this from head so it has probably some errors in it)

Or you can write your own CIL interpreter and interpret few instructions each frame :)

  • \$\begingroup\$ this is a good answer but a little bit confusing, I kind of understand what is happening, and I at least understand the concept, ill research this as I think this could be the method I have been looking for \$\endgroup\$ Commented Aug 21, 2013 at 23:37
  • \$\begingroup\$ @MatthewPigram Take a look at this: web.archive.org/web/20120406144758/http://blog.nickgravelyn.com/… \$\endgroup\$
    – Kikaimaru
    Commented Aug 22, 2013 at 8:23

Not the answer you're looking for? Browse other questions tagged .