diff --git a/docs/scarpet/language/SystemFunctions.md b/docs/scarpet/language/SystemFunctions.md index 88f5af55..0959f243 100644 --- a/docs/scarpet/language/SystemFunctions.md +++ b/docs/scarpet/language/SystemFunctions.md @@ -100,7 +100,9 @@ from other threads, just because the only possibility of a deadlock in this case bad code, not the internal world access behaviour. Some things tough like players or entities manipulation, can be effectively parallelized. -If the app is shutting down, creating new tasks via `task` +If the app is shutting down, creating new tasks via `task` will not succeed. Instead the new task will do nothing and return +`null`, so most threaded application should handle closing apps naturally. Keep in mind in case you rely on task return values, +that they will return `null` regardless of anything in these situations. ### `task(function, ... args)`, `task_thread(executor, function, ... args)` @@ -120,22 +122,22 @@ a = 3; task_thread('temp', _(outer(a), b) -> foo(a,b), 5) => Another example of running the same thing passing arguments using closure over anonymous function as well as passing a parameter. -### `sleep()` `sleep(timeout)`, `sleep(timeout, close_call, ... args)` +### `sleep()` `sleep(timeout)`, `sleep(timeout, close_expr)` Halts the execution of the thread (or the game itself, if run not as a part of a task) for `expr` milliseconds. It checks for interrupted execution, in that case exits the thread (or the entire program, if not run on a thread) in case the app -is being stopped/removed. If the closing call is specified, executes the following function when a shutdown signal is triggered. -If run on the main thread (not as a taks) the call may only be invocated when the entire game shuts down, so close call only -makes sense for threads. For programs use `__on_close()` handler. +is being stopped/removed. If the closing expression is specified, executes the expression when a shutdown signal is triggered. +If run on the main thread (i.e. not as a task) the close expression may only be invoked when the entire game shuts down, so close call only +makes sense for threads. For regular programs, use `__on_close()` handler. -Since `close_call` is executed after app shutdown is initiated, you won't be able to create new tasks in that block. Threads +Since `close_expr` is executed after app shutdown is initiated, you won't be able to create new tasks in that block. Threads should periodically call `sleep` to ensure all app tasks will finish when the app is closing or right after, but the app engine -will not forcefully remove your running tasks so the tasks themselves need to properly react to the closing request. +will not forcefully remove your running tasks, so the tasks themselves need to properly react to the closing request.
 sleep(50)  # wait for 50 milliseconds
-sleep(1000, _() -> print('Interrupted')) # waits for 1 second, outputs a message when thread is shut down.
+sleep(1000, print('Interrupted')) # waits for 1 second, outputs a message when thread is shut down.
 
### `task_count(executor?)` diff --git a/src/main/java/carpet/script/language/Threading.java b/src/main/java/carpet/script/language/Threading.java index 15383e7d..dcaa62ef 100644 --- a/src/main/java/carpet/script/language/Threading.java +++ b/src/main/java/carpet/script/language/Threading.java @@ -88,7 +88,7 @@ public class Threading } }); - expression.addLazyFunctionWithDelegation("sleep", -1, (c, t, expr, tok, lv) -> + expression.addLazyFunction("sleep", -1, (c, t, lv) -> { long time = lv.isEmpty()?0L:NumericValue.asNumber(lv.get(0).evalValue(c)).getLong(); boolean interrupted = false; @@ -107,9 +107,7 @@ public class Threading Value exceptionally = Value.NULL; if (lv.size() > 1) { - FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, lv, 1, false); - FunctionValue fun = functionArgument.function; - exceptionally = fun.callInContext(expr, c, t, fun.getExpression(), fun.getToken(), functionArgument.args).evalValue(c); + exceptionally = lv.get(1).evalValue(c); } throw new ExitStatement(exceptionally); } diff --git a/src/main/resources/assets/carpet/scripts/camera.sc b/src/main/resources/assets/carpet/scripts/camera.sc index 28b18fc3..0ba530e6 100644 --- a/src/main/resources/assets/carpet/scripts/camera.sc +++ b/src/main/resources/assets/carpet/scripts/camera.sc @@ -473,7 +473,7 @@ show() -> global_markers = __create_markers(); ); __show_path_tick(); - sleep(100, on_close); + sleep(100, call(on_close)); ); call(on_close); ));