Visual Studio Code for LSL

VS Code Logo

This is a brief guide on setting up Visual Studio Code in Windows for creating LSL scripts for Second Life. I am by no means an expert with Visual Studio Code and would like to thank Coal Edge for a lot of tips and info used in this guide. The end goal is to get as close to Eclipse with LSLForge as currently possible (minus the simulation features). You will be able to create your LSL files with syntax highlighting and use external tools to preprocess the script (for #includes) and to optimize your scripts using LSL-PyOptimizer.

You could use the Firestorm preprocessor but I feel like doing all your code preprocessing, editing, and optimizing externally from Firestorm has a better workflow – at least for me.

The below assumes some basic understanding of Visual Studio Code so it’s a fairly brief set of instructions – you may need to read through the extension descriptions on their marketplace pages or other linked documentation for more info on how to accomplish certain tasks.

  • Install the following VS Code extensions:
    • TextMate Languages
    • LSL-FP
      1. Update with snippets.json from https://github.com/Shaunnnnn/vscode-lsl-fp (or a more recent fork if available). This will give you more recent auto-complete/intellisense data. (%USERPROFILE%\.vscode\extensions\dalghost.lsl-fp-1.2.1\snippets\snippets.json — note if you’re not using version 1.2.1 of the extension, your path will be different)
      2. Replace lsl.tmLanguage.json (link to my own generated file from June 25, 2022) located in ‘%USERPROFILE%\.vscode\extensions\dalghost.lsl-fp-1.2.1’ in Windows.
      3. Optional: If you want to create your own tmLanguage file…. generate Sublime tmLanguage files with https://github.com/Makopo/kwdb_to_sublime – I created a Ubuntu virtual machine for this and installed the required modules listed on the GitHub page above. Then run the generate.sh script. This is probably the trickiest part of these instructions. Convert your generated lsl.tmLanguage file generated in the last step to lsl.tmLanguage.json using the TextMate Languages extension you installed at the beginning (see marketplace page for command for this).
    • LSLint For VSCode (doesn’t yet support #includes, so I don’t use it but it’s here if you want to use it instead of includes/mcpp)
      • Use this for real-time syntax checking (linting) of LSL files in VS Code. If you want to use #includes, they are not supported with this.
    • Add other extensions as desired.

Next, we’ll set up a folder in Windows to store our tools for processing our LSL files.

  • Create directory “C:\SL” (if you use a different directory, update paths as required below).
  • Add “C:\SL” to Windows path (see this tutorial)
  • Download the mcpp binary package and copy mcpp.exe to “C:\SL” directory. This is a preprocessor that will allow you to include other .lsl files into your code with #include “filename.lsl” at the top of the file. It has other features, but I mainly just use it for the includes.
  • Extract LSL-PyOptimizer contents (or git clone) to “C:\SL\LSL-PyOptimizer” directory.
  • Install Python 2.7 (or whichever is compatible with current version of LSL-PyOptimizer). You should be able to run the command “python” from a command prompt and have it enter a Python shell. If the store pops up asking you to install Python 3, see this thread about Python app executions in Windows apps and features. Make sure that during the installation process you enable the option “Add python.exe to Path” or else you’ll get an error saying python is not recognized.
  • Create a custom task in VS Code

Replace contents of tasks.json file with the below (it will be in a .vscode folder in your project after creating a custom task above):

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "LSL-Optimize",
            "command": "python",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "args": [
                "C:\\SL\\LSL-PyOptimizer\\main.py",
                "-p",
                "mcpp",
                "--timestamp",
                "${file}"
            ],
            "presentation": {
                "echo": true,
                "panel": "shared",
                "focus": true,
                "clear": true,
                "showReuseMessage": false
            }
        }
    ]
}

There are a few settings we need to tweak to tie everything together.

In VS Code, File > Preferences > Settings > search “terminal right click”
Set Terminal Integrated Right Click to default. This allows you to right-click and select all in the terminal pane which will allow you to copy the script output from LSL-PyOptimizer so you can paste it into a script in-world.

Run above custom task via Terminal > Run build task (or CTRL+SHIFT+B).

The end result is that when you finish working on your script in VS Code and run the build task, it will run it through LSL-PyOptimizer which is configured to preprocess the file with mcpp first, then output a memory-optimized LSL file to the terminal pane in VS Code. Then just select all of the output and copy it into a script in-world.

Some notes on #include directives for mcpp:
You may have to specify the include as an absolute path to make it work. Sometimes mcpp seems to have trouble finding the file to include, even if it’s in the same directory as the script that is including it. To quickly get the absolute path of the include file, hold SHIFT and right-click it in File Explorer, then select Copy as path. You can then paste it after the #include directive at the top of your script.

Some examples of #includes:

#include "myInclude.lsl"
// Basic include. Should work most of the time.

#include "C:\Path\To\myInclude.lsl"
// Absolute path to the include.
// This one might generate an error along the lines
// of 'Illegal UCN sequence' but still works. Likely due to the 
// unescaped backslashes

#include "C:\\Path\\To\\myInclude.lsl"
// Backslashes are escaped... kind of a pain.
// I prefer the one below to escaping the backslashes.

#include <C:\Path\To\myInclude.lsl>
// This one seems to deal with the backslashes better