-1

I would like to create a simple game engine for my own game, and I want to make it supports script system.

I'll make my own script language be compiled to C# code, and built as a DLL Module, loaded by main application. however, there's several problems here.

  1. How can I link the compiled DLL at runtime?

    I can link modules what will be used with "Reference". but I don't know how to link DLLs at runtime programmatically.

  2. How can make loaded-module to refer main application's function, or variables, etc...

    Loaded module(compiled script) must call main application's member(well, I don't know whether it's right expression or not...) for it's purposes.

How can I resolve these problems? I hope this problem will be resolved with pure C#(or .NET Framework), not external libraries.

Thank you!

Kangjun Heo
  • 1,023
  • 1
  • 8
  • 19
  • Is it necessary to link it at runtime? If you just reference it from your application it would make your application coding a lot easier. – Slepz Mar 10 '17 at 10:09
  • This is a very broad question, far too broad for Stack Overflow. – DavidG Mar 10 '17 at 10:10
  • Slepz/because it's compiled game script. – Kangjun Heo Mar 10 '17 at 10:10
  • That's a very broad question. I recommend to close it. – zmechanic Mar 10 '17 at 10:27
  • 1
    @zmechanic So why did you answer it then? – DavidG Mar 10 '17 at 10:27
  • @DavidG I answered it to best it can probably be answered for this sort of question. But it was immediately down-voted. Which indicates that the person asking the questions looking for some specific answer, but he didn't ask it. All answers will be down-voted, which is very counterproductive. – zmechanic Mar 10 '17 at 10:32
  • @zmechanic oops, I was reading your answer. somebody downvote every answers include my question. I apologize if you upset. – Kangjun Heo Mar 10 '17 at 10:35
  • @zmechanic your answer made me to make path. it was really helpful. thank you. – Kangjun Heo Mar 10 '17 at 10:36

2 Answers2

0

How can I link the compiled DLL at runtime?

I can link modules what will be used with "Reference". but I don't know how to link DLLs at runtime programmatically.

You should use reflection to dynamically load the DLLs (as plugins) from a specified folder. Examples can be found here and here.

How can make loaded-module to refer main application's function, or variables, etc...

Loaded module(compiled script) must call main application's member(well, I don't know whether it's right expression or not...) for it's purposes.

You should code those DLLs against an Interface of your framework. And you don't ask the plugins to call your framework, instead your framework should call them.

Community
  • 1
  • 1
hyankov
  • 4,049
  • 1
  • 29
  • 46
0

Your questions are more architectural than code related.

First, it's a meaty task to write a transpiler (the code which translates one language to another). Compiling C# on the fly is easy, though. You can use Roslyn for this. Check out this article for an awesome tutorial: http://www.tugberkugurlu.com/archive/compiling-c-sharp-code-into-memory-and-executing-it-with-roslyn.

The other problem comes next. Once you've got a DLL you'll need to load it into your main AppDomain. It's extremely easy as well. Just use Assembly.Load(...) method. Here is some example: https://msdn.microsoft.com/en-us/library/25y1ya39(v=vs.110).aspx But you need to know once you've loaded you DLL into AppDomain, you can't unload it. If you don't use any tricks, you'll load your DLL into main AppDomain of your application. Then you'll not be able to get rid of it when you change code in your DLL. The only way is to close and start application again. Otherwise, you'll need to load your DLL into separate AppDomain.

Now, how to refer application functions. For this you'll need to create an interface. Code your application functions in a class which conforms to the interface, and then pass instance of this class into your other class you load and instantiate from your dynamically loaded DLL.

Loading DLL as I described above, will do nothing useful. You need to create instances of classes from your DLL. For this you can use Activator.CreateInstance(...). As parameters to the class you about to create pass a class instance that conforms to an interface you created as above. For your reference this software development pattern is called "strategy".

zmechanic
  • 1,842
  • 21
  • 27