Personal tools

Lua/Tutorials/Module loading process

From JC2-MP Documentation

Jump to: navigation, search

Server startup

When the server starts, it iterates through each directory inside of scripts/ and loads it as a module. The module's name is the directory name. If the module's name contains disabled, it is not loaded. After all modules are loaded, the ServerStart event is called and the ModulesLoad event is called.

Module loading

When a module, such as MyModule, is loaded on the server, script files in scripts/MyModule/server/ are automatically ran:

  1. Any files named _init.lua are ran, including subdirectories.
  2. Any files with the .lua extension are ran, including subdirectories.
  3. The ModuleLoad event is fired.
  4. If this is the last module loaded, (at server startup, or using the load or reload commands), the ModulesLoad event is fired.

Clients are sent any scripts in scripts/MyModule/client/, and the same process happens as above, with one difference: after a module is loaded, the client tells the server to fire the ClientModuleLoad event. It is important to note that the client does not store the scripts anywhere.

There is also a shared directory. Scripts in here are ran before server and client.

Individual script files in a module can interact with each other just fine. There is no need to require anything. However, scripts in two different modules can only interact in a few ways. See Cross-module communication.

The order in which scripts are loaded is undefined. It seems to be alphabetical order, but you should not depend on it.

Module unloading

When a module is unloaded, either on server shutdown, client disconnect, or the reload or unload server console commands, the ModuleUnload event is called.

lua/autorun/ directory

If IKnowWhatImDoing is enabled in the server's config.lua, script files in the lua/autorun/ directory will be loaded before every module. The directory is in the root of the server. It follows a similar server/client/shared directory structure. Example: 'MyJcmpServer/lua/autorun/client/MyScript.lua'

See also