Modules Introduction

From Multi Theft Auto: Wiki
Jump to navigation Jump to search

Modules are extensions for Multi Theft Auto's Lua core, allowing the integration and use of custom Lua functions that have been written in C++, and compiled as a DLL or SO file. Modules are commonly used to create functions for such purposes that Multi Theft Auto lacks, such as sockets.

Getting sterted

To start writing new modules, you need to have at least basic knowledge of programming in C/C++.
This tutorial does not teach you how to get started with C++.
Let's start by downloading a template to create our own module. We can do it in 2 ways:

From terminal

Make sure you have the GIT source control package installed. Open the command prompt (Win + R -> CMD) and enter this command:

git clone https://github.com/TracerDS/mtasa-modules.git

From website

Go to the website of the sample module and download it (Green button "Code" -> "Download ZIP") then unpack the archive

How do I start?

Go to the

./mtasa-modules/ml_example

folder

Windows

Open CMD in that folder and then run the newModule.bat script in this form: .\newModule.bat moduleName e.g. .\newModule.bat myModule

Linux

Open terminal in that folder and then run the newModule.sh script in this form: ./newModule.sh moduleName e.g. ./newModule.sh myModule


After running the script, a new folder should be created called myModule_module. A Visual Studio project and a README file that you MUST read will be created in the current folder.

Template's content

    The structure of the template module

  • include
    • init
      • Common.h
      • ILuaModuleManager.h
      • lauxlib.h
      • luaconf.h
      • lua.h
      • lua.hpp
      • lualib.h
    • CFunctions.h
    • CLuaArgument.h
    • CLuaArguments.h
    • ml_moduleName.hpp
  • lib
    • lua5.1.lib
    • lua5.1_64.lib
  • src
    • CFunctions.cpp
    • CLuaArgument.cpp
    • CLuaArguments.cpp
    • ml_example.cpp
  • ml_moduleName.sln
  • ml_moduleName.vcxproj
  • ml_moduleName.vcxproj.filters
  • ml_moduleName.vcxproj.user
  • README

Files are marked in green font
Folders are marked in bolded blue font
Files and folders that should not be modified are marked in red

What is ___?

Q: Folder init?
A: Dont touch this folder. The headers needed for initial module initialization are stored there.

Q: File CFunctions.h?
A: The declarations of user-created functions are stored there. The definitions of these functions are stored in the file CFunctions.cpp.

Q: Folder lib?
A: Do not touch it at all. It stores the libraries needed for the connection between C++ and Lua.

Q: Files CLuaArgument/s.h/.cpp?
A: These files are used to correctly handle parameters from C++ to Lua