Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make a game engine with C# and I want the games to be extremely modifiable
(E.G. External uncompiled C# code and sprites located outside the engine)

I also want the external C# to call classes and methods from within the engine.

EG:

Engine code:

Program.cs (compiled)

C#
using System;

namespace GameEngine
{
    class Program
    {
        static void Main(string[] args)
        {
            //Code to open args[0] (Path to CS file)
            //Execute it
        }
    }
}


Engine.cs (compiled)

C#
using System;

namespace GameEngine
{
    class Program
    {
        static void SayHi()
        {
            Console.WriteLine("Hello");
        }
    }
}


Game Code:

Program.cs (uncompiled)

C#
using System;
using GameEngine;
using GameEngine.Engine;

namespace MyGame
{
    class Program
    {
        static void Main(string[] args)
        {
            Engine.SayHi();
        }
    }
}


Any way I could do this?

What I have tried:

I have tried using "Microsoft.CSharp" and "System.CodeDom.Compiler" but I couldn't find out how to call methods from inside the compiled code.
Posted
Updated 3-May-21 23:25pm
Comments
The Other John Ingram 3-May-21 17:56pm    
Look up plugins, they are modules of code that can be loaded in at startup.
Different plugins can do different things and can be updated.

Maybe this will help?

Compiling Source Code from a String[^]
 
Share this answer
 
As someone has already commented on your question, if you want to expose your engine's functions to the external C# you might have a better experience using a plugin engine instead. It is possible to compile the code within the application domain using tools like the System.CodeDom.Compiler but you are going to have to deal with type resolution. And also, anyone scripting for the engine would need to know how to call the functions presumably without the benefits of autocompletion? That could be a headache.

Instead, you could build a DLL containing interfaces representing the core aspects of your game engine, and a contract for the plugin itself. A developer could then reference this DLL and code their implementation within VS. Your application could then fulfil the interfaces in this DLL (ie. IGameEngine -> GameEngine) and load the plugins into the application domain.

This would be good for breaking down your game engine into manageable pieces. If you know a plugin may need to load textures, you could build an ITextureLoader interface for that. If the plugin should be able to define NPCs, skills, AI behaviour, break those down INpc, ISkill, IAIBehaviour
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900