Search Unity

Loading in scripts at runtime for modding

Discussion in 'Scripting' started by Good_Venson, May 18, 2016.

  1. Good_Venson

    Good_Venson

    Joined:
    Oct 12, 2014
    Posts:
    22
    Hi, I am making a game sort of like http://gamejolt.com/games/chateaux-en-guerre/8661#! , 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?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    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:
    https://msdn.microsoft.com/en-us/library/1009fa28(v=vs.110).aspx

    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.
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    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.
     
  4. Good_Venson

    Good_Venson

    Joined:
    Oct 12, 2014
    Posts:
    22
    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
     
  5. Good_Venson

    Good_Venson

    Joined:
    Oct 12, 2014
    Posts:
    22
    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.
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,706
    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.
     
    Last edited: May 18, 2016
    Good_Venson likes this.
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    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).
     
  8. Good_Venson

    Good_Venson

    Joined:
    Oct 12, 2014
    Posts:
    22
    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.
     
  9. Good_Venson

    Good_Venson

    Joined:
    Oct 12, 2014
    Posts:
    22
    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.
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Should work fine for all desktop platforms.
     
  11. Good_Venson

    Good_Venson

    Joined:
    Oct 12, 2014
    Posts:
    22
    Okay, thanks.
     
  12. winxalex

    winxalex

    Joined:
    Jun 29, 2014
    Posts:
    166
    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.