Question

Okay, this should be a pretty simple question

I'm working on a game engine type thing that needs to dynamically load external DLLs that contain mod content. The question is basically thus:

I have, within my engine, the following:

public class Mod
{
    public abstract void OnLoad(ModLoadedArgs args);
}

If I were to put, in the DLL, the following:

public class ContentMod : Mod
{
    public void OnLoad(ModLoadedArgs args)
    {
        BaseEngine.Dictionary.Register(new Thing()); // BaseEngine.Dictionary is
                                                     // a static object
    }
}

Would this work properly?

Sorry for the basic question, I'm new to working with DLLs and don't have access to an IDE on my laptop or I would try myself.

EDIT 1:

So... So far so good, except .Load doesn't seem to change the oreDict object I have in Program.cs I feel like this is a stupid problem, but hurts my head nonetheless.

Assembly a = Assembly.LoadFrom(files[i]);
foreach (Type t in a.GetTypes())
{
    if (t is Mod)
    {
        ((Mod)t).Load(new ModOnLoadArgs(oreDict));
    }
}

EDIT 2:

So I added a 'Console.WriteLine' call in the Load function of my Mod, and nothing comes up. It should say something, right? I'm not going crazy? Could I test this better?

Was it helpful?

Solution

The DLL would need a reference to your engine, which is a bit inconvenient, since this will make it more difficult to change stuff in your engine -- you have to be careful not to break backwards compatibility to your DLL developers.

Another solution would be to create a library that only contains the interfaces required by the mod designers:

public interface IMod
{
    void OnLoad(ModLoadedArgs args);
}

This interface library would be referenced both by your engine and by the mod developers -- it's the contract between the two systems. This interface would change rarely, and you are free to develop and modify your engine as you wish.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top