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
- You almost never need a hand-written `boolexpr`. The enum natives have overloads without a
1740
+
filter, and the closure wrappers let you filter inline with normal `if`.
1741
+
- Don't pre-optimize by reaching for the lowest-level construct. The compiler inlines and
1742
+
optimizes high-level code; readability and correctness come first.
1743
+
- If you genuinely do need a `code` value (e.g. a native with no Wurst wrapper), remember a
1744
+
parameterless, non-capturing lambda is also a valid `code` value. See
1745
+
[Lambda expressions as code-type](#lambda-expressions-as-code-type).
1746
+
1747
+
In short: `code` and `boolexpr` remain available in Wurst so it can talk to the engine, but when
1748
+
you write your own logic, use closures.
1749
+
1631
1750
### Lambdas and Closures
1632
1751
1633
1752
A lambda expression (also called anonymous function) is a lightweight way to provide an implementation
@@ -1710,8 +1829,8 @@ doAfter(10.0, () -> begin
1710
1829
doMoreStuff()
1711
1830
end)
1712
1831
```
1713
-
It is also possible to have a return statement inside a begin-end expression
1714
-
and `return` can now be used anywhere inside the closure statement block.
1832
+
A `return` statement can be used anywhere inside a closure body, including inside
1833
+
a begin-end expression.
1715
1834
1716
1835
Inside a closure body, `it` refers to the closure object itself. This can be used for self-references, for example to destroy the closure from within its own body without passing it as a lambda parameter.
Copy file name to clipboardExpand all lines: _doc/wurstbeginner.md
+15Lines changed: 15 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -128,6 +128,21 @@ The functions inside the `Printing` package then reference the original warcraft
128
128
129
129
You can use the global vscode search or file search (`ctrl+p`) to find packages you're looking for.
130
130
131
+
## Printing and Debugging
132
+
133
+
The `print` function is your most basic debugging tool. It takes a `string` and displays it on screen.
134
+
135
+
Unlike some scripting languages, Wurst does not implicitly convert other types to text. To print a number or any non-string value, convert it explicitly with `.toString()` and build the message with the `+` operator:
Most standard library types provide a `.toString()` method, and it is conventional to add one to your own classes for readable debug output (use a separate `display()`-style method if the text is meant for players rather than debugging).
143
+
144
+
For longer-lived or filtered logging, the standard library also offers the `ErrorHandling` and `Execute` utilities, but `print` plus `.toString()` covers the vast majority of debugging needs.
145
+
131
146
## Coding Conventions
132
147
133
148
In Wurst, it is generally not recommended to call most natives directly. Instead, an extension function should be used to make the code more concise, readable, consistent and documented. Extension functions can also be easier found via autocomplete.
Copy file name to clipboardExpand all lines: _tutorials/getclose.md
+19-3Lines changed: 19 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -115,11 +115,11 @@ init
115
115
print(i) // This will print "1"
116
116
```
117
117
118
-
Here, the second print uses the value of `i` as it was initially. Setting it inside the closure only has an effect in the closure's context. To share changes, you can use a `Reference<T>` to wrap the variable:
118
+
Here, the second print uses the value of `i` as it was initially. Setting it inside the closure only has an effect in the closure's context. To share changes, you can wrap the variable in a `Reference<T>`, created with the `reference()` helper:
119
119
120
120
```wurst
121
121
init
122
-
var i = new Reference(1)
122
+
let i = reference(1)
123
123
execute() ->
124
124
i.val = 2
125
125
print(i.val) // This will print "2"
@@ -128,7 +128,23 @@ init
128
128
destroy i
129
129
```
130
130
131
-
By using `Reference<T>`, both the closure and the rest of your code can work with the same underlying value.
131
+
Because the reference is a normal object, the closure captures the same instance the outer code holds, so both work on the same underlying value. Remember to `destroy` it when you are done (or use `i.into()`, which returns the contained value and destroys the reference in one step).
132
+
133
+
---
134
+
135
+
## Early return inside a closure
136
+
137
+
`return` works inside a closure body just like it does in a normal function: it exits the closure.
138
+
139
+
This is useful for flattening with guard clauses. Instead of wrapping the rest of the body in an `if`, you return early on the cases you want to handle separately and keep the main logic unindented:
0 commit comments