Editor plugins

Polycarbonate, the editor, is built from plugins written in Dialect. Its Explorer, its Properties panel, its menus and its toolbar are all plugins that ship with it, and they use the same functions you do. A plugin you write can add a panel, a menu command, a toolbar button or a tool.

A first plugin

A plugin is a file whose first line is --!studio. The easiest way to write one is in the editor itself: File > New plugin makes a file from a template in the plugin folder and opens it in a tab above the 3D view, with completions for studio. and ui.. Ctrl+S saves it and runs it at once, so what it registered is replaced and a mistake shows in Output; the tab is starred while it has changes that were not run. Settings > Plugins > Edit opens an existing one the same way. A Script you wrote in the place becomes a plugin with right-click > Install as a plugin (studio.install_script()): its --!server line becomes --!studio and it is saved in the plugin folder. To add a file you have already written, use File > Add plugin... (or Settings > Plugins > Add plugin..., or drop the file on the editor): the editor checks the first line, copies the file into the plugin folder and runs it at once. The folder is plugins beside the editor's settings, in %LOCALAPPDATA%\Studs, and its files load in name order when the editor starts. Settings > Plugins lists them and can remove one.

--!studio
studio.command {
    id = "insert.three_parts",
    menu = "Insert",
    title = "Three parts",
    run = function()
        for _ = 1, 3 do
            studio.insert("Part")
        end
    end,
}

The command appears in the Insert menu. One click inserts three parts, and Undo takes back each insert on its own.

Panels

A panel is a rectangle the editor draws a frame and a tab around. Give it an icon, the name of a Silk icon (the file names in the icons folder without .png, such as "sitemap"). The editor calls your draw function whenever it redraws, and you fill the panel with widgets from ui. A widget is drawn where it is called and answers at once, so there is nothing to keep in sync:

--!studio
local count = 0

studio.panel {
    id = "counter",
    title = "Counter",
    dock = "right",
    draw = function()
        ui.label("Clicked " .. count .. " times")
        if ui.button("Click me") then
            count += 1
        end
    end,
}

Panels in the same dock share its height. The View menu shows and hides them (add a command that calls studio.toggle_panel("counter")).

The Explorer's search box and the Properties panel's collapsible sections are plugin code too: ui.text_edit(key, value, hint) is a field with a dimmed hint, studio.tree(open, first, count, filter) takes a filter, and ui.header(text, open, icon) draws a section bar, with an optional icon, that you flip when it is clicked. ui.icon(name) puts an icon in a row, ui.placeholder(icon, title, hint) fills an empty panel with a centered message, ui.drag(value, speed, min, max, label) shows an X, Y, Z, R, G or B label in its axis colour, and studio.summary() returns { count, class, name } for the selection. An icon name that does not exist raises an error. studio.toggle_theme() switches between the light and dark themes.

Rows of a long list should use ui.list, which asks you to draw only the rows in view, and studio.tree, which gives the Explorer's rows a window at a time. The built-in Explorer draws 5,000 parts this way without slowing down.

For plots, meters and anything else that is drawn rather than made of widgets, ui.canvas(height, draw) reserves a full-width rectangle. draw(c) is called with c.w and c.h (its size), c.x and c.y (the pointer from its top left, missing when the pointer is neither over it nor dragging out of it), and c.hovered, c.pressed (the left button went down on it this frame), c.down (held, after going down on it), c.released and c.double. Inside draw, ui.draw_line(ax, ay, bx, by, color, width), ui.draw_rect(x, y, w, h, color), ui.draw_circle(x, y, radius, color) and ui.draw_text(x, y, text, color) paint in points from the top left, clipped to the rectangle; outside a canvas they are an error. Properties' curve graph is written this way.

Commands and tools

A command has an id, a title, and a run function. Give it a menu to put it in a menu, a shortcut such as "Ctrl+D" or "Del", and toolbar = true with an icon (a Silk icon name) to make a toolbar button. An enabled function, returning a boolean, greys it out when it cannot run. Registering a command with an id that exists replaces it, which is how a plugin changes a built-in one.

