Conditional compilation
REDscript supports conditional compilation, which can be used to handle complex cases of conflicting functionality between different modules. It allows you to compile different versions of your code based on some information that is available to the compiler. It's achieved through the use of a special @if annotation on top-level declarations and imports.
1
module My.Mod
2
// conditional import
3
// it'll be available only when the module is present
4
@if(ModuleExists("Other.Mod"))
5
import Other.Mod.*
6
7
// you can write two different versions of a function based on existence of another module
8
// imports from Other.Mod will be available only in the first version, since the import was conditional
9
@if(ModuleExists("Other.Mod"))
10
func Testing() -> Int32 {
11
return 1;
12
}
13
@if(!ModuleExists("Other.Mod"))
14
func Testing() -> Int32 {
15
return 2;
16
}
17
18
// you can also conditionally define a method wrapper/replacement
19
@if(!ModuleExists("Other.Mod"))
20
@wrapMethod(PlayerPuppet)
21
protected cb func OnGameAttached() -> Bool {
22
Log("I'm in a wrapper");
23
wrappedMethod();
24
}
Copied!
Only a small subset of REDscript is allowed in the condition of the @if annotation. You can only use:
  • Boolean constants (true, false)
  • Standard boolean operators (&&, ||, !, ?:)
  • The ModuleExists function which determines whether the given module exists at compile time
Copy link