# 🔷 Modules

Modules are design to allow development of silo code; that is, code that the engine is not necessarily aware of and cannot reference but modules themselves are allowed to reference engine code. Modules can also be enabled and disabled when the engine is compiled to either achieve a lower binary size or to disable/enable certain features and functionality.

Module development allows developers to treat C++ as a glorified scripting language within Nuake, without ever needing to hack the engine to add functionality. Modules are statically compiled directly into the Nuake library.

# Module Creation

To create a module, first create a directory in the `Nuake/Source/Modules/` folder. This folder can be called anything but must not contain spaces or special characters. This directory name forms the fundamental machine name of your module and is use throughout module development.

Once this directory has been made, make a file called `Module.lua`. The module system will search for this file in particular which must contain a module descriptor, as sampled below:

```lua
return {
    name = "My Cool Module",
    description = "This module just does stuff",
    module_header = "MyModule.h",
    -- Place all your sources here
    sources = {
        "MyModule.cpp"
    }
}
```

* `name`: The friendly name of the module, this is used for debug and display purposes.
* `description`: Another “for display purposes” feature.
* `module_header`: The main header to be included within the module translation unit upon code generation.
* `sources`: A list of sources the module wishes to add into the Nuake static library.


:::warning
Sources does not currently support Glob patterns. Explicit listing of all related sources must be within this list.

:::


## What can you do with modules?

As more of the engine gets exposed to modules, this list of features is expected to grow with time. At the moment, Nuake officially support the following:

* Adding custom asset resolver
* Adding custom asset baker
* Adding custom scene systems
* Registering custom components
* Extending the scripting API

For example, the `AssimpModule` is a great example of an asset baker. It takes in a source file, and outputs a new one(Preferably one Nuake can understand)

There is also the `AudioModule` which registers a scene system that takes care of everything audio related. This can be a good base to implement a third-party audio library like `FMod`.

Here is a code snippet of the `AudioModule`:

```cpp
#include "AudioModule.h"
#include "AudioSceneSystem.h"

#include "Nuake/Scene/SceneSystems.h"

void AudioModule_Startup()
{
	using namespace Nuake;

	SceneSystemDB::Get().RegisterSceneSystem<AudioModule::AudioSystem>();
}

void AudioModule_Shutdown()
{
}
```

## Custom components

You can easily add new components which will be available in the editor and will be serialized for you. Of course, these components are pretty much useful unless you create a system for them, which is also possible.  
  
Here is a sample on how to create your own component:

```cpp
// Has to be in the Nuake namespace
namespace Nuake
{
    // Create a new class which inherits from Component
	class MyModuleComponent : public Component
	{
        // This macro registers the type with a display name
		NUAKECOMPONENT(MyModuleComponent, "My custom component")

        // This function is where you expose fields and bind them
		static void InitializeComponentClass()
		{
            // The first parameter is the internal name of the field, the second is
            // is a display name which will be displayed in the editor
			BindComponentField<&MyModuleComponent::MyFloat>("MyFloat", "My Float");
		}

	public:
		float MyFloat = 1.0f;
  
	};
}

void ExampleModule_Startup()
{
    using namespace Nuake;
    
	// Finally, you have to call this function on your component to initialize it
	MyModuleComponent::InternalInitializeClass();
 }
```

After rebuilding, your new component should now be displayed in the editor like so:

[Nuake_Engine_vLTwk0yvK2.mp4 1608x934](https://docs.antopilo.dev/api/files.get?sig=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ1cGxvYWRzL2FlOGRmOTVjLWZlZTktNGRmNy1hZjVlLTBkNTJkYjg4MmJjZC81MDdhNTZlZi1mZjc4LTQzOGMtYmQ1OS0wOTJjYzJlZmU0MWYvTnVha2VfRW5naW5lX3ZMVHdrMHl2SzIubXA0IiwidHlwZSI6ImF0dGFjaG1lbnQiLCJpYXQiOjE3OTYzMDEwMDAsImV4cCI6MTc5NjM4NzQwMH0.e5PknF0AmUPYy6ILesQGl-gy9ZlmUvtfWiX9-PCInlU)

## Extending the Scripting API

It might be useful to expose certain functions to the scripting API to make them available in your game scripts. For example, it might be useful to move some game logic in C++ for performance reasons. You can do this with modules.

Here is a simple example:

```cpp
// ExampleModule.cpp
void SimpleFunction()
{
  // Do something!
}

int ExampleSum(int a, int b)
{
  return a + b;
}

NUAKEMODULE(ExampleModule)
void ExampleModule_Startup()
{
  auto& module = ModuleDB::Get().RegisterModule<ExampleModule>();
  module.BindFunction<SimpleFunction>("SimpleFunction");
  module.BindFunction<ExampleSum>("ExampleSum", "a", "b"); // Specifying argument names
}
```

The next step is to generate the scripting API for your modules. Don’t worry, it’s a very simple process.


1. Recompile the engine to register the new modules
2. Launch the editor with the `--generate-bindings` argument to generate a `bindings.json` file.
3. Recompile `Nuake.Net`(This will launch the API generator with the new binding data)

  
Now, `Nuake.Net` contains your new functions that you can use:

```csharp
// GameScript.cs
ExampleModule.SimpleFunction(); // Invokes the C++ SimpleFunction function
int sum = ExampleModule.ExampleSum(5, 2); // Invokes the C++ ExampleSum and returns 7
```

---

**Documents**

- [Project Status](https://docs.antopilo.dev/s/2b239c4c-0499-4059-8de9-b240f71887c0/doc/project-status-6fyDviuXlE)
- [Welcome](https://docs.antopilo.dev/s/2b239c4c-0499-4059-8de9-b240f71887c0/doc/welcome-yF3kGz8XH3)
- [Workflow](https://docs.antopilo.dev/s/2b239c4c-0499-4059-8de9-b240f71887c0/doc/workflow-S9ota9vgbD)
- [Scene](https://docs.antopilo.dev/s/2b239c4c-0499-4059-8de9-b240f71887c0/doc/scene-HdnkvdILFd)
- [Scripting](https://docs.antopilo.dev/s/2b239c4c-0499-4059-8de9-b240f71887c0/doc/scripting-LI3IE46tt2)
- [UI](https://docs.antopilo.dev/s/2b239c4c-0499-4059-8de9-b240f71887c0/doc/ui-ZW3OQfS29Z)
- [Debugging](https://docs.antopilo.dev/s/2b239c4c-0499-4059-8de9-b240f71887c0/doc/debugging-UmswlxDLyv)
- [Modules](https://docs.antopilo.dev/s/2b239c4c-0499-4059-8de9-b240f71887c0/doc/modules-h26DveHarI)