Redscript
HomeGitHubDiscord
  • Home
  • Getting Started
    • Downloads
    • Setup for VSCode
    • Setup for JetBrains IDEs
    • How to start REDscripting
      • Step 1: Mod structure
      • Step 2: Finding the right class
  • Language
    • Intro
      • REDscript in 2 minutes
      • How to create a hook
        • Things to hook
    • Language Features
      • Intrinsics
      • Loops
      • Strings
      • Modules
      • Annotations
      • Conditional compilation
      • Configurable user hints
    • Built-in Types
    • Built-in Functions
      • Math
      • Random
      • Utilities
  • References and examples
    • Common Patterns
      • Safe downcasting
      • Class constructors
      • Hash maps
      • Heterogeneous array literals
      • Scriptable systems (singletons)
      • DelaySystem and DelayCallback
      • Generic callbacks
      • Persistence
    • Logging
    • UI Scripting
      • Logging Widget Trees
      • Popups
    • Vehicle system
    • Weapons
    • Codeware callbacks
      • Scriptables comparison
    • Libraries
    • Gameplay
      • Sleeping and Skipping Time
  • Help
    • Community
    • Troubleshooting
Powered by GitBook
On this page
  1. Language
  2. Language Features

Modules

Introduction to modules.

PreviousStringsNextAnnotations

Last updated 1 year ago

Was this helpful?

CtrlK

Was this helpful?

You can organize your REDscript code into modules, which are separate files that start with a line like this:

module MyMod.Utils

A module name is a series of identifiers separated by dots. Each module has its own namespace and its items are not visible outside of the module unless you declare them as public and import them in another file. This helps you structure your code and avoids name conflicts with existing game scripts, so you can use any name without overriding in-game definitions.

If you do not include a module header at the beginning of your file, your code will be in the global scope and implicitly visible everywhere.

Here is an example of a module:

module Math.Constants

public func Pi() -> Float = 3.14159
public func E() -> Float = 2.71828

Types and functions placed in modules are only accessible by fully qualified names at runtime. This matters when you try to

  • interact with them from CET or red4ext

  • refer to them with a CName, which is accepted by some methods like ScriptableSystemsContainer.Get

The fully qualified name consists of the full module path followed by the name of the type/function. In this case these two functions become Math.Constants.Pi and Math.Constants.E.

One should also be careful not to define existing in-game natives like LogChannel inside a module because that will always lead to a name mismatch.

This module has two public functions that return math constants. You can use them in another file by importing them and calling them by name like this:

// In another file
import Math.Constants.Pi

class Circle {
  let circumference: Float;

  public final func Radius() -> Float {
    return this.circumference / (2.0 * Pi());
  }
}

There are different ways to import symbols from a module:

  • import Math.Constants.* will import all public symbols (both Pi and E in this case)

  • import Math.Constants.{Pi, E} will only import the symbols you specify (Pi and E in this case)