A tool is registered with studio.tool { id, title, icon }, the icon a Silk icon name. One tool is active at a time; studio.active_tool() tells you which. Select, Move, Scale and Rotate use the same tool hooks below; a tool may have a shortcut, one character (the built-in tools use the number keys 1 to 4, which leaves the letters free for flying). Any other id is a tool of your own: give it on_click = function(click) ... end and it runs when the 3D view is clicked with that tool active. click holds x and y (the pointer), origin and dir (the ray through it, as lists of three numbers), hit (the id of the part under it, missing on empty ground), ctrl and shift. The view selects nothing for such a tool, so the function decides what a click means: studio.tool { id = "pick", title = "Pick", icon = "cursor", on_click = function(c) if c.hit then studio.select({ c.hit }) end end }. A tool may also have on_drag = function(drag) ... end, called for every step of a drag in the view: drag.phase is "begin", "move" (every frame) or "end", x and y are the pointer, start_x and start_y where the button went down, origin and dir the ray through the pointer, and hit the part under the start point. What the function changes between begin and end is one step of undo. A click that does not travel is an on_click, not a drag; a drag draws no selection box for such a tool.

Handles are a tool's own work. on_draw = function(pointer) runs every frame while the tool is active, with pointer.x and pointer.y (missing when the pointer is not over the view) and pointer.down. It places things with studio.view_project(x, y, z) (a world point to screen x and y, or nothing behind the camera) and studio.view_scale(x, y, z) (studs per screen point there, to keep a handle one size at any distance), and draws them with studio.view_line(ax, ay, bx, by, color, width) and studio.view_rect(x, y, w, h, color). on_press = function(press) is asked the moment the button goes down, with the same table on_drag gets: return true to take the press (the drag follows at once, with no click slop to travel) or false to let the view select as it does for Select. A tool with on_press that returns false gets the view's usual click and box selection, so it only has to handle its own handles. To edit parts it reads studio.selection_center() and studio.selection_parts() (a list of { id, x, y, z, sx, sy, sz, qx, qy, qz, qw }: where a part is, how big in studs, and how turned as a quaternion; descendants are included) and writes studio.set_parts(rows, label), where each row is an id with any of x y z, sx sy sz or qx qy qz qw (a group whole or left out). The edit joins one undo step, named label, while the button is held.

studio.focus() frames the selection in the 3D view, and the built-in View > Frame selection (F) calls it.

The 3D view

The middle of the editor is the 3D view, drawn by the game's renderer. With the pointer over it, W, A, S and D fly the camera and Q and E go down and up (Shift is faster); no mouse button needs to be held. Drag with the middle button to orbit, with Shift and the middle button to pan, scroll to zoom towards the pointer, or hold the right button to look around. F frames the selection, and so does double-clicking a part in the Explorer. Click a part to select it, Ctrl+click to add or remove one. The number keys 1 to 4 choose Select, Move, Scale and Rotate; with one of the last three, drag a coloured handle on the selection. A whole drag is one undo step, and holding Ctrl while dragging snaps to whole studs, half studs or 15 degrees.

Files, scripts and the output

The File menu is plugin commands too: studio.new_place(), studio.open(), studio.save() and studio.save_as() do what they say, asking first about unsaved changes. The Studz account is studio.cloud() (a table: state, name, can_publish, games, link, autosave and more) with sign_in, sign_out, refresh_games, open_game(id), publish(name, note) and set_autosave(on); the editor never sees the password, signing in happens in the browser. A place opened from a game is saved back to it every 15 seconds. Developer tools: studio.profile(target, on), profile_reset, profile_report(target), frame_stats() and run_console(target, line) work on "editor" (your plugins) or "server" (the game Play started). The Profiler panel and the console in Output are built on them. Panels that share a dock are tabs of one panel. studio.publish_model(name, description, tags, listed) publishes the selection to the Creator Store. The Creator Store is studio.store(kind, query, page) and studio.store_insert(kind, id); studio.import_model() imports a .glb or .gltf. The account's uploads are studio.library(kind) (decal, sound or mesh), studio.insert_asset(kind, id) and studio.upload_mesh(). Play is studio.play(), studio.stop_play() and studio.playing(): the place as it is now, unsaved changes included, runs in a game window against a local server, and what the server prints appears in Output. The editor also opens a place file dropped on its window.

The Place panel reads lighting and workspace rows from studio.place_settings(kind) and writes them with studio.set_place_setting(kind, name, value). Collision groups and their four plugin functions have been removed.

A Script, LocalScript or ModuleScript is a part holding source text. studio.insert("Script") makes one, and studio.open_script(id) opens it in a tab above the 3D view (double-clicking it in the Explorer does the same). The editor colours Luau as you type, keeps its own undo, and checks the source after a pause, marking a mistake on its line. What you type goes back into the place as one undo step when you pause, switch tab, save or run a command. Scripts and what plugins print appear in the Output panel under the 3D view (studio.output(first, count) reads it and studio.clear_output() empties it); a panel can sit there too, with dock = "bottom".

When something goes wrong

