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. References and examples
  2. Common Patterns

Class constructors

PreviousSafe downcastingNextHash maps

Last updated 1 year ago

Was this helpful?

CtrlK

Was this helpful?

Redscript does not have natively support constructors and to set the data of class fields you would have to either set them manually

public class CustomClass{
    //all the variables 
    public let myInt: Int32;
    public let myString: String;
}
let myCustomClass = new CustomClass();
myCustomClass.myInt = 1;
myCustomClass.myString = "Hello";

or if you want to do it with a single clean line, you can define a static helper method

public class CustomClass{
    //all the variables 
    public let myInt: Int32;
    public let myString: String;
    
    public static func Create(inputInt: Int32, inputString: String) -> ref<CustomClass>{
        //you create the new instance of your class here instead of in your code
        //and set its variables
        let self = new CustomClass();
        self.myInt = inputInt;
        self.myString = inputString;
        return self;
    }
}

and how to use it

let myCustomClass = CustomClass.Create(1,"Hello");