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 may need use casts in order to perform operations on different types of numbers (Int32, Int64, Float etc)
22
// the type of mult here will be Float, since we cast the integer literal 10 to Float
23
let mult = 10.0 * Cast(10);
24
// array literals
25
let arr = [1, 2, 3];
26
// array iteration
27
for item in arr {
28
// logging and string operations
29
Log("at " + ToString(item));
30
}
31
}
32
​
33
// you can define your own classes
34
public class IntTuple {
35
let fst: Int32;
36
let snd: Int32;
37
​
38
// you can define static member functions
39
public static func Create(fst: Int32, snd: Int32) -> ref<IntTuple> {
40
let tuple = new IntTuple();
41
tuple.fst = fst;
42
tuple.snd = snd;
43
return tuple;
44
}
45
46
public func Swap() {
47
let tmp = this.fst;
48
this.fst = this.snd;
49
this.snd = tmp;
50
}
51
}
52
​
53
// you can replace existing in-game methods by specifying the class they belong to
54
@replaceMethod(CraftingSystem)
55
private final func ProcessCraftSkill(xpAmount: Int32, craftedItem: StatsObjectID) {
56
// instantiate a class using the new operator
57
let xpEvent = new ExperiencePointsEvent();
58
xpEvent.amount = xpAmount * 100;
59
xpEvent.type = gamedataProficiencyType.Crafting;
60
GetPlayer(this.GetGameInstance()).QueueEvent(xpEvent);
61
}
62
​
63
// you can add new methods to existing classes as well
64
// they are visible to other code using the class
65
@addMethod(BackpackMainGameController)
66
private final func DisassembleAllJunkItems() -> Void {
67
let items = this.m_InventoryManager.GetPlayerItemsByType(gamedataItemType.Gen_Junk);
68
let i = 0;
69
for item in items {
70
ItemActionsHelper.DisassembleItem(this.m_player, InventoryItemData.GetID(item));
71
};
72
// some methods require CName literals, they need to be prefixed with the n letter
73
this.PlaySound(n"Item", n"OnBuy");
74
}
Copied!
Last modified 1mo ago
Copy link