pxt-calliope/olddocs/js/libraries.md

65 lines
2.2 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# Create and Use Libraries
2016-04-02 01:22:47 +02:00
Libraries are scripts with functions that you can use in other scripts.
2016-03-26 00:47:20 +01:00
### @parent js/language
Libraries are scripts with functions that you can use in other scripts. For example, `game` is a library of game-related functions that you can use in your scripts.
Benefits of using libraries:
* **reuse code** between scripts
* **collaborate** with other people on a project by combining scripts into a library
### Add a library
To add a library to a script:
2016-04-13 17:27:45 +02:00
1. Open a script in the [Touch Develop editor](/js/editor) and then click `script` (in the upper-right corner).
2016-03-26 00:47:20 +01:00
2. Click `+` **add new**.
3. Click `library`.
4. Choose the library you want to use or search for a library (such as `game`).
2016-04-13 17:27:45 +02:00
Once you've added a library to your script, you can use any of the library's non-private [functions](/js/function). Here's how:
2016-03-26 00:47:20 +01:00
2016-04-13 17:27:45 +02:00
* on the [Code Keyboard](/js/editor) click the button with the library's name (for example, `micro:bit` and `micro:bit game` are library buttons). The functions in the library have a button on the Code Keyboard.
2016-03-26 00:47:20 +01:00
### Create a library
Creating and publishing a script as a library is easy. Here's how:
2016-04-13 17:27:45 +02:00
1. Open a script in the [Touch Develop editor](/js/editor), and then click `script`.
2016-03-26 00:47:20 +01:00
2. Click the script name to open the script properties.
3. Mark the `this script is a library` check box.
4. Click `script`.
5. Click `publish`.
Once a script is marked as a _library_ and published, it's immediately available to other people.
### Visibility
The following library items are not accessible by other scripts:
* data (global variables)
* functions marked as `private`
2016-04-13 17:27:45 +02:00
If you want to access global library variables from other scripts, you need to create [functions](/js/function) that pass values in and out of the library script.
2016-03-26 00:47:20 +01:00
### Library and function documentation
2016-04-13 17:27:45 +02:00
Be sure to document the purpose of your functions and libraries. Add a [comment](/js/comment) at the beginning of a library to describe the purpose of the library.
2016-03-26 00:47:20 +01:00
2016-04-13 17:27:45 +02:00
Use [comments](/js/comment) at the beginning of your [functions](/js/function) to describe the function's purpose. Comment text shows in the help area of the Code Keyboard when you insert the function.
2016-03-26 00:47:20 +01:00
### See also
2016-04-13 17:27:45 +02:00
[functions](/js/function), [Touch Develop Documentation](/js/contents)
2016-03-26 00:47:20 +01:00