Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I want to implement an interesting concept into my game in which clients can program certain parts of their AI. I will provide them an unchangeable namespace and class formatting and they will code their AI within the class (I.e. OnEnemyInRange(Unit enemy) {}). I want to save this code in a string and run it as C# code in-game.

My problem is that I don't even know where to start. I've heard about reflection but only seen it done for method names. How would I go about running string as code? - conditions, methods, variables, and operations.

share|improve this question

closed as off-topic by gnat, Doc Brown, MainMa, gbjbaanb, GlenH7 Jan 15 at 14:26

  • This question does not appear to be about software development within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

    
The technical term for this is eval. Google that and you will find answers. –  Kilian Foth Jan 14 at 7:34
1  
recommended reading: Where to start –  gnat Jan 14 at 8:24
    
Your start at Google, enter "C# running string as code", and find plenty of answers. –  Doc Brown Jan 14 at 8:43
3  
This question appears to be off-topic because such questions belong on stackoverflow, but it would be closed immediately there as a duplicate. –  Doc Brown Jan 14 at 8:44
    
Oh, well thanks for helping and showing me the specific duplicate :p. Obviously I asked because I couldn't find it. Anyways, thanks to David Perfors for giving me some actually helpful input. @Doc: I was using Yahoo search on my friend's computer lol. Okay, I learned my lesson. –  JPtheK9 Jan 14 at 18:25

1 Answer 1

up vote 2 down vote accepted

The build in way for .NET is using the CodeDomProvider, it has all the functionality you need, but as far as I know it is not easy to use.

A better option would be a C# scripting engines like csscript or some other library that gives easy access to the C# compiler and makes it possible to run code. But this also gives you a lot of work.

Probably the easiest and fastest way is to create a plugin interface where you just load an assembly and directly plug it into your game.

share|improve this answer
    
Thanks! I'll make a plugin interface. It'll also help by restricting the users so they can't hack any of the other scripts hehe. –  JPtheK9 Jan 14 at 18:26

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