Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

closed as too broad by Josh Petrie Nov 11 '14 at 17:20

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

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) – Kikaimaru Aug 20 '13 at 9:07
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. – Benjamin Danger Johnson Aug 20 '13 at 18:35
Also bear in mind if you made AI's shareable you could just be creating a vehicle for spreading computer viruses. – Benjamin Danger Johnson Aug 20 '13 at 18:36
@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. – Matthew Pigram Aug 21 '13 at 1:12
@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? – Matthew Pigram Aug 21 '13 at 1:19
up vote 6 down vote accepted

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.

share|improve this answer
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? – Matthew Pigram Aug 20 '13 at 6:49
@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.) – bummzack Aug 20 '13 at 6:55
ok great, thanks for the answer! – Matthew Pigram Aug 20 '13 at 7:00
JavaScript might be a good choice as well. – danijar Aug 20 '13 at 15:03
@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. – Matthew Pigram Aug 21 '13 at 1:20

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.

share|improve this answer
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. – Matthew Pigram Aug 20 '13 at 6:04
would an interpretive based language such as python work? that would be really awesome – Matthew Pigram Aug 20 '13 at 6:09
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. – ddyer Aug 20 '13 at 17:20
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). – Matthew Pigram Aug 21 '13 at 1:22
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. – ddyer Aug 21 '13 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 :)

share|improve this answer
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 – Matthew Pigram Aug 21 '13 at 23:37
@MatthewPigram Take a look at this:… – Kikaimaru Aug 22 '13 at 8:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.