Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What approach should I use for C# scripting

I'm writing a particle system. (for a game and a particle editor) In this system there are a 0 to n modifiers. Those modify the particles in the particle system every frame.

For example you could have a predefined modifier called "GravityModifier" that only does the following to every particle every frame: "particle.Velocity.Y += 9.81" or something like that.

Now I want the user to be able to write additional modifiers at runtime (in the editor). I would like to be able to just use C# for this. And since the particle systems are written to JSON files, the custom modifier scripts should be written as sourcecode (maybe base64 encoded) to the file.

--

My question is: Assume the game wants to load such a particle system file. How do I compile the custom modifiers into executable code?

Another very important thing to consider when answering this question: Note that this will be a particle system for a game. The compiled code will be called more than 3000 times every frame. (mostly at 60fps) So it's very important that the compiled code doesn't take much longer to execute than other game-functions. It would be nice to have the modifier scripts compiled into delegates of the form: delegate void ModifyParticle(ref Particle p);

Is this possible? If so, how?

like image 215
Riki Avatar asked Nov 18 '11 21:11

Riki


People also ask

Which software should I use for C?

Eclipse. It is one of the most popular, powerful and useful IDEs used by developers for C/C++ programming. It is open-source software which is simple and easy to use.

Which approach is adapted by C?

So C always uses top down approach, whereas C++ also uses bottom up approach. Because we use main function in c++ at anywhere but the compiler starts to compile the program in main function so it is called as bottom up approach.

Which compiler should I use to learn C?

Though there are many compilers available for C, GCC stands out to be one of the best as of now. The winner declaration here lies based on durability, optimization, speed, and code/error/syntax checks. Through this, we can clearly understand that the Compiler is an important pillar to the programming languages.


2 Answers

There is a CTP of the 'Roslyn' C# compiler as a service API, where you can execute C# code defined at runtime, such as a REPL etc. Here is an MSDN Article covering it
http://visualstudiomagazine.com/articles/2011/11/16/the-roslyn-scripting-api.aspx

Mono has had this functionality for a while in their Mono.CSharp library so that could be used as an alternative.

like image 160
TJB Avatar answered Oct 04 '22 19:10

TJB


Yes,

This is possible and quite simple using the CSScript library.

like image 22
dmck Avatar answered Oct 04 '22 19:10

dmck