AI Porn is here, Create and Fap TRY FREE
x

Tutorial My engines mod and script system

Diconica

Well-Known Member
Apr 25, 2020
1,159
1,222
295
I wanted a way to mod the games made on my engine without needing to recompile the game and I didn't want to deal with a slow script language.
So I started with creating an API for the game that way I could allow mods to be compiled as DLLs and load them from a mod directory.
That is fine for sharing mods but it could be easier for developing mods. So I wanted to build in a compiler to the game.
GNU isn't made to do that and if you include it in a distribution it becomes a pain because of the license. Clang and LLVM license fit the bill.
There are effectively 3 ways to include clang in a project. You can target and use the DLLs or you can run clang using system or create a process using clang on windows.
The problem with directly implementing clang in the game engine is if it crashes or hangs during a compile your game crashes so the last 2 options are best.
So I figure I may want to compile more than 1 mod and even have more than 1 file in a mod.
So I came up with a rule that each mod had a sub folder in the mod_src folder. The name of the mod folder would be the name of the resulting mod. Any files in that folder would be assumed as part of the mod if they were in this case .c and .h files.
I can then compare the dates of the src files to the DLLs generated in the mod folder. If the src file is newer then compile it again. If not don't. That means checking each file in the src folder and comparing to the DLL file. If the DLL file doesn't exist obviously compile it.
The game has a micro IDE. Any errors from compilation will cause that to come up displaying the compile errors. Compiled mods you have no worry of that.
There are two ways compilation gets started. First is if you are in the ide you can click a button to compile. If you start the engine it will try to compile the mods if needed. If a failure happens at that point it will bring up the micro ide and display the error.

Most the work ended up me removing 1.3 gigs of stuff I didn't need from clang. Mostly libraries of systems I have no intent on supporting. That said you don't need to do that. You could also not directly include it in your system and have it download if the users want to develop mods with game. You could even make the ide optional by just including it in a DLL.

So what is the benefit. Mods that run at native speed. The individual making the mod only needs to deal with programming and clicking a button to test it or restarting the game. You can recompile it from in the game test it and make changes again and not have to shut the game down each time you recompile. It is possible for the engine to release the DLL and then grab it after recompile.