What approach should be used for C # scripts

I am writing a particle system. (for the game and particle editor) There are modifiers from 0 to n in this system. They modify the particles in the particle system in each frame.

For example, you might have a predefined modifier called "GravityModifier" that only makes each next particle in each frame: "particle.Velocity.Y + = 9.81" or something like that.

Now I want the user to be able to write additional modifiers at run time (in the editor). I would like to be able to use C # for this. And since particle systems are written to JSON files, custom modifier scripts must be written as source code (possibly base64 encoded) to the file.

-

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

Another important thing to consider when answering this question: Please note that this will be a particle system for the game. Compiled code will be called more than 3,000 times for each frame. (mainly at 60 frames per second) Therefore, it is very important that the compiled code takes much longer than other game functions. It would be nice if the modifier scripts were compiled into form delegates: delegate void ModifyParticle (ref Particle p);

Is it possible? If so, how?

+6
source share
5 answers

Yes,

This is possible and fairly simple using the CSScript library.

+5
source

The Roslyn checkout is an upcoming (but still very early beta) preview of the C # compiler function as a service in the next .NET Framework.

+2
source

What about Lua instead? See here . Lua is a fantastically simple scripting language and is ideal for in-game action scripts. Quite a few iPhone games use Lua in objective-c, and there are many others that use Lua via .Net, too.

0
source

Use the built-in compiler from Microsoft in .NET CSharpCodeProvider :

How to programmatically compile code using the C # compiler:

The .NET Framework provides classes that allow programmatically access to the C # language compiler. This can be useful if you want to write your own code compilation utilities. This article provides sample code that allows you to compile code from a text source. The application allows you to either simply create an executable file or build an executable file and run it. Any errors that occur during the compilation process are displayed on the form.

http://support.microsoft.com/kb/304655

Just compile and download executable files when you start the game.

Additional examples of how to compile the dll and load / execute it directly can be found at http://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider.aspx and its community has added content.

0
source

Source: https://habr.com/ru/post/901847/


All Articles