diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index 86d62dc0..9309a5ac 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -1675,6 +1675,8 @@ public class CarpetExpression *
Removes tag from entity.
*modify(e, 'talk')Make noises.
+ *modify(e, 'ai', boolean)If called with false value, it will disable AI in the mob. true will enable it again.
modify(e, 'home', null), modify(e, 'home', block, distance?), modify(e, 'home', x, y, z, distance?)Sets AI to stay around the home position, within distance blocks from it. distance
* defaults to 16 blocks. null removes it. May not work fully with mobs that have this AI built in, like
@@ -2443,14 +2445,20 @@ public class CarpetExpression
*
Renders a cuboid of particles between point pos to pos2 with supplied density.
create_marker(text, pos, rotation?, block?)Spawns a (permanent) marker entity with text or block at position. Returns that entity for further manipulations
+ *Spawns a (permanent) marker entity with text or block at position. Returns that entity for further manipulations. + * Unloading the app that spawned them will cause all the markers from the loaded portion of the world to be removed. + * Also - if the game loads that marker in the future and the app is not loaded, it will be removed as well.
*remove_all_markers()Removes all scarpet markers from the loaded portion of the world in case you didn't want to do the cleanup
+ *Removes all scarpet markers from the loaded portion of the world created by this app, in case you didn't want + * to do the proper cleanup
*nbt(expr)Treats the argument as a nbt serializable string and returns its nbt value.
* In case nbt is not in a correct nbt compound tag format, it will return null value.
Consult section about container operations in Expression to learn about possible operations on nbt values.
escape_nbt(expr)Excapes all the special characters in the string or nbt tag and returns a string that can be stored in nbt directly as + * a string value.
*print(expr)Displays the result of the expression to the chat. Overrides default scarpet behaviour of
* sending everyting to stderr.
invoke family of commands provide convenient way to invoke stored procedures (i.e. functions
* that has been defined previously by any running script. To view current stored procedure set,
- * run /script globals, to define a new stored procedure, just run a /script run function(a,b) -> ( ... )
+ * run /script globals(or /script globals all to display all functions even hidden ones),
+ * to define a new stored procedure, just run a /script run function(a,b) -> ( ... )
* command with your procedure once, and to forget a procedure, use undef function:
* /script run undef('function')
To effectively write programs that have more than one line, a programmer needs way to specify a sequence of
* commands that execute one after another. In scarpet this can be achieved with ;. Its an operator,
* and by separating statements with semicolons. And since whitespaces and $ signs are all treats as
- * whitespaces, how you layout your code doesn't matter, as long as it is readable to everyone involved. The
- * usefulness of preceding all the lines of the script with $ is explained in the preamble
* expr;
* expr;
@@ -848,22 +847,22 @@ public class Expression implements Cloneable
* expr
*
* Notice that the last expression is not followed by a semicolon. Since instruction separation is functional
- * in scarpet, and not barely an instruction delimiter,
- * terminating the code with a dangling operator wouldn't be valid.
scarpet, and not barely an instruction delimiter, terminating the code with a dangling operator
+ * wouldn't be valid. Having said that, since many programming languages don't care about the number of op terminators
+ * programmers use, carpet preprocessor will remove all unnecessary semicolons from scripts when compiled.
* In general expr; expr; expr; expr is equivalent to
- * (((expr ; expr) ; expr) ; expr). In case the programmer forgets that it should be warned with a
- * helpful error at compile time.
(((expr ; expr) ; expr) ; expr).
* Result of the evaluated expression is the same as the result of the second expression, but first expression is * also evaluated for sideeffects
*
* expr1 ; expr2 => expr2 // with expr1 as a sideeffect
*
+ * All defined functions are compiled, stored persistently, and available globally -
* accessible to all other scripts. Functions can only be undefined via call to undef('fun'), which
* would erase global entry for function fun. Since all variables have local scope inside each function,
* one way to share large objects is via global variables
*
Any variable that is used with a name that starts with 'global_' will be stored and accessible globally,
* not, inside current scope. It will also persist across scripts, so if a procedure needs to use its own construct, it needs to
* define it, or initialize it explicitly, or undefine it via undef
expr, so all non-global variables are not
* visible from the caller scope. All parameters are passed by value to the new scope, including lists and other
* containers, however their copy will be shallow.
+ * The function returns its name as a string, which means it can be used to call it later with the call function
Using _ as the function name creates anonymous function, so each time _ function is defined,
+ * it will be given a unique name, which you can pass somewhere else to get this function called.
* a(lst) -> lst+=1; list = l(1,2,3); a(list); a(list); list // => [1,2,3]
*
@@ -904,6 +906,23 @@ public class Expression implements Cloneable
*
* Ability to combine more statements into one expression, with functions, passing parameters, and global and outer * scoping allow to organize even larger scripts
+ *call(function, args.....)calls a user defined function with specified arguments. It is equivalent to calling function(args...)
+ * directly except you can use it with function name instead. This means you can pass functions to other user defined
+ * functions as arguments and call them with call internally. And since function definitions return the
+ * defined function name, they can be defined in place
Little technical note: the use of _ in expression passed to built in functions is much more efficient due to
+ * not creating new call stacks for each invoked function, but anonymous functions is the only mechanism available
+ * for programmers with their own lambda arguments
+ * my_map(list, function) -> map(list, call(function, _)); + * my_map(l(1,2,3), _(x) -> x*x); // => [1,4,9] + * + * profile_expr(my_map(l(1,2,3), _(x) -> x*x)); // => ~32000 + * sq(x) -> x*x; profile_expr(my_map(l(1,2,3),'sq')); // => ~38000 + * profile_expr(map(l(1,2,3), _*_)); // => ~80000 + * + **
return(expr?)Sometimes its convenient to break the organized control flow, or it is not practical to pass @@ -2645,6 +2664,14 @@ public class Expression implements Cloneable * title('aBc') => 'Abc' * * + *
replace(string, regex, repl?); replace_first(string, regex, repl?)Replaces all, or first occurence of a regular expression in the string with repl expression, or
+ * nothing, if not specified
+ * replace('abbccddebfg','b+','z') // => azccddezfg
+ * replace_first('abbccddebfg','b+','z') // => azccddebfg
+ *
+ *
* length(expr)Returns length of the expression, the length of the string, * the length of the integer part of the number, or length of the list