Skip to content

wyozi-gmod/vscode-gmod-workspace

Repository files navigation

gmod-workspace

VSCode plugin to reload/load .lua files on a GarrysMod SRCDS when running a VSCode command.

Basically this is Garry's Mod's own autoreload, except completely controllable by you as the developer and triggerable remotely.

Requirements

  • VSCode
  • GarrysMod Server
    • -allowlocalhttp startup parameter (so we can query the VSCode server)
    • gmodworkspace.lua installed to garrysmod/lua/autorun
      • You can do this with included VSCode command "GModDev: Install Lua module"

Running

Open VSCode instance using Remote SSH and install the extension.

You should see "GMod: Waiting" entry in the VSCode status bar. This means that we have a local HTTP server running to serve the Lua refresh requests.

Troubleshooting

Having no "GMod: Waiting" means that the activation event for gmod-workspace was not triggered.

Make sure you open the garrysmod folder (with addons, gamemodes, etc) in VSCode

Assuming you have correctly installed gmodworkspace.lua, the status bar item should turn into "GMod: Connected" soon. This means that the server is succesfully requesting data from our editor and the workflow should be enabled.

Now you can start using the extension. Edit something, run one of the commands starting with "GModDev: Run", and you should see the file be refreshed ingame (or an error message in console).

Detecting updates from Lua

The Lua runtime sets a global variable GMODWS_UPDATE when a Lua script is run using GMod workspace.

You can use the global to easily reload things, e.g.

local function myTimer()
  -- Do something expensive
end
timer.Create("periodicRunner", 60, 0, myTimer)

if GMODWS_UPDATE then
  -- only executed when this file is run by GModWorkspace
  myTimer()
end

Architecture

  1. VSCode extension detects that we're in Garry's Mod folder and activates a local HTTP server
  2. GMod server polls the HTTP server every few seconds
  3. You run a VSCode command, the currently active file is appended to an internal list and served by the HTTP server for the next polling.

HTTP polling was chosen instead of sockets to not require modules and minimize implementation complexity.

Development

Follow instructions at https://code.visualstudio.com/api/advanced-topics/remote-extensions#installing-a-development-version-of-your-extension to test development version.