0

I'm trying to make a game where the players could write their own script.

I don't want to use things such as CodeDOM for security reason, and I try to avoid 3rd-party components as much as possible.

So I managed to write my own parser to parse the script.

It's not pretty, but it get the job done.

But now comes the problem: Because the amount of user-made scripts is quite large, so it inevitably create the performance issue.

So I thought maybe I should turned the already-parsed-once script into some sort of method or delegate or something.

I had done some research on the subject of CreateDelegate and Expression, but it seems like not what I thought it was.

And to be honest, it's VERY complicated and advanced, and beyond my skills of understanding.

I mean, I could probably turn "each individual line" into method using this solution, but how about the "whole script"? And surely it's NOT a good way of doing things, is it!?

And I had also thought about making each individual line into a small class and "chain" all those classes together to make a smooth rundown of the script.

But, again, because the amount of scripts is not-so-little, it occupied most of my resources just for handling those classes.

So, could somebody PLEASE be so kind and help me out?

Much appreciated!

PiggyChu001
  • 440
  • 6
  • 20

1 Answers1

1

Ambitious stuff here. You are trying to build an interpreted programming language interpreted by c#.

You can try roslyn that enables runtime c# scripts running, or another scripting language from the asset store.

I did not try any, but there are assets to run scripts at runtime. This might be another alternative to run c# code at runtime, or you might be interested in this other alternative which provides a simplified runtime language interpreted by c#, which might be desirable for non-programmer users. Also a simplified language might provide a more accurate validation/interpretingCorrection features, to be able to write working code before runtime, which is an interesting feature. In case that feature is not availble, with a simplified language the approach to build a code validation system yourself would be much more feasable than if its raw c# :)

rustyBucketBay
  • 4,320
  • 3
  • 17
  • 47
  • Thanks for your answer, but I "tried" to avoid 3rd-party components as much as possible! I'll use these as my last resort if-and-only-if I can't figure out any way to do it. Thanks. – PiggyChu001 Dec 18 '20 at 08:56
  • OK, I think that you are trying something a little bit to big to avoid 3rd party components, but I am not sure though so you can keep on, I wish you good luck. On the other hand delegates are functions that can be stored in the code itself soas to be invoked whereever in the code its appropiate, such an event that is a delegate specific. However I dont know if this approach can be used to scape compilation, and introduce not compiled code. It would be nice to know – rustyBucketBay Dec 18 '20 at 09:43
  • I think I had definitely bite off more than I can chew! I did some more research on `DynamicMethod`, and it so complicated and confusing that I might as well better off reading Martian literatures! >" – PiggyChu001 Dec 18 '20 at 10:21
  • haha yeah. Making a programming language yourself is really advanced stuff. Give a try to the assets, that is what they are for!!! The cool thing is that if later on you still motivated to make a solution yourself, you can rely in some of the tips you may catch in other solutions, so that you can approach a really big problem in smaller steps because the initial step might be to big at the beginnig for your current knowledge. That the awesome thing of code and knowledge sharing ;) – rustyBucketBay Dec 18 '20 at 13:12