A mistake in a plugin never takes the editor down. A plugin that fails to load is reported and skipped. A panel whose draw raises an error shows the error in place of its contents and is not called again. A function that runs for more than a few milliseconds, or waits, is stopped. Messages from plugins, including anything you print, go to the editor's output.

The functions

Every function, with its arguments and what it returns, is on the plugin reference. Plugins use the same language as game scripts (signals, scopes and time, the world), but a plugin runs only in the editor and never in a game.

A panel can open a context menu: on the frame of a right click (ui.tree_row returns it as its fourth value) call studio.context_menu { "edit.duplicate", "-", "edit.delete" } with the ids of registered commands; "-" is a separator, and a command that is not enabled shows dimmed. Dragging works the same way: remember the row that ui.tree_row reported pressed, call ui.dropping(true) while the button is down to outline the row under the pointer, and when ui.mouse_released() is true move the part with studio.reparent(ids, parent). The built-in Explorer is written exactly like this.

Settings, themes, layout and shortcuts

Everything a person can change in Polycarbonate (Edit > Settings, Ctrl+,) is declared by plugins, so yours can add to it.

studio.setting { id = "mytool.speed", category = "My tool", title = "Speed", type = "number", default = 5, min = 1, max = 20 } declares a setting. type is number, bool, string, choice (with choices) or color (with rgb(...)). It appears in the Settings window under its category, is kept between runs, and is read with studio.get_setting("mytool.speed"). A number is held to its range; a value of the wrong kind is an error.

A theme is a dark or light base with the colors that differ:

--!studio
studio.theme { id = "sunset", title = "Sunset", base = "dark", colors = { accent = rgb(255, 120, 60), panel = rgb(40, 30, 36) } }

studio.theme_colors() lists every color name a theme can set. Whoever uses a theme can still change single colors in Settings > Appearance; those changes stay when they switch themes.

The layout is theirs too: drag a panel's tab to the left, right, bottom or the middle of the window to dock it there (the middle makes it a floating panel: carry it by its title, resize it by any edge or corner, and it is remembered), or right-click the tab, or use Settings > Layout (studio.move_panel(id, "left")). A panel declared with dock = "window" opens as a window over the editor, like the Settings. Shortcuts are changed in Settings > Keymap (studio.bind(command, "Ctrl+K")). Ctrl+Alt with + or - makes the whole interface larger or smaller.

Plugins in the plugin folder (Settings > Plugins shows where) are loaded at start; studio.reload_plugins() runs them again without closing the editor.

Edit > Cut, Copy and Paste (Ctrl+X, Ctrl+C, Ctrl+V) work on parts through studio.cut(), studio.copy(), studio.paste() and studio.can_paste(); a paste lands where the copy was, or inside the one Model or Folder that is selected. Right-click menus are lists of command ids given to studio.context_menu: the Explorer's rows and its empty space, the 3D view and the Output panel have one, and ui.right_clicked() tells a panel that the right button went down in its body. The script editor has its own Cut, Copy, Paste and Select all menu, and its keys use the system clipboard.

Working with the editor's own plugins

The editor's own panels, commands and tools (Explorer, Properties, Edit > Undo and the rest) are plugins too, but they are sealed: a plugin that registers a command, panel, tool, setting or theme with an id they own gets an error in Output, so the editor's logic cannot be replaced or broken. What a plugin can do instead:

  • Change how something looks. studio.customize { command = "edit.undo", title = "Take back", icon = "arrow_undo" } renames it or gives it another icon, moves it to another menu (menu = "Tools", or menu = "" to take it out of the menus), puts it on or off the toolbar, or hides it (hidden = true; its shortcut keeps working). { panel = id, title?, icon? } and { tool = id, title?, icon? } do the same for panels and tools.
  • React to what happens. studio.on("selection_changed", "my.id", function(ids) ... end). The events are selection_changed, document_changed (it fires every frame while something is dragged), command_run (called with the command id), play_started and play_stopped. The second argument names the handler, so running the plugin again replaces it instead of adding another; what a handler changes is not reported back to it.
  • Hang something on the Explorer. studio.explorer_decorator("my.id", function(row) return { suffix = "[locked]", icon = "lock" } end) shows a suffix after a row's name and/or another icon. Return nil to leave a row alone.
  • Add a section to Properties. studio.properties_section { id = "my.id", title = "Mine", draw = function() ui.label("hello") end } is drawn under the fields when something is selected. A mistake in it is shown inside the section and nowhere else.
  • Add to a right-click menu. studio.context_item("explorer", "my.command") (or "viewport").
  • Add to the status bar. studio.status_item("my.id", function() return "3 selected" end); a function that fails is taken out and reported.

