text stringlengths 0 897 |
|---|
``` |
You'll find `chars.ms` in this directory. Use `load "chars"` and `edit` to view this file. You'll see that it looks like an ordinary MiniScript program, with some helpful comments and (in this case) a bunch of assignments to things like `left`, `upArrow`, `mu`, `emptyBox`, and `heart`. That last one is the value you... |
So here's how `import` works: the name file is found, and then run in a special way, such that any global variables created by that file become entries in a map with the name of the module. That assignment to `heart` in `chars.ms` becomes accessible as `chars.heart`, and so on for all the other values assigned. Explo... |
```terminal |
]chars.dieFace[3] |
⚂ |
]chars.bullet |
• |
``` |
D>Always read an import file to learn how to use it! Unlike the MiniScript language or the built-in Mini Micro classes, which are documented in places like the MiniScript wiki, most import modules are documented only within their own source. |
{i:"`listUtil` module;Mini Micro, `listUtil` module"} |
Let's try another one. There's a library module called `listUtil` ("util" here is short for "utilities"). As always with import modules, begin by reading the source: |
{i:"`/sys/lib`,`listUtil`"} |
```terminal |
]load "listUtil" |
356 lines loaded from /sys/lib/listUtil.ms |
]edit |
``` |
This one looks a little different, because most of the assignments in this file are adding to the built-in `list` type. For example, near the top of the file you should find something like this: |
```miniscript |
// contains: return true if this list contains the given element. |
list.contains = function(item) |
return self.indexOf(item) != null |
end function |
``` |
This assigns a function to `list.contains`. The built-in list type does not have a `contains` method, but after importing this module, it does! Try it out: |
```terminal |
]import "listUtil" |
]a = [2, 4, 6, 8] |
]a.contains 2 |
1 |
]a.contains 3 |
0 |
``` |
Edit `listUtils` again to see what other goodies have been added. Some of them are a bit complex, and it's OK if you don't understand everything in here just yet. But many of them are easy to understand, and could be quite useful: |
```terminal |
]a.min |
2 |
]a.max |
8 |
]a.any |
6 |
``` |
Of course when you try it, you may get a different value for `a.any`, since the whole point of the new `list.any` method is to return a random element! |
One particularly useful function added by `listUtil` is `list.removeVal`. You will of course find this in the `listUtil` source code, and it begins like this: |
```miniscript |
// removeVal: remove the first (or all) occurrences of a |
// given value. |
list.removeVal = function(val, removeAll=false) |
``` |
This is handy because the standard `list.remove` method takes an element *index* to remove, not a value. You would pass `list.remove` an argument of 0 to remove the first element, 1 to remove the second element, and so on. But in many cases, what you have is a particular *value* you want to remove, and you don't know... |
```terminal |
]a.removeVal 6 |
]a |
[2, 4, 8] |
``` |
Of course all this works not only on the REPL command line, but also in your own programs. Now that you know about it, you'll notice `import` statements near the top of many of the demo programs. You should take some time to explore what these `/sys/lib` modules have to offer. Here's a quick summary of the most impo... |
{i:"import modules;`/sys/lib`"} |
{caption:"Import modules found in `/sys/lib`."} |
|chars|defines names for various special characters| |
|grfon|functions to save and load data in GRFON format| |
|json|functions to save and load data in JSON format| |
|listUtil|adds additional methods to the `list` type| |
|mapUtil|adds additional methods to the `map` type| |
|mathUtil|additional math & geometry functions| |
|pathUtil|functions for working with 2D paths| |
|sounds|some predefined synthesized sounds| |
|spriteControllers|extra functionality for sprites| |
|stringUtil|handy string constants, and adds methods to the `string` type| |
|textUtil|functions related to drawing text| |
|tileUtil|functions related to tile displays| |
|tsv|functions to read tab-separated data| |
|turtle|defines a `Turtle` class, for doing turtle graphics| |
Finally, it should be noted that an import module is also a perfectly valid MiniScript program in its own right. You can `load` an import module, and then `run` it like any other program. When you do, it will often run some testing code or otherwise demonstrate its functionality. This works via a special trick. Whe... |
```miniscript |
if globals == locals then runUnitTests |
``` |
This says, if we're being run as a regular program (rather than imported), then call `runUnitTests`. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.