Loading in scripts at runtime for modding

Hi, I am making a game sort of like Game Jolt - Share your creations! , but with moddable cards. With the current version, you can set several predefined values (like card cost, amount of damage it deals, description, etc…) for each card to create an interesting deck. But now I want to take it a step further and let the player create scripted templates for new card types that are not currently handled. However, I do not know how to load in those player created scripts at runtime.

What would be the best method to do this in this case?

First off, this will only work for desktop builds. AOT builds for platforms like iOS and WebGL do not support dynamic loading of scripts (because it’s compiled ‘Ahead Of Time’).

What you’d do is dynamically load the assemblies with Assembly.LoadFrom:

Once you have the assembly loaded, you can look up types and use them for various stuff.

From here, it’s a design matter. How you want to hook in to allow them control. Maybe they get to write their own MonoBehaviours, which you attach to GameObjects. Maybe they implement some interface that you load up and call the members of. From here it’s a matter of what you need specifically… but at least this gets you your code loaded into the application domain.

You can also look into scripting solutions like IronPython that will let you write python code in plain .py files that your game loads, compiles, and executes. We have the functionality in our framework but ended up not using it for abilities because the performance wasn’t good enough. It would probably work for a card game though where the hitch associated with it wouldn’t be as noticeable.

Thank you. This sounds like what I need. I like the idea of using a common interface. I can give the user enough freedom to do a lot of things while retaining a structured system that will be easy to use. While a game like this would be cool on mobile, and pretty easy to implement otherwise, it isn’t a big issue. Who wants to write scripts on a phone keypad anyway? =P

Yes, I was looking into some similar solutions using lua scripting, but it doesn’t seem much different than doing the same thing with c# code… As for the performance issue, yeah, it wouldn’t be a problem.

Gamers may be more comfortable and familiar with Lua than C#, though. Lua’s a fairly standard modding language. And there are plenty of Lua implementations (like this one) that run in Unity and work with AOT builds like iOS and WebGL.

1 Like

It also doesn’t require modders to compile their code to a DLL.

Either approach is fine and in fact our plan is for “heavier” mod code to be loaded through assemblies (once we get there).

Interesting. I’ll keep that in mind. So far, I am equally ignorant of both methods, ha ha. I like the fact that lua is already a somewhat common modding language, and that there are implementations that work with AOT.

I finally came up with a solution that works really well for my situation.
I looked into loading assemblies, but it seemed way out of scope, although it was very interesting.
Now, I am using a lua script wrapped as a string in a serialized class which also contains other variables like card name and card description.
I am currently building a deck builder tool with a card creator with built in lua editor. This way, the player can create decks and cards easily without ever leaving the game.

One question I have is about serialization. Currently, I am serializing to xml using system.xml.serialization, and saving to the file system. This works well on windows, but I have no idea as to what other platforms this will work on, and I’d like to use the most flexible method.

Should work fine for all desktop platforms.

Okay, thanks.

You can save scripts structure, variable values and reference connections into asset. Then you can load asset that will recreate structure. Graph is good structure to save all scripts relations/dependencies. I’ve made platform were you can draw and connect scripts. Then you can save in asset in runtime. Then load the asset.