Search Unity

Making a Moddable Game?

Discussion in 'Editor & General Support' started by Tyler-Jaacks, Mar 20, 2015.

  1. Tyler-Jaacks

    Tyler-Jaacks

    Joined:
    Mar 6, 2015
    Posts:
    3
    There are many Unity games out there that supporting modding. When I mean modding I'm talking about C# Modding. I am attracted to these kinds of games because It allows me and others to extend the game. Some examples are Cities: Skyline and Kerbal Space Program. Since I don't know how they accomplish mod support they may do it completing different. But I want to know how add mod support to my game where people just drop a .dll into a folder and on the game starts loads it?
     
  2. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    Making your game support modding is a relatively advanced area of programming (we're adding it to our game so we know first hand).

    What you're looking to do involves loading assemblies at runtime. If you want to let modders code in C# and not worry about compiling themselves, then you can look into runtime assembly creation.

    A quick google provides you with a few starting points


    http://stackoverflow.com/questions/604501/generating-dll-assembly-dynamically-at-run-time
    https://msdn.microsoft.com/en-us/library/dd153782(v=vs.110).aspx
     
    Last edited by a moderator: Jul 9, 2018
    clarHandsome, McMayhem and Ryiah like this.
  3. darkhog

    darkhog

    Joined:
    Dec 4, 2012
    Posts:
    2,218
    It's funny you're saying that, because my game will support mods (at least in the form of custom maps/levels). The way I've realized it is by having a custom level format (which is basically a binary blob of specific structure).

    I'm also working on a level editor so powerful, yet easy to use, that official levels would be made in it. So basically it will be as moddable as original Doom or Quake (minus putting in custom textures since art style of the game is "untextured polygons" ;)).
     
    muzboz, Ryiah and CWolf like this.
  4. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    Modding all the way =). We're doing something similar. We have our own mod tool (also developed in Unity). You can create structures / ships with it, import 3D models (Collada / .dae) at runtime and a few other things relating to the game.

    Instead of loading assemblies at runtime we're going with Lua for the scripting language.
     
    tatoforever and Ryiah like this.
  5. Tyler-Jaacks

    Tyler-Jaacks

    Joined:
    Mar 6, 2015
    Posts:
    3
    CWolf I want to thank you for your help, but that is not necessarily what im looking for. Im looking to load .dlls at runtime that will modify the game like in KSP.
     
  6. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,804
    He just told you how to get started. Did you even check the links?
     
    Ryiah and CWolf like this.
  7. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    Thanks @Ostwind.

    @Tyler Jaacks: You may have explain yourself badly if the information I gave wasn't what you wanted exactly. .dlls for what you described are usually called assemblies. Most modded games implement their modding in their own way but the high level things tend to be the same, or similar.

    In this case, if KSP and Cities use C# .dlls / assemblies then you'll want to look into assembly loading at runtime. If this isn't what you wanted, maybe you can explain exactly what you want in more detailed. The devs will release an API or provide the useful methods for creating things within their game, and the modders will create C# classes that hook into these - then either compile it to .dll or the devs make their game compile the C# code into the assembly as part of their mod support.

    We can't give you the answer out right as modding is too complex an area of coding and it can be done many ways but we can always point you in the right general direction.
     
  8. Zeblote

    Zeblote

    Joined:
    Feb 8, 2013
    Posts:
    1,102
    Another thing to consider is that now with unity personal every player can export asset bundles for including custom graphics/content in his mods, which the game can then load
     
    CWolf likes this.
  9. Random_Civilian

    Random_Civilian

    Joined:
    Nov 5, 2014
    Posts:
    55
    By any chance, has anyone gotten Unity's mono compiler as service to work outside the editor?
     
  10. Tyler-Jaacks

    Tyler-Jaacks

    Joined:
    Mar 6, 2015
    Posts:
    3
    CWolf I thought you meant a C# Scripting thingy without compiling a .cs. Sorry My bad
     
    CWolf likes this.
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    As a general suggestion you should consider making the game moddable from the start. Build your core engine, then build all your gameplay and content as mods.
     
  12. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    I agree with @BoredMormon completely. If you want to do modding well you need to plan it from the start of development.
     
  13. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    Is there anything like that on mac? Or will .dlls work because of mono?

    Using unity to build your own sandbox engine of runtime scriptable elements sounds amazing.
     
  14. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    @Tomnnn: I'm not too familiar with mac development but assemblies (for this discussion specifically) are managed c# so I believe they would work (or you'd need to compile them out to another mac format worst case).

    We've taken the approach to make our game as flexible as possible when modding. The core game is coded as a 'core' mod (which can be disabled if the players so wish). We code it in the script / mod layer so we can battle test the mod API (Lua <--> C#). So far it's working very well.

    EDIT: To make it clear we investigated and decided not to go with the reloading assembly route ourself for a few reasons. One main reason was wanted to keep a controlled API and prevent total control of the game code for security reasons. That's why we've gone with embedded Lua.
     
    Last edited: Mar 22, 2015
    muzboz and tatoforever like this.
  15. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    @CWolf I should probably test it while I've still got a mac this week.

    I hope this thread stays open, I've been curious about this topic for quite a while. Even great games can benefit from mods. Skyrim is pretty good (npc character development aside) but the warzone mod fills it with epic battles that people would expect from something like a game of thrones season finale!
     
    CWolf likes this.
  16. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    @Tomnnn: I'd be interested in knowing the results if you do get around to testing it.

    If anyone is interested in mod creation my company blogs about development. We do series of blogs and one of them I write is about modding games (only two articles so far) https://www.roguevector.com/category/mod-support/. My next post will be about integrating Lua into Unity then future articles will go into Unity runtime model importing (since it's not supported by Unity out of the box) and various other useful things.

    I completely agree with you about how even great games can benefit from modding. Back in the day I came from the Fallout modding community and I feel very, very strongly about moddable games. So many benefits can come from allowing users to experiment and change things with your game. This is one of the reasons we've spent so much time setting up a mod framework for our game on top of Unity. We believe the easier we can make it, the more people will get into modding (another reason we picked Lua over C# since we wanted to lower the bar for entry. More people find dynamically typed languages easier to get into than statically typed).
     
    muzboz, AL0932 and Tomnnn like this.
  17. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    @CWolf haha the classes.class(<args>) is something I've never seen before. Nice read, very interesting.

    Since I can't make any promises around this computer, I think we should conclude that since I've seen steam integration with unity games which requires a dll plug-in that mono covers that as well. Mono is magic.

    Can you imagine someone turning besiege into an RTS via modding? That'd be pretty incredible.
     
    CWolf likes this.
  18. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    Yeah, completely. I'm of the mindset that a good game developer, if they have created an open enough modding system, will see mods that are of a higher quality than the core game. Some modders are so, so dedicated to their cause =).
     
  19. Deleted User

    Deleted User

    Guest

    Going to share how I'm doing modding support, maybe it can help you or give a hint. I come from modding Id Tech based engine games, (eg. Quake, Doom 3 etc...) so I wanted to do something similar on that regard. Basicly I do the main game as a mod itself reading everything from external files.

    In Unity Editor itself I do very little, a game scene is just 3 gameobjects, all is managed trough code. First I studied what Unity can do in terms of importing assets at runtime that are not from asset bundles ( consider that I started this before Unity5 release) and how I can facilitate it, like for textures. Textures are easy to import at runtime as there are helpers to do the job, but for other things I needed to write my own parsers and my own file formats, for example for fonts.

    Taking fonts as example I did a simple format for it, is just a bunch of ints and floats that are used to reconstruct at runtime the font. If you check the docs you see the unity font class uses CharacterInfo which hold to all info for uv rect and char position. THen for the texture itself of the font instead of having a separate png I write down the bytes of the alpha channel of every pixel of the font bitmap and reconstruct it from that (I don't need rgb info so I'm good with alpha only). Then the trick is just to read back the bytes from the file trough System.IO and BinaryReader and reconstruct the Font class at runtime.

    This stuff is used also for the rest, like materials, entities and all other stuff with the difference that for materials and other files I use plain text and parse with StreamReader and use Reflection to find the members and fill the values.

    After that you also want to be sure about performance, take for example 2 models that uses the same material in the game, you can create the material twice at runtime and assign it, but the second material would be another instance making it another drawcall, and allocating more memory. What I do is to fill a cache, which is just a class with static functions where I store shared data, I create the material once, store it in the cache and is always accessible by using a simple id search in a dictionary.

    Then I had to do a dictory management system for additional mods, I have my main data folder inside StreamingAssets, and an addon folder where you can put mods in other subfolders. At runtime I check the addon ffolder to see if there is anything in there and populate a list of paths. If someone select a mod the new main path is stored in a string, so everytime the game have to check for files first look up the mod, if nothing is found there it checks the main game and if it fails there too I just put failsafe to return null temporary assets to avoid errors and crashing, so to manage that I had to do also a custom debug log to print down all warnings for missing or wrong paths on assets.

    For example if a texture isn't found I load an error texture from resources instead avoiding a crash but still telling what was wrong.

    So in the end wit works in a override way, if a mod is selected checks mod folder for stuff if nothing is in there go to main. This way is possible also to have mods with same file name of different assets without worrying about conflicts. Is just important to remember to have failsafes in place.

    So if you are ntot going to use assetbundles for assets you have study first what unity can do at runtime, and in the cases unity won't cooperate then find a workaround and build your own system like I had to do with sprite animations as I can't create animations for sprites at runtime with unity functions I had to do my own system.
     
    tatoforever and CWolf like this.
  20. derf

    derf

    Joined:
    Aug 14, 2011
    Posts:
    356
    This is the most important part of creating mod games. IF you are truly going to make a game that users can modify through "Editor Tools" that you will create, then you must first create these tools; and then use the same tools you created to make the default game users can play. Sort of a playable demo of your editor tools like Neverwinter Nights by Bioware.

    This allows you to test your mod tools and make sure users can correctly mod the game features. Whether it be in levels, items, monsters, game play, etc., make sure it will work as you intended by creating the base modded game that comes with your editor tools.

    You can still create the basic logic core of the game whether it be a fantasy game (Neverwinter Nights), a horror game (Amnesia) or a sci-fi game (Galactic Civilization 2).
     
    CWolf and darkhog like this.
  21. darkhog

    darkhog

    Joined:
    Dec 4, 2012
    Posts:
    2,218
    Exactly how I'm doing Computer Virus Simulator. All enemy/player logic and itemdef will be done as code, but levels will be made in level editor I am doing and it will be provided along with the game. This will ensure level editor is so powerful you can replicate whole game in it.

    I'm still struggling a bit with UI for said editor a bit, so help would be very welcome.
     
    CWolf likes this.
  22. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    What's your favorite example of that? For me, first place is a tie between counterstrike existing because of talented modders of half life and the skyrim multiplayer mod.
     
    CWolf likes this.
  23. tiggus

    tiggus

    Joined:
    Sep 2, 2010
    Posts:
    1,240
    I think is probably wise, I wouldn't want someone distributing mods for my game that could startup external processes etc. Even if you put a disclaimer in it would be a pain reviewing or policing them all and when your game wipes someones hard drive they aren't going to care you had a disclaimer about mods being unsafe.
     
    CWolf likes this.
  24. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    @Tomnnn: Hmm... it's hard to pick from a good selection of moddable games. I'd say Half Life modders did some amazing things. I'll always be very fond of the Fallout mod scene (started there). While I'm personally tired of Minecraft the mod community, especially in the early days, did some amazing work there.
     
  25. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    Exactly. We're going to give them as much control as possible as the main game is coded as a mod but, when / if we do provide file system access, we wanted to have complete control over exactly what happens there. We don't want to be responsible for some hard drive wipes. Game crashes? Fine, that's part of the flip side when it comes to modding - but not anything horrible.
     
  26. Deleted User

    Deleted User

    Guest

    CWolf, just a question about the modding of your ame. How do you deal with audio? I'm asking this because I'm having some troubles with importing audio files at runtime, is not a problem of reading bytes and construct an audioclip out of it, is more about memory management I can't get a a good result on that, I would avoid to clutter memory so much. Do you use some external library for streaming from hard disk or something?
     
  27. derf

    derf

    Joined:
    Aug 14, 2011
    Posts:
    356
    Should also point out that their are different degree of modding available based on game example.

    For example Galactic Civilization 2, it has the ability for players to mod it, but that modding is more face plate modding and not altering the underlying logic core. Where in Neverwinter Nights you can literally take control of the underlying logic core and have something totally different occur.

    For example; Neverwinter Nights 2 is basic AD&D 3.5 core rules system, but when a player is damaged you can override the standard "damage subtracted from current hit points" and apply some other custom damage system using the override method OnPlayerDamage event. This has allowed some modded games that take it to a whole new level of game play.

    There was a modder putting a large amount of work into making an Stardrive game system using Neverwinter Nights 2 modding ability.
     
    Last edited: Mar 24, 2015
    Ryiah, Deleted User and CWolf like this.
  28. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    @Neurological: I was the coder on my team for audio so I can share how we went about things. Firstly, this was all done on Unity 4.x. We'll probably rip things out completely now to work with Unity 5.x (if it passes a few prereqs).

    We ended up using NVorbis to load our audio in and then remove it when it's not needed. We didn't hit memory issues yet but we have a lot more work to do with the audio as we only reached a tech demo phase.

    Ideally, the new Unity audio will support streaming from the file system (I don't hold much hope though and I haven't had a chance to investigate it yet). If it doesn't then we'll probably use NVorbis to stream the audio in again and dump it into the new audio system.

    Sorry, not too helpful I know regarding your question on memory management. From my experience, streaming the audio is probably best for memory management on longer files (like music). Not sure if sound effects like benefit from it though.
     
    Deleted User likes this.
  29. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    Yeah, there are so many different levels of 'mod support' that can be provided. From basic configuration file changes to full blown total conversion support, and everything inbetween!
     
  30. Deleted User

    Deleted User

    Guest

    Thanks, well it was helpfull, didn't know about NVorbis. O fcourse my main concern is with music, small audio effects are ok, there is no reason to stream those as they would be needed too often.

    As for Unity 5 audio stuff, I once asked about it on a blog post and they said that streaming is better, but didn't specify if is done trough editor or runtime, so need to be seen, I still didn't touch the new audio features so I don't know for certain.
     
    CWolf likes this.
  31. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    @Neurological: Always good to know that the new streaming will be better. Hit some problems with the old one before too.

    Fingers crossed for more Unity Engine / runtime support. Any time I see 'Unity Editor supported only' my heart sinks. It means, for the way we're adding mod support, it's useless to us. I've been looking a lot into Mecanim runtime support. Since we're unable to create the state machines by scratch at runtime I need to think of some creative ways to get around this issue.
     
  32. Deleted User

    Deleted User

    Guest

    Had to ask that too on the old blog post about the state machine, heres the reply:

    "
    The API is Editor only at the moment. We don’t have short term plans to allow editing of StateMachine at runtime ( other than http://docs.unity3d.com/ScriptReference/AnimatorOverrideController.html)

    But we are working on bringing more flexibility and control over what animations are played at runtime!

    "
     
  33. Gnatty

    Gnatty

    Joined:
    May 17, 2012
    Posts:
    77
    I believe Arma Dayz is a very successful mod.
     
    CWolf likes this.
  34. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    Ha. I read that comment. Didn't realise you asked that. When I read that I was just fed up =). I've been thinking up a few workarounds.

    One is, write my own animation blending system (yuk!). Another is, create a large generic state machine and keep swapping in and out animations and transitions - then use it when appropriate. I've prototyped neither and I'm not looking forward to doing either too.

    EDIT: I've also looked for third-party animation systems to replace Mecanim but all I seem to find are IK add-ons. We already use FinalIK from the asset store, which is great. Will have to keep looking on the off chance too.
     
  35. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    Hah, minecraft. I love the Crash Landing modpack. Using HQM, it gives you an entire story and questline. Using the template map feature, it ensures that you start in a world made of dust inside a crashed ship. Greatest minecraft experience ever.

    Minecraft modpacks provide more content than the game ever will haha. And with HQM, similar combinations of mods can produce entirely different methods of play and progression! HQM is the best mod that ever was.
     
    CWolf likes this.
  36. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    ... and that's the mod effect! I love mods. Problem with me is sometimes I mod things too much. Played a game of X3 and just modded it so much all the mods interfered with each other and pirates broke. They hunted me down so hard it felt like they wanted to take me home to say hi to their mum I suppose you could say =).
     
    Tomnnn likes this.
  37. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    Too much? Enough is never enough until you have flying sharks!
     
    CWolf likes this.
  38. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    the simplest and most flexible modding design i'm aware of, is providing a directory for users to place files. Have them loaded at runtime into your asset database, freely overwriting anything with the same name.

    of course this is prone to a lot of bugs and crashes if people make nonfunctional content, but that's not your problem. Modding the game is generally done at one's own risk.

    This method easily allows asset replacement, and placing of scripts where they're needed. Providing some additional directory for scripts to somehow be autorun covers most additional use cases.

    This is mostly how bethesda does modding in their games, which have proven insanely popular.
     
    graworg and hippocoder like this.
  39. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    Yeah, it's a common approach. Override / mod load order is important too for some of these systems.
     
    rarr4 likes this.
  40. Deleted User

    Deleted User

    Guest

    I still would suggest for some failsafes where is possible to give little info on whats wrong. I'm speaking out of personal experience, wasting a hour trying to pinpoint a problem and realizing I just used a capital letter on a folder path and the system is case sensitive. But for more complex stuff I agree, is people problem to figure out whats wrong and fix it.

    By the way would like to link an interesting project I found recently, hope the author is ok with it: http://www.dfworkshop.net/?page_id=1145

    Basicly is a set of tools for unity to import daggerfall content into unity, is not really modding as everything is done in editor, but there is a lot of interesting code to study for parsing different file formats and see how stuff is structured. The code is well done and I'm finding it usefull to learn new methods to read external data.
     
    CWolf likes this.
  41. Sebioff

    Sebioff

    Joined:
    Dec 22, 2013
    Posts:
    218
    So....what about AssetBundles?
    I've got to admit I have no experience with them, but shouldn't they work for modding? Maybe not for extensive mods that change the entire game, but would they allow to load additional GameObjects with scripts on them (prepared by modders using Unity) into the game?
    From what I understand that's their purpose, and now that they're available in Unity Free it seems like that might be a convenient and easy way to allow at least some modding.
     
  42. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    Asset bundles didn't used to be a real option due to their Pro licence restrictions. Now they are open to all in Unity 5.x it is now a valid option for modding.

    For a lot of developers, they don't want to force modders to go to the Unity Editor as a 'mod tool' for their game. We're one such developer for instance. Asset bundles are Editor only functionality. The Unity Editor can be completely overwhelming for new users and if you're trying to lower the bar of entry for modders it's not a good option in my opinion. Also, to rely on Unity for a mod tool isn't always the best option (adds extra level of dependence).
     
  43. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    @Neurological: Yeah, I agree. Can never be too careful with log / warning messages in logs. Helps cut down debugging massively.

    I saw the Daggerfall editor tools before - I think it looks amazing! I keep meaning to look into the code behind it but I never find the time. I'll have to get to it.
     
  44. Zerot

    Zerot

    Joined:
    Jul 13, 2011
    Posts:
    135
    Assetbundles are a decent option now for art, sounds and models. However, one of their restrictions is that they can't contain code/scripts, so they can't be used to add new logic. With a flexible enough component system/collection modders could still create new items that behave different enough using the existing (provided by you) components, but they would ultimately still be limited.
     
    CWolf likes this.
  45. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    Honestly, letting people use the unity editor as a mod tool sounds like an incredible idea. That could save months of work for a several-person team, not having to write an editor of their own
     
    yoonitee, Sebioff and CWolf like this.
  46. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    Yep, I agree. For some developers it's a great idea. Not for others though.
     
  47. Deleted User

    Deleted User

    Guest

    Well if you look at Might & Magic X they released mod tools in form of editor extension of Unity, there was a limit back then, these tools would require Unity pro, so I think it used to bundle up assetbundles for mod release, not sure the state of the tools now.
     
  48. Sebioff

    Sebioff

    Joined:
    Dec 22, 2013
    Posts:
    218
    Ah, well - still pretty interesting though! Thanks!
    You could still ship LUA scripts or something similar with the AssetBundle I suppose.
     
  49. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,363
    There are different kinds of modding:

    Simplest is just to have assets loaded at runtime. So you could load in textures from a texture sheet. And users could swap that image for something else. Maybe also loading the sound files at runtime. The users feel like they're "hacking" but not really.

    Next simplest is to load lots of properties at runtime from a text file such as an XML file. You could even store all your game level geometry as XML files or say 3DS format.

    Third is to load scripts at runtime stored in text files. Like javascript or Python scripts. This would require added Javascript or Python scripting to your game. There are some c# implementations of these.

    Some games implement there own small scripting languages which users can use to modify games.
     
    graworg likes this.
  50. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    I forgot to mention computercraft when talking about minecraft & modding. ComputerCraft has CraftOS, which is a fully implemented operating system and scripting language in LUA with an API for wireless networks and managing little robots called turtles.

    7daystodie has their crafting recipes and level generation variables in XML files. When ever an update they have makes the game unreasonably difficult for early game survivors, I add in a recipe that uses basic materials to make guns :D Hunting down books in a randomly generated world is not fun.

    @CWolf laptop is back from repairs. the mac is gone. officially unable to test that assemblies thing, sorry.
     
    Last edited: Mar 25, 2015