Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Recently I'm working on a 2d game engine example in .Net with C#. My main problem is that I can't figure out how I should include the game logic within the game. Currently I have a base engine which is a set of classes that they are running sub-systems like Render, Sound, Input and Core functionality. There is an editor which helps the user to add resources, build levels, write scripts and other stuffs.

I came up with an idea to use Reflection and CSharpCodeProvider from my editor to compile the written code. This way I can get an executable of my product too. This way is quite well but I would like to know what's really the solution and architecture to do this.

My engine's role is 2d platform. The scripting language is C# right now because I can't consist any other embeddable language for now. The game needs compilation and CSharpCodeProvider is the only way for me to do it meantime.

share|improve this question

3 Answers 3

I came up with an idea to use Reflection and CSharpCodeProvider from my editor for compile the written codes, in this way I can get an executable of my product too. This way is quite well but I would like to know that, what's really the solution and architecture to do this.

This is a huge security risk, the user can use reflection or edit the code dom to rewrite any and all of your application. Additionally to being an easy attack vector for viruses, keyloggers, and the rest it would be impossible to enforce any anti cheating measures because of this.

Look into scripting. Iron Python and CSScript are widely used and have a good reputation. The more commonly used scripting language is Lua but Lua is a pian to integrate with a .net application. At least last time I tried.

share|improve this answer
Lua is a proper name, not an acronym. It's never LUA. –  Nicol Bolas Feb 24 '12 at 0:06
I know, it's really a big mistake so I asked the right way here. I worked with Iron Python but the major problem is, for each time you want to perform any written script, you should compile it first. I'm now using JavaScript wrapper for .Net but it's like these embeddable languages and it need to be compiled for each code that I give! I don't know really, I don't think this is the right way. –  MahanGM Feb 24 '12 at 18:48
LuaInterface makes it really easy, and it also has good performance. –  OverMachoGrande Apr 20 '12 at 22:47

I assume from your question that the entire game is C# (the engine is C# and the scripting language is C#?). Are you are using a 3rd party engine or rolling your own?

Can you separate your game into logical modules? For example, a core engine module for all the general bits (rendering, audio, UI primitives, etc.) and a game module with all the specific bits (game logic, defining the UI, what your buttons do, which textures to load, your level format) ?

At a higher level, consider whether you are really in a situation where you need a script engine, or would clever code separation + data meet your needs until & if you decide you need to go down that path? Yes, scripts can give you the ability to alter game behavior on the fly/without recompiling, but it does add an extra layer of complexity. If you are experimenting with tech that's one thing, but you may find that you just don't need it.

share|improve this answer
Thanks for feedback. It's been so long that I posted this question and in this while I read some articles about it. Totally there is a mistake in my way of implementation. I saw Unity3D that uses MonoDevelop to compile the user scripts into it's output file. I don't know how does it works but I know it's the true way and I noticed that CryEngine is using Lua as an external scripting language which is good. But the last problem is that I don't know how to compile these user scripts with my output file which is an exe file. –  MahanGM Mar 22 '12 at 17:02
BTW, I read a book which is Advanced 2D Game Programming that has chapter about embedding scripts into your engine. It uses Lua but all of code running is at the runtime. I think it's not quite suitable for a game to run it's scripts from outside and I think it would decrease the game total FPS. –  MahanGM Mar 22 '12 at 17:09

I solved a very similar problem a while back. I was in Javaland, but the same principles apply.

What we did was provide a scripting API via the methods of an API class. Then, user code was wrapped inside a child of the API class, that class instantiated, and the user-defined run() method called.

To mitigate security concerns, we used Java role-based security, with draconian settings. (Instances of the child class had access to their own methods only). This essentially means that you're restricting the users to the API you define, while remaining in a single language environment.

I believe that you can do the same in .NET by using Role-Based Security

PS: This is the same method that Processing uses.

share|improve this answer
Thanks for reply. I wrote my comment at @Chronic Game Programming's answer. I think it's my problem. Besides, I think it's better to move on C++. At least there are lots of libraries for it right now. –  MahanGM Mar 22 '12 at 17:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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