Porymap is extensible via scripting capabilities. This allows the user to write custom JavaScript (technically, ECMAScript) files to support enhanced workflows, without having to fork Porymap itself. While the possibilities are endless, some useful examples of scripting might be:
- Toggle Day/Night Palettes
- Custom Map Painting Brushes
- Detect Tile Errors
- Show Diagonistic Information
- Procedurally Generated Maps
- Randomize Grass Patterns
Writing a Custom Script
-----------------------
Let's write a custom script that will randomize grass patterns when the user is editing the map. This is useful, since it's cumbersome to manually add randomness to grass patches. With the custom script, it will happen automatically. Whenever the user paints a grass tile onto the map, the script will overwrite the tile with a random grass tile instead.
First, create a new script file called ``my_script.js``--place it in the project directory (e.g. ``pokefirered/``).
Next, open the Porymap project config file, ``porymap.project.cfg``, in the project directory. Add the script file to the ``custom_scripts`` configuration value. Multiple script files can be loaded by separating the filepaths with a comma.
..code-block::
custom_scripts=my_script.js
Now that Porymap is configured to load the script file, let's write the actual code that will power the grass-randomizer. Scripts have access to several "callbacks" for events that occur while Porymap is running. This means our script can define functions for each of these callbacks. We're interested in the ``onBlockChanged()`` callback, since we want our script to take action whenever a user paints a block on the map.
..code-block:: js
// Porymap callback when a block is painted.
export function onBlockChanged(x, y, prevBlock, newBlock) {
// Grass-randomizing logic goes here.
}
It's very **important** to remember to ``export`` the callback functions in the script. Otherwise, Porymap will not be able to execute them.
In addition to the callbacks, Porymap also supports a scripting API so that the script can interact with Porymap in interesting ways. For example, a script can change a block or add overlay text on the map. Since we want to paint random grass tiles, we'll be using the ``map.setMetatileId()`` function. Let's fill in the rest of the grass-randomizing code.
export function onBlockChanged(x, y, prevBlock, newBlock) {
// Check if the user is painting a grass tile.
if (grassTiles.indexOf(newBlock.metatileId) != -1) {
// Choose a random grass tile and paint it on the map.
const i = randInt(0, grassTiles.length);
map.setMetatileId(x, y, grassTiles[i]);
}
}
Let's test the script out by re-launching Porymap. If we try to paint grass on the map, we should see our script inserting a nice randomized grass pattern.
The grass-randomizer script above happens implicitly when the user paints on the map. However, other times we probably want to call the custom script on demand. One of the API functions Porymap provides is the ability to trigger scripting functions from the ``Tools`` menu, or a keyboard shortcut. To do this, we will usually want to register the action when the project loads. Here is an example script where some custom actions are registered.
Called when the selected tab in the main tab bar is changed. Tabs are indexed from left to right, starting at 0 (``0``: Map, ``1``: Events, ``2``: Header, ``3``: Connections, ``4``: Wild Pokemon).
Called when the selected tab in the map view tab bar is changed. Tabs are indexed from left to right, starting at 0 (``0``: Metatiles, ``1``: Collision).
:param boolean forceRedraw:Force the map view to refresh. Defaults to ``true``. Redrawing the map view is expensive, so set to ``false`` when making many consecutive map edits, and then redraw the map once using ``map.redraw()``.
:param boolean commitChanges:Commit the changes to the map's edit/undo history. Defaults to ``true``. When making many related map edits, it can be useful to set this to ``false``, and then commit all of them together with ``map.commit()``.
:param boolean forceRedraw:Force the map view to refresh. Defaults to ``true``. Redrawing the map view is expensive, so set to ``false`` when making many consecutive map edits, and then redraw the map once using ``map.redraw()``.
:param boolean commitChanges:Commit the changes to the map's edit/undo history. Defaults to ``true``. When making many related map edits, it can be useful to set this to ``false``, and then commit all of them together with ``map.commit()``.
:param boolean forceRedraw:Force the map view to refresh. Defaults to ``true``. Redrawing the map view is expensive, so set to ``false`` when making many consecutive map edits, and then redraw the map once using ``map.redraw()``.
:param boolean commitChanges:Commit the changes to the map's edit/undo history. Defaults to ``true``. When making many related map edits, it can be useful to set this to ``false``, and then commit all of them together with ``map.commit()``.
:param boolean forceRedraw:Force the map view to refresh. Defaults to ``true``. Redrawing the map view is expensive, so set to ``false`` when making many consecutive map edits, and then redraw the map once using ``map.redraw()``.
:param boolean commitChanges:Commit the changes to the map's edit/undo history. Defaults to ``true``. When making many related map edits, it can be useful to set this to ``false``, and then commit all of them together with ``map.commit()``.
:param boolean forceRedraw:Force the map view to refresh. Defaults to ``true``. Redrawing the map view is expensive, so set to ``false`` when making many consecutive map edits, and then redraw the map once using ``map.redraw()``.
:param boolean commitChanges:Commit the changes to the map's edit/undo history. Defaults to ``true``. When making many related map edits, it can be useful to set this to ``false``, and then commit all of them together with ``map.commit()``.
:param boolean forceRedraw:Force the map view to refresh. Defaults to ``true``. Redrawing the map view is expensive, so set to ``false`` when making many consecutive map edits, and then redraw the map once using ``map.redraw()``.
:param boolean commitChanges:Commit the changes to the map's edit/undo history. Defaults to ``true``. When making many related map edits, it can be useful to set this to ``false``, and then commit all of them together with ``map.commit()``.
:param boolean forceRedraw:Force the map view to refresh. Defaults to ``true``. Redrawing the map view is expensive, so set to ``false`` when making many consecutive map edits, and then redraw the map once using ``map.redraw()``.
:param boolean commitChanges:Commit the changes to the map's edit/undo history. Defaults to ``true``. When making many related map edits, it can be useful to set this to ``false``, and then commit all of them together with ``map.commit()``.
:param boolean forceRedraw:Force the map view to refresh. Defaults to ``true``. Redrawing the map view is expensive, so set to ``false`` when making many consecutive map edits, and then redraw the map once using ``map.redraw()``.
:param boolean commitChanges:Commit the changes to the map's edit/undo history. Defaults to ``true``. When making many related map edits, it can be useful to set this to ``false``, and then commit all of them together with ``map.commit()``.
:param boolean forceRedraw:Force the map view to refresh. Defaults to ``true``. Redrawing the map view is expensive, so set to ``false`` when making many consecutive map edits, and then redraw the map once using ``map.redraw()``.
:param boolean commitChanges:Commit the changes to the map's edit/undo history. Defaults to ``true``. When making many related map edits, it can be useful to set this to ``false``, and then commit all of them together with ``map.commit()``.
:param boolean forceRedraw:Force the map view to refresh. Defaults to ``true``. Redrawing the map view is expensive, so set to ``false`` when making many consecutive map edits, and then redraw the map once using ``map.redraw()``.
:param boolean commitChanges:Commit the changes to the map's edit/undo history. Defaults to ``true``. When making many related map edits, it can be useful to set this to ``false``, and then commit all of them together with ``map.commit()``.
Redraws the entire map area. Useful when delaying map redraws using ``forceRedraw = false`` in certain map editing functions.
..js:function:: map.commit()
Commits any uncommitted changes to the map's edit/undo history. Useful when delaying commits using ``commitChanges = false`` in certain map editing functions.
The following functions are related to reading/writing the map's header properties.
..js:function:: map.getSong()
Gets the name of the background song for the currently-opened map.
:returns string:the name of the song
..js:function:: map.setSong(song)
Sets the name of the background song for the currently-opened map. The song name must be one of the names in the "Song" dropdown menu on the Header tab.
:param string song:the name of the song
..js:function:: map.getLocation()
Gets the name of the region map location for the currently-opened map.
:returns string:the name of the location
..js:function:: map.setLocation(location)
Sets the name of the region map location for the currently-opened map. The location name must be one of the names in the "Location" dropdown menu on the Header tab.
:param string location:the name of the location
..js:function:: map.getRequiresFlash()
Gets whether flash would be required in-game for the currently-opened map.
:returns boolean:whether flash is required
..js:function:: map.setRequiresFlash(require)
Sets whether flash would be required in-game for the currently-opened map.
:param boolean require:whether flash should be required
..js:function:: map.getWeather()
Gets the name of the weather for the currently-opened map.
:returns string:the name of the weather
..js:function:: map.setWeather(weather)
Sets the name of the weather for the currently-opened map. The weather name must be one of the names in the "Weather" dropdown menu on the Header tab.
:param string weather:the name of the weather
..js:function:: map.getType()
Gets the name of the map type for the currently-opened map.
:returns string:the name of the map type
..js:function:: map.setType(type)
Sets the name of the map type for the currently-opened map. The map type name must be one of the names in the "Type" dropdown menu on the Header tab.
:param string type:the name of the map type
..js:function:: map.getBattleScene()
Gets the name of the battle scene for the currently-opened map.
:returns string:the name of the battle scene
..js:function:: map.setBattleScene(battleScene)
Sets the name of the battle scene for the currently-opened map. The battle scene name must be one of the names in the "Battle scene" dropdown menu on the Header tab.
:param string battleScene:the name of the battle scene
..js:function:: map.getShowLocationName()
Gets whether the location name will appear in-game for the currently-opened map.
:returns boolean:whether the location name will be shown
..js:function:: map.setShowLocationName(show)
Sets whether the location name should appear in-game for the currently-opened map.
:param boolean show:whether the location name should be shown
..js:function:: map.getAllowRunning()
Gets whether running is allowed in-game for the currently-opened map.
:returns boolean:whether running is allowed
..js:function:: map.setAllowRunning(allow)
Sets whether running should be allowed in-game for the currently-opened map.
:param boolean allow:whether running should be allowed
..js:function:: map.getAllowBiking()
Gets whether biking is allowed in-game for the currently-opened map.
:returns boolean:whether biking is allowed
..js:function:: map.setAllowBiking(allow)
Sets whether biking should be allowed in-game for the currently-opened map.
:param boolean allow:whether biking should be allowed
..js:function:: map.getAllowEscaping()
Gets whether escaping (using Escape Rope or Dig) is allowed in-game for the currently-opened map.
:returns boolean:whether escaping is allowed
..js:function:: map.setAllowEscaping(allow)
Sets whether escaping (using Escape Rope or Dig) should be allowed in-game for the currently-opened map.
:param boolean allow:whether escaping should be allowed
..js:function:: map.getFloorNumber()
Gets the floor number for the currently-opened map.
:returns number:the floor number
..js:function:: map.setFloorNumber(floorNumber)
Sets the floor number for the currently-opened map. Floor numbers can be any number between -128 and 127 inclusive.
The following functions are related to an overlay that is drawn on top of the map area. Text, images, and shapes can be drawn using these functions. Items can be drawn and manipulated on separate layers by specifiying a layer id. Items on higher layer ids will be drawn above those on lower layers. If no layer is specified they will be added to the default layer ``0``. The visibility and position of each layer can be changed; by default all layers are visible, and their position is ``0,0``.
:param number layer:the layer id. Defaults to ``0``
:param boolean useCache:whether the image should be saved/loaded using the cache. Defaults to ``true``. Reading images from a file is slow. Setting ``useCache`` to ``true`` will save the image to memory so that the next time the filepath is encountered the image can be loaded from memory rather than the file.
Creates an image item on the specified overlay layer. This differs from ``map.addImage`` by allowing the new image to be a transformation of the image file.
:param number x:the x pixel coordinate of the image's top-left corner (relative to the layer's position)
:param number y:the y pixel coordinate of the image's top-left corner (relative to the layer's position)
:param string filepath:the image's filepath
:param number width:the image width. If ``-1``, use the full width of the original image. Defaults to ``-1``
:param number height:the image height. If ``-1``, use the full height of the original image. Defaults to ``-1``
:param number offset:the pixel offset into the original image where data should be read from. Defaults to ``0``
:param boolean xflip:whether the image should be a horizontal flip of the original image. Defaults to ``false``
:param boolean yflip:whether the image should be a vertical flip of the original image. Defaults to ``false``
:param number paletteId:the id of which currently loaded tileset palette to use for the image. If ``-1``, use the original image's palette. Defaults to ``-1``
:param boolean setTransparency:whether the color at index 0 should be overwritten with transparent pixels. Defaults to ``false``
:param number layer:the layer id. Defaults to ``0``
:param boolean useCache:whether the image should be saved/loaded using the cache. Defaults to ``true``. Reading images from a file is slow. Setting ``useCache`` to ``true`` will save the image to memory so that the next time the filepath is encountered the image can be loaded from memory rather than the file.
Creates an image of a tile on the specified overlay layer.
:param number x:the x pixel coordinate of the image's top-left corner (relative to the layer's position)
:param number y:the y pixel coordinate of the image's top-left corner (relative to the layer's position)
:param number tileId:tile value for the image
:param boolean xflip:whether the tile image is flipped horizontally
:param boolean yflip:whether the tile image is flipped vertically
:param number palette:palette number for the tile image
:param boolean setTransparency:whether the color at index 0 should be overwritten with transparent pixels. Defaults to ``false``
:param number layer:the layer id. Defaults to ``0``
..js:function:: map.addTileImage(x, y, tile, setTransparency = false, layer = 0)
Creates an image of a tile on the specified overlay layer. This is an overloaded function that takes a single tile as a JavaScript object instead of each of the tile's properties individually.
:param number x:the x pixel coordinate of the image's top-left corner (relative to the layer's position)
:param number y:the y pixel coordinate of the image's top-left corner (relative to the layer's position)
:param {tileId,xflip,yflip,palette} tile:the tile to create an image of
:param boolean setTransparency:whether the color at index 0 should be overwritten with transparent pixels. Defaults to ``false``
:param number layer:the layer id. Defaults to ``0``
..js:function:: map.addMetatileImage(x, y, metatileId, setTransparency = false, layer = 0)
Creates an image of a metatile on the specified overlay layer.
:param number x:the x pixel coordinate of the image's top-left corner (relative to the layer's position)
:param number y:the y pixel coordinate of the image's top-left corner (relative to the layer's position)
:param number metatileId:id of the metatile to create an image of
:param boolean setTransparency:whether the color at index 0 should be overwritten with transparent pixels. Defaults to ``false``
:param number layer:the layer id. Defaults to ``0``
The following functions are related to tilesets and how they are rendered. The functions with "preview" in their name operate on a "fake" version of the palette colors. This means that changing these "preview" colors won't affect the actual tileset colors in the project. A good use of the "preview" palettes would be Day/Night tints, for example.
Sets a palette in the primary tileset of the currently-opened map. This will NOT affect the true underlying colors--it only displays these colors in the map-editing area of Porymap.
:param number paletteIndex:the palette index
:param array colors:array of colors. Each color is a 3-element RGB array
Sets all of the palettes in the primary tileset of the currently-opened map. This will NOT affect the true underlying colors--it only displays these colors in the map-editing area of Porymap.
:param array palettes:array of arrays of colors. Each color is a 3-element RGB array
Sets a palette in the secondary tileset of the currently-opened map. This will NOT affect the true underlying colors--it only displays these colors in the map-editing area of Porymap.
:param number paletteIndex:the palette index
:param array colors:array of colors. Each color is a 3-element RGB array
Sets all of the palettes in the secondary tileset of the currently-opened map. This will NOT affect the true underlying colors--it only displays these colors in the map-editing area of Porymap.
:param array palettes:array of arrays of colors. Each color is a 3-element RGB array
Sets all of the palettes in the secondary tileset of the currently-opened map. This will permanently affect the palettes and save the palettes to disk.
:param array palettes:array of arrays of colors. Each color is a 3-element RGB array
:param boolean xflip:whether the new tile is flipped horizontally
:param boolean yflip:whether the new tile is flipped vertically
:param number palette:new tile's palette number
:param boolean forceRedraw:Force the map view to refresh. Defaults to ``true``. Redrawing the map view is expensive, so set to ``false`` when making many consecutive map edits, and then redraw the map once using ``map.redraw()``.
Sets the tile at the specified index of the metatile. This is an overloaded function that takes a single tile as a JavaScript object instead of each of the tile's properties individually.
:param boolean forceRedraw:Force the map view to refresh. Defaults to ``true``. Redrawing the map view is expensive, so set to ``false`` when making many consecutive map edits, and then redraw the map once using ``map.redraw()``.
:param boolean xflip:whether the new tiles are flipped horizontally
:param boolean yflip:whether the new tiles are flipped vertically
:param number palette:new tiles' palette number
:param number tileStart:index of the first tile to set. Defaults to ``0`` (the first tile)
:param number tileEnd:index of the last tile to set. Defaults to ``-1`` (the last tile)
:param boolean forceRedraw:Force the map view to refresh. Defaults to ``true``. Redrawing the map view is expensive, so set to ``false`` when making many consecutive map edits, and then redraw the map once using ``map.redraw()``.
Sets the tiles in the specified range of the metatile. This is an overloaded function that takes an array of tiles as JavaScript objects instead of each of the tile properties individually.
:param array tiles:array of tiles to set. Each tile is an object of the form ``{tileId, xflip, yflip, palette}``. If the array does not have sufficient objects to set all the tiles in the specified range then the remaining tiles will be set with all default values.
:param number tileStart:index of the first tile to set. Defaults to ``0`` (the first tile)
:param number tileEnd:index of the last tile to set. Defaults to ``-1`` (the last tile)
:param boolean forceRedraw:Force the map view to refresh. Defaults to ``true``. Redrawing the map view is expensive, so set to ``false`` when making many consecutive map edits, and then redraw the map once using ``map.redraw()``.
Gets the pixel data for the specified tile. The pixel data is an array of numbers indicating which palette color each pixel uses. Tiles are 8x8, so the pixel array will be 64 elements long.
:returns string:``"pokeruby"``, ``"pokefirered"``, or ``"pokeemerald"``
..js:function:: map.getCustomScripts()
Gets the list of paths to custom scripts.
:returns array:string array of custom scripts paths
..js:function:: map.getMainTab()
Gets the index of the currently selected main tab. Tabs are indexed from left to right, starting at 0 (``0``: Map, ``1``: Events, ``2``: Header, ``3``: Connections, ``4``: Wild Pokemon).
:returns number:current main tab index
..js:function:: map.setMainTab(tab)
Sets the currently selected main tab. Tabs are indexed from left to right, starting at 0 (``0``: Map, ``1``: Events, ``2``: Header, ``3``: Connections, ``4``: Wild Pokemon).
:param number tab:index of the tab to select
..js:function:: map.getMapViewTab()
Gets the index of the currently selected map view tab. Tabs are indexed from left to right, starting at 0 (``0``: Metatiles, ``1``: Collision).
:returns number:current map view tab index
..js:function:: map.setMapViewTab(tab)
Sets the currently selected map view tab. Tabs are indexed from left to right, starting at 0 (``0``: Metatiles, ``1``: Collision).
Registers a JavaScript function to an action that can be manually triggered in Porymap's ``Tools`` menu. Optionally, a keyboard shortcut (e.g. ``"Ctrl+P"``) can also be specified, assuming it doesn't collide with any existing shortcuts used by Porymap. The function specified by ``functionName`` must have the ``export`` keyword.
:param string functionName:name of the JavaScript function
:param string actionName:name of the action that will be displayed in the ``Tools`` menu
:param string shortcut:optional keyboard shortcut
..js:function:: map.setTimeout(func, delayMs)
This behaves essentially the same as JavaScript's ``setTimeout()`` that is used in web browsers or NodeJS. The ``func`` argument is a JavaScript function (NOT the name of a function) which will be executed after a delay. This is useful for creating animations or refreshing the overlay at constant intervals.
:param function func:a JavaScript function that will be executed later
:param number delayMs:the number of milliseconds to wait before executing ``func``