A panel can start closed with visible = false in studio.panel; View > its name opens it, and the editor remembers it as open. The Library and the Profiler start that way.

Themes, fonts and icon packs

Settings > Appearance is the place to change the look, and everything on it is a plugin function too.

  • Colors. Every color of the interface and the code editor (studio.theme_colors, studio.set_theme_color).
  • Shape. studio.theme_metrics() lists radius (how round buttons, fields and panels are, in points), text_scale (all text, 0.8 to 1.4), spacing (the gaps and padding, 0.6 to 1.6), row_height (rows, tree rows and controls, 0.8 to 1.6) and bar_height (tab strips, title bar, menu bar, toolbar and status bar, 0.8 to 1.5); studio.set_theme_metric(name, value) changes one, and with no value puts it back to the theme's.
  • Fonts. studio.set_look("ui_font", "C:/fonts/MyFont.ttf") and "code_font" use a .ttf or .otf file; with no value the built-in fonts come back. A file that is missing or is not a font is reported and the built-in one stays. studio.pick_font(which) asks for the file.
  • Icon packs. An icon pack is a folder of .png files named like the icons (brick.png, folder.png...) inside an icon_packs folder beside the plugin folder. Pictures of any size are fitted to the 16 point icon cell, an icon the pack lacks stays as it is, and a pack can add names of its own, which plugins may then use (icon = "my_icon"). studio.icon_packs() lists the packs and studio.set_look("icon_pack", folder_name) chooses one (studio.icon_packs_folder() shows the folder).
  • A theme carries all of it. studio.theme { id = "mine", title = "Mine", base = "dark", colors = { accent = rgb(80, 200, 120) }, metrics = { radius = 8, text_scale = 1.1 }, icons = "neon", fonts = { ui = "C:/fonts/MyFont.ttf" } }. What the person changes in Settings goes on top of the theme and wins, as the colors always did.
  • Save what you have. studio.save_theme("My look", "My look") (the Save button on the page) writes the colors, shape, fonts and icon pack in force as a theme plugin in the plugin folder, runs it and chooses it; the changes on top are cleared because they are the theme now. The file is an ordinary plugin: edit its numbers and press Ctrl+S.

studio.reset_look() puts shape, fonts and icon pack back to the theme's.

Plugin switches

Settings > Plugins lists every plugin with a switch. Off takes away everything the plugin added (panels, commands, tools, settings, themes, event handlers, Explorer decorators, Properties sections, menu items and status bar texts) without restarting, and the choice is kept. On runs it again. The editor's own Explorer, Properties, Output, Settings and the built-in menus and tools are essential and have no switch; the Library, Profiler, account and built-in themes can be switched off. studio.plugins() returns { name, ok, enabled, builtin, essential } for each and studio.set_plugin_enabled(name, on) switches one. Running a plugin by hand (Ctrl+S in its tab, Add plugin) switches it on.

Layout

  • Two groups per dock. Each side dock can hold a second group of tabs under the first, and the bottom dock one beside it: studio.move_panel(id, "left2"), "right2" or "bottom2", or drop a tab on the lower half of a side zone (the right half of the bottom zone), or use the right-click menu on a tab, or Settings > Layout (the "2nd group" switch). When both groups of a dock are open they share it with a divider you can drag; the split is remembered. With only one group open it fills the dock.
  • Saved layouts. studio.save_layout(name) keeps where every panel is, which are open, the dock sizes, the dividers and the floating panels; studio.apply_layout(name) brings one back and studio.delete_layout(name), studio.layouts() manage them (Settings > Layout > Saved layouts).
  • Options. Settings > Layout can lock the layout (nothing can be dragged, resized or floated by accident) and make the bottom dock span the whole window, so the side docks stand on top of it.

Icon pack format

An icon pack is a folder inside the icon_packs folder (beside the plugin folder). Each icon is a PNG file named exactly like the icon: the name a plugin gives (icon = "brick") is the file name without .png (brick.png). Built-in names are lowercase with underscores (arrow_undo, page_copy, application_xp_terminal). The icons are designed on a 16 by 16 pixel grid with transparency; any other square size is fitted to that cell (a 32 or 64 pixel picture is scaled down smoothly, so make larger ones a multiple of 16), a picture that is not square is stretched, and at higher interface scales a 16 pixel icon is enlarged by whole numbers so it stays crisp. A file with a name no built-in icon has becomes a new icon plugins can use; an icon missing from the pack stays the built-in one. A pack reads at most 512 files of 256 KB each. Settings > Appearance > Icons > Export the built-in icons (studio.export_icons(name)) writes all of them, correctly named, into a new pack to edit.

