REDscript in 2 minutes
1
// redscript allows line comments
2
/* as well as block comments */
3
4
// it supports global functions
5
func add2(x: Int32, y: Int32) -> Int32 {
6
return x + y;
7
}
8
9
// functions without a type annotation default to Void return type
10
func tutorial() {
11
let x: Int32 = 10;
12
// compiler can infer types for local variables, y will be Int32
13
let y = 20;
14
// it supports arithmetic
15
let sum = x + y + 13;
16
// as well as mutation
17
let mutable = 0;
18
mutable += 10;
19
// numbers with decimal points default to type Float
20
let num = 10.0;
21
// you can cast between some types
22
let uint: Uint8 = Cast(10);
23
// array literals
24
let arr = [1, 2, 3];
25
// array iteration
26
for item in arr {
27
// logging and string operations
28
Log("at " + ToString(item));
29
}
30
}
31
32
// you can define your own classes
33
public class IntTuple {
34
let fst: Int32;
35
let snd: Int32;
36
37
// you can define static member functions
38
public static func Create(fst: Int32, snd: Int32) -> ref<IntTuple> {
39
let tuple = new IntTuple();
40
tuple.fst = fst;
41
tuple.snd = snd;
42
return tuple;
43
}
44
45
public func Swap() {
46
let tmp = this.fst;
47
this.fst = this.snd;
48
this.snd = tmp;
49
}
50
}
51
52
// you can replace existing in-game methods by specifying the class they belong to
53
@replaceMethod(CraftingSystem)
54
private final func ProcessCraftSkill(xpAmount: Int32, craftedItem: StatsObjectID) {
55
// instantiate a class using the new operator
56
let xpEvent = new ExperiencePointsEvent();
57
xpEvent.amount = xpAmount * 100;
58
xpEvent.type = gamedataProficiencyType.Crafting;
59
GetPlayer(this.GetGameInstance()).QueueEvent(xpEvent);
60
}
61
62
// you can add new methods to existing classes as well
63
// they are visible to other code using the class
64
@addMethod(BackpackMainGameController)
65
private final func DisassembleAllJunkItems() -> Void {
66
let items = this.m_InventoryManager.GetPlayerItemsByType(gamedataItemType.Gen_Junk);
67
let i = 0;
68
for item in items {
69
ItemActionsHelper.DisassembleItem(this.m_player, InventoryItemData.GetID(item));
70
};
71
// some methods require CName literals, they need to be prefixed with the n letter
72
this.PlaySound(n"Item", n"OnBuy");
73
}
Copied!
Copy link