You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+13Lines changed: 13 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,11 +9,17 @@ struct Message:
9
9
contents: string
10
10
sender: offline player
11
11
const timestamp: date = now
12
+
converts to:
13
+
string via this->contents
12
14
```
13
15
A template's name and field names are case-insensitive. The template name follows the same rules as a function name, while field names can only consist of letters, underscores, and spaces.
14
16
Each field has a name and a type, as well as an optional default value. This default value may be an expression, as it is evaluated when a struct is created, not when the template is registered.
15
17
Adding `const` or `constant` to the start will prevent the field from being changed after creation.
16
18
19
+
Structs also support conversion expressions, which allow you to define how a struct can be converted to other types. The syntax is
20
+
`<type> via <expression>`, where `<type>` is the type to convert to and `<expression>` is an expression that evaluates to that type.
21
+
The expression may use `this` to refer to the struct being converted. Multiple conversions can be defined by adding more lines under the `converts to:` section.
22
+
17
23
Creating a struct involves a simple expression:
18
24
```
19
25
set {_a} to a message struct
@@ -31,6 +37,13 @@ set {_a} to a message struct:
31
37
contents: "hello world"
32
38
```
33
39
40
+
### Custom Types
41
+
42
+
Struct templates automatically create a new Skript type using the template's name + `struct`.
43
+
In the above example, `message struct` is now a valid Skript type that can be used in function parameters and other struct fields.
44
+
**You should be very careful when reloading templates, as any existing code that used the type may break if the template was modified or removed.**
45
+
ALWAYS reload all scripts after modifying templates to ensure all code is properly re-parsed.
46
+
34
47
### Type Safety
35
48
oopsk attempts to ensure type safety at parse time via checking all fields with the given name. This means having unique field names across structs allows oopsk to give you more accurate parse errors, while sharing field names can result in invalid code not causing any errors during parsing.
36
49
Any type violations not caught during parsing should be caught at runtime via runtime errors that cannot be suppressed. Note that code parsed in one script prior to updates to a struct in another script will not show parse errors until it is reloaded again, though it should properly emit runtime errors.
"The default value will be evaluated when the struct is created.",
53
60
"Fields can be marked as constant by adding 'const' or 'constant' at the beginning of the line. Constant fields cannot be changed after the struct is created.",
54
61
"Dynamic fields can be made by adding 'dynamic' to the beginning of the line. Dynamic fields require a default value and will always re-evaluate their value each time they are called. " +
55
-
"This means they cannot be changed directly, but can rely on the values of other fields or even functions."
62
+
"This means they cannot be changed directly, but can rely on the values of other fields or even functions.",
63
+
"Converters can be defined in a 'converts to:' section. Each converter is defined in the format '<target type> via %expression%'. " +
64
+
"Note that oopsk cannot generate chained converters reliably, so you should expressly define converters for all target types you wish to convert to.",
65
+
"Be careful when using converters, as they can cause unexpected behavior in all of your scripts if not used properly." +
66
+
"Best practice is to ensure you reload all scripts after defining or modifying struct templates to ensure all converters are registered correctly."
56
67
})
57
68
@Example("""
58
69
struct message:
59
70
sender: player
60
71
message: string
61
72
const timestamp: date = now
62
73
attachments: objects
74
+
converts to:
75
+
string via this->message
63
76
""")
64
77
@Example("""
65
78
struct Vector2:
66
79
x: number
67
80
y: number
68
81
dynamic length: number = sqrt(this->x^2 + this->y^2)
69
82
""")
83
+
@Example("""
84
+
struct CustomPlayer
85
+
const player: player
86
+
rank: string = "Member"
87
+
dynamic isAdmin: boolean = whether this->rank is "Admin"
88
+
converts to:
89
+
player via this->player
90
+
location via this->player's location
91
+
""")
70
92
@Since("1.0")
71
93
publicclassStructStructTemplateextendsStructure {
72
94
@@ -77,6 +99,7 @@ public class StructStructTemplate extends Structure {
0 commit comments