0

I am not really sure that i am picked right title. But here i will try to explain the details what i want to do.

Currently I am developing simple 2d game engine for my own game. Let's say i have 4 classes:

  • Scene and SceneObject: There is SceneObject[] Objects variable in Scene class which represent Objects in Scene (SceneObject could be a text, image, button etc. For text it will be SceneObject.Text, for image it will be SceneObject.Image and so on).
  • ContentManager: The class that handle image source for SceneObject.Image, all Image is stored in this class (Where "Image" is System.Drawing.Image type)
  • Log: Simple Class to logging

What i want is Loading the SceneObject from C# Script, but in that script, i want to allow the script writer to interact with ContentManager (and all classes that may i made in my project, like the Log class)

Here the example of C# Code that to be compiled in Run-time (let's say its called "ExampleScript.cs"):

using System;
using System.Collections.Generic;
using System.Text;

using Engine.Graphics;
using Engine.UI;

namespace Engine
{
    public class Example : Scene
    {
        public SceneObject.Button BtnExample;
        public SceneObject.Text LblExample;
        public Example()
        {
            // This will throw an exception if Example.png isn't exist in the ContentManager
            BtnExample = new SceneObject.Button(ContentManager.GetImage("Example.png"));
            LblExample = new SceneObject.Text("Hello World!");

            // This is the plus for C# Run-Time Code
            // Allow writer to add customization for each SceneObject
            BtnExample.Rotate(10);
            BtnExample.Color = Color.White;
            // and so on
            // I believe complicated effect / customization can be done easier than loading from plain text file (and it's somehow look cooler LOL)

            // Add it to "Objects" variable
            Add(LblExample, BtnExample);
            Log.WriteLine("SceneLoaded!");
        }
    }
}

and then i compile and get all SceneObject like this:

try
{
    Script SceneScript = new Script("ExampleScript.cs");
    Scene Example = SceneScript.Compile();
    SceneObject[] Objects = Example.Objects;
}
catch (Exception ex)
{
    // Compile Error here
    // Something like "'Example.png' isn't exist in content manager"
}

So how can i do this?

I tried to search on Google, but what i found is just something like compiling console application in run-time, that's not enough for me :\

Thanks in advance! :D

CXO2
  • 628
  • 10
  • 28
  • thanks for what. This "question" is somewhat unclear in what your question is. – Dbl Jul 03 '14 at 12:37
  • How can I compile the script in run-time? and I want the script is able to interact with the classes that i made (like `SceneObject`, `ContentManager` etc etc) – CXO2 Jul 03 '14 at 13:20
  • You could compile your script at run time with roslyn too if i recall correctly. That requires .net-4.5 if that's an option for you. It's an interesting topic either way – Dbl Jul 03 '14 at 13:32
  • 2
    You can compile any string into a method by doing this: http://mattephraim.com/blog/2009/01/02/treating-c-like-a-scripting-language/ and for more detail try this http://stackoverflow.com/questions/1799373/how-can-i-prevent-compileassemblyfromsource-from-leaking-memory – Moby Disk Jul 03 '14 at 13:55
  • Thank you, that thread help me a lot! – CXO2 Jul 03 '14 at 15:43
  • Thank you for @Moby-Disk! this link help me a lot: http://stackoverflow.com/questions/1799373/how-can-i-prevent-compileassemblyfromsource-from-leaking-memory – CXO2 Jul 03 '14 at 15:45

1 Answers1

0

I would recommend looking at the CS-Script library. In particular, take a look at their documentation around the 'type-sharing' pattern, which I think will do exactly what you are looking for (albeit you need to use their runtime library to do it)

Brandon Langley
  • 553
  • 3
  • 9
  • The documentation makes CS-Script look like a whole .NET runtime. Is it really just a wrapper for the CompileAssemblyFromSource method? – Moby Disk Jul 03 '14 at 13:54
  • It's really just a wrapper (but with a lot of utility beyond what I would consider a basic wrapper), but the doc is definitely a little misleading there. Looks like there is also some support for Mono and alternative CLRS, although I have no idea how deep that goes. – Brandon Langley Jul 03 '14 at 14:09