Search Unity

Is it possible to create modifiable games using this method?

Discussion in 'General Discussion' started by kennethrunescape2019, Jun 24, 2019.

  1. kennethrunescape2019

    kennethrunescape2019

    Joined:
    Feb 5, 2019
    Posts:
    109
    I am wanting to create a game players can make mods for. Suppose I have a class called "Vehicle". Within this class are all the statistics about the vehicle such as max speed, turn speed, etc. The class also contains the functions the vehicle can perform. For example when the "W" key is pressed it calls the "Accelerate" function. All vehicles will contain a class that is a subclass of "Vehicle" which contains functions that override the originals in the "Vehicle" class.

    With that being said, when someone wants to create a mod vehicle for the game, is there any way to give them access to the "Vehicle" class so they can create their own subclass for the vehicle?

    Thanks in advanced.
     
  2. sxa

    sxa

    Joined:
    Aug 8, 2014
    Posts:
    741
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    There is many ways to do moddable game.
    The easiest way, is to expose dll files and allow users to replace it, with modified dll.
     
    Kiwasi likes this.
  4. Gladyon

    Gladyon

    Joined:
    Sep 10, 2015
    Posts:
    389
    You may want to take a look at Harmony:
    https://github.com/pardeike/Harmony
    This library has the ability to replace methods within an existing Dll, without having to recompile the Dll.

    In order to make your game moddable using Harmony, the idea is that the modders will have to compile their own C# Dll by referencing the Dlls of your game (so they will be able to access your classes) and Harmony's Dll (so they will be able to execute Harmony).

    On your end, you will have to create a way for people to load the modders' C# dlls into your game, and execute one of their method (you can force them to implement a 'ModInit' interface which contains the 'Initialize()' method).
    In that 'Initialize()' method they will execute Harmony in order to replace parts of your own code by their own.


    I know that it seems a bit scary to have an external Dll replacing methods deep within your own Dlls, but it actually works well. I do not use Harmony because I have developed the same features in parallel (I learned about Harmony a bit too late...) and had no problem with it.
    Also, Harmony is already used to mod several games (Unity games if I am not mistaken).
     
    NotaNaN, Samuel411 and xVergilx like this.
  5. kennethrunescape2019

    kennethrunescape2019

    Joined:
    Feb 5, 2019
    Posts:
    109
    I don't think you understand my question. Is there any way to where I can give the modders access to the base class and use it to make their own sub class?
     
  6. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    @kennethrunescape2019 I think everyone who has responded to your questions is understanding you just fine. External users (non-developers) of your game cannot have direct access to your base class and create subclasses, short of you providing the source code for them. This would require them to possess your entire game and build it just like you do. You would be essentially giving your game away.

    The suggestions by the other responses provide you solutions for ways that your users can develop their own source code extensions that sits on top of your existing code via DLLs, using an API that you provide to them.

    Aside from DLLs, there are still numerous ways for you to support modding your game. Personally, for my game, I am writing a scripting layer using Miniscript, authored by @JoeStrout. This method gives you more control over what you expose to your users, so as not to inadvertently expose exploits that break your game.

    With Unity, I've also explored allowing users to create their own AssetBundles and/or data, considering the Unity Editor is free. Er, I just looked at the ModTool link provided by @sxa. That looks pretty cool. I'll have to look at that.
     
    Last edited: Jun 27, 2019
  7. Gladyon

    Gladyon

    Joined:
    Sep 10, 2015
    Posts:
    389
    Sorry, I haven't understood your initial question correctly.
    The thing is, I do not know where you are stuck about that, because it just work fine without any specific trick.

    I mean, any modder will be able to create a class which inherits from your 'Vehicle' class, provided your class is public and the modder's DLL reference the DLL which contains the 'Vehicle' class.

    Of course, you will have to provide the modders a way to add their code into your game, for that there's usually 2 ways:
    - you have some code which will load the modders' DLLs
    - you have some code which will compile at runtime the C# code written by the modders
    There's nothing built-in in Unity for that, you have to create that part yourself.


    If you want to help modders, I suggest that you set all your classes as 'public'.
    Also, you should use 'virtual' extensively for your 'Vehicle' class and avoid using 'private' in it.
     
  8. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,980
    its like nearly 2 years later, I doubt they are waiting for you to answer :)
     
  9. Gladyon

    Gladyon

    Joined:
    Sep 10, 2015
    Posts:
    389
    Sorry.
    Reminder for me: do not answer a post without checking the date, even when my answer have just been 'liked' in my 'Alerts'.
    Reminder for me 2: do not post anything at 5.55am local time, get some sleep first... ;)
     
    aer0ace, NotaNaN and MadeFromPolygons like this.