Editing a game together

The owner of a game can invite other accounts to edit it with them (the Team tab of the game in the developer hub). Everyone opens the game from the account window and works in the same place: what one person changes shows up for the others within a second or two, parts they add and delete included. A drag arrives when it is released, not while it is held. Each person has their own undo, and it only undoes their own steps. Only the owner can publish the game or change its settings.

Clans can own games. A clan game is made from the clan's Games tab, or from the developer hub with the clan chosen as its owner. Its members work on it through their clan role: a role with Edit the clan's games opens it in Polycarbonate and edits it together with the rest, and a role with Manage the clan's games can also create more, publish, change settings and access, invite people from outside the clan, and manage its passes and badges. The clan's owner chooses which roles get which on the clan's Roles tab (the Owner and Admin roles have both). Access follows the role at once: a member who leaves the clan, or whose role loses the permission, can no longer open or save the game, and a game keeps belonging to the clan when the member who made it leaves. What the game earns goes to the clan's owner. If the clan is disbanded, its games become the owner's own. A clan can have ten games, and up to 64 people can be editing one at the same moment.

studio.cloud() tells a plugin about it:

  • link.owner is the account that owns the game (the clan's name for a clan game), link.shared is true when the person may edit it but not publish it or change its settings (it was shared with them, or their clan role only edits), and link.clan is the name of the clan it belongs to, or an empty string.
  • team.peers is the list of the other editors in the game, each { name, selection, editing }: selection is the list of part ids they have selected (studio.part_name(id) gives a name) and editing is the id of the script they have open in front of them, or 0. It is empty when nobody else is there.
  • team.ready is true once the editor has joined the game's team and team.reset is true when the editor was away so long that the game has to be opened again (save the place to a file first).

When two people change the same part at the same moment, the latest change wins, for the whole part. Scripts are handled more carefully, because typing in the same script cannot be merged: a script one person has open is read only for anyone who opens it after them, with a line above it saying who is editing, until they close it or switch to another tab. The text they write appears in the other person's tab as they go, and the other person's editor lets them select and copy but not change it. If two people open the same script within a moment of each other, the same one of them keeps it on both screens. Whatever a person had typed in a script before it was locked is saved first, so nothing is lost.

The lighting and workspace settings are shared as they change, like parts. The 3D view outlines the parts the others have selected, each person in their own colour and with their name, and the built-in account window lists who is editing, what they have selected and which script they have open. The editor's autosave and Publish save the place the way File > Save does, which includes the lighting and workspace settings; they used to leave them out.

What a plugin may do

Plugins run in a sandbox with no files, no network and no other programs: the only way out is the studio functions. The ones that reach the Studz account (cloud, sign_in, sign_out, publish, publish_model, open_game, set_autosave, library, store, store_insert, upload_mesh, import_model, insert_asset, refresh_games, refresh_library, open_site) or the system (new_place, open, save, save_as, quit, play, stop_play, run_console, add_plugin, new_plugin, edit_plugin, remove_plugin, install_script, set_plugin_enabled, set_plugin_trusted, reload_plugins, open_plugins_folder, icon_packs_folder, save_theme, export_icons, pick_font, set_look, reset_all, settings_path), or that change the editor's own commands and reach what other plugins registered (bind, reset_keymap, customize, explorer_decorators, property_sections), are checked each time they are called. The editor's own plugins may call them, and so may a plugin that was allowed; any other plugin gets an error that names the plugin and the function and says where to allow it. Everything else (the interface, the selection, inserting and changing parts, menus, panels, themes, layouts) needs no permission, and a plugin can still change the place.

  • A plugin you write in the editor (File > New plugin, Install as a plugin) is yours and is allowed.
  • A plugin added from a file or dropped on the window first shows what it asks for (the checked functions its source mentions) and you choose: add it and allow them, add it without them, or cancel. A plugin cannot hide a call from that list and then use it: the check is at the call, not in the list.
  • A plugin that was already in the plugin folder is not allowed until you switch on Allow account and system access for it in Settings > Plugins; the switch takes effect at once. A plugin cannot allow itself.
  • Plugin files are limited to 1 MB; an icon pack is one folder name (it cannot point elsewhere) and a font is a .ttf or .otf file. studio.open_site only opens pages of the Studz site.
  • A plugin that loops forever is stopped by the same time limit as game scripts (and the 64 MB memory limit applies); a core panel it made fail tries again when plugins change, so switching the plugin off brings the panel back.