The Code Editor is an online real-time collaborative editor that allows you to edit your Script assets and also all the other text based assets like JSON, HTML, CSS etc.
Opening The Code Editor
You can open the Code Editor from inside the PlayCanvas Editor by pressing Ctrl + I. You can also click on this toolbar icon:
Alternatively you can double click on a text-based asset like a Script to open the Code Editor focused on that asset.
The menu contains all the different tools and options available along with their keyboard shortcuts.
Here you can see all the assets that can be edited by the Code Editor. Click on an asset to select it. Selecting an asset will open it in the Text Editor for editing.
Single clicking an asset will open it in Preview Mode. This means that if you open another asset it will be shown in the same Tab. If you want to open an asset permanently double click on the asset or start editing it.
By right clicking on an asset you will see the context menu with various asset-related options. You can also drag and drop assets into folders.
Each open asset has a corresponding tab in the tab view. You can rearrange tabs by dragging and dropping them and you can also close tabs by clicking on the X button that appears when you put your mouse cursor on a tab. If you right click on a tab you will see the tab context menu with various tab-related options.
Here you can actually edit the contents of your assets. The editor uses a modified version of CodeMirror.
In addition, the editor supports auto-complete. When you type a letter or hit Ctrl+Space you will see a list of auto-complete suggestions.
The Editor supports real-time collaborative editing by all users with permission to edit the code. You will see other user's avatars here.
Here you can see various helpful messages while you are interacting with the Code Editor.
Collaboration, Saving & Reverting
The code editor is collaborative which means that every user who has the document open in the editor will see changes as they are made by other developers. However, changes are not saved automatically and when the application is run from the Editor the saved version is loaded. This means you can choose at which point to commit your change to be used by the application. Any user can save the document at any time.
If you have unsaved changes the Revert option in the File menu will cancel these changes and restore the document back to the saved version. If all users exit the document before saving the unsaved changes will be preserved for a short time before the document is automatically reverted to the saved version. So you should save your work before exiting the document.
The editor supports various shortcuts to help you when editing code. You can find all those options in the Menu. The most common can be found below:
|Undo||Ctrl + Z||Cmd + Z|
|Redo||Ctrl + Y||Cmd + Shift + Z or Cmd + Y|
|Find in file||Ctrl + F||Cmd + F|
|Find next match||F3||Cmd + G|
|Find previous match||Shift + F3||Cmd + Shift + G|
|Replace||Ctrl + H||Cmd + Alt + F|
|Find in Files||Shift + Ctrl + F||Shift + Cmd + F|
|Comment line or selection||Ctrl + /||Cmd + /|
|Block Comment line or selection||Alt + Cmd + /||Shift + Ctrl + /|
|Indent line or selection||Tab||Tab|
|Un-Indent line or selection||Shift + Tab||Shift + Tab|
|Auto indent line or selection||Ctrl + I||Cmd + I|
|Jump to declaration||Alt + .||Alt + .|
|Show autocomplete||Ctrl + Space||Ctrl + Space|
|Show documentation||Ctrl + O||Cmd + O|
You can search for text in a file by pressing Ctrl + F. This will open up the Find panel on the bottom:
Enter your search term here and press Enter. You can navigate between matches by continuously hitting Enter (or Shift + Enter to go backwards).
If you want to replace a match enter the text you want to replace it with in the Replace input field on the right. Then hit Enter to replace the match and keep hitting Enter to replace each subsequent match (or Shift + Enter to replace backwards).
Find in Files
You can also find a term by searching in all your assets. To do this hit Ctrl + Shift + F to bring up the Find in Files panel on the bottom. Like before enter your search term and hit Enter. This will open up a new tab where the search results will be displayed:
On the left side of the input fields there are various search options. These allow you to search using a regular espression, do a case-sensitive search or search for whole words. If you would like to learn and experiment more with regex, then RegExr is great site with cheatsheets, examples and an online editor for testing regex expressions.
You can double click on a line in the search results to go that particular line in the asset.
You can quickly open an asset by hitting Ctrl + P (or Cmd + P on Mac) which will open a panel where you can search for the asset by its name. Start typing the name of the asset and the panel will perform a fuzzy search to find the asset you are looking for. Hit Enter to open the selected asset.
You can edit your preferences by clicking on Edit -> Preferences. Here you can change the font size for the text editor and also edit other editor related options.
Highlighting a word in the code editor will highlight all other matches in the file which makes it easier to check where a variable or function is being used.
Multiple Selection Editing
Holding down Ctrl (PC) or Cmd (Mac) and clicking left mouse button will add another cursor to the editor. This allows you to quickly make the same changes in multiple areas of the file.
Holding down Alt and dragging left mouse button will create a rectangular selection which is useful for selecting and editing columns of text.