Skip to main content

VS Code Snippets

Below are some VS Code snippets that can be used to speed up development.

Using Snippets

Snippets are a Visual Studio Code feature. Check out the Snippets documentation for more info. Adding Snippets for Lua is very easy.

  1. Within Visual Studio, navigate from the toolbar: File -> Preferences -> User Snippets
  2. Type in and select lua.json
  3. Within the {} braces, include any or all of the snippets below
  4. Save the file
  5. Within your actual source files, start typing a prefix (e.g. "Cradle") and select the autocompleted snippet to paste it in
  6. Depending on the snippet, parts of the pasted code will be selected and can be typed over (e.g. setting the name of a service)

Cradle Snippets

Below are useful VS Code snippets for Cradle.

Cradle

Include a require statement for Cradle.

Snippet
"Cradle": {
"prefix": ["cradle"],
"body": ["local Cradle = require(game:GetService("ReplicatedStorage").Cradle)"],
"description": "Require the Cradle module"
}
Code Result
local Cradle = require(game:GetService("ReplicatedStorage").Cradle)

Service

Reference a Roblox service.

Snippet
"Service": {
"prefix": ["service"],
"body": ["local ${0:Name}Service = game:GetService(\"${0:Name}Service\")"],
"description": "Roblox Service"
}
Code Result
local HttpService = game:GetService("HttpService")

Cradle Service

Reference Cradle, create a service, and return the service.

Snippet
"Cradle Service": {
"prefix": ["cradleservice"],
"body": [
"local Cradle = require(game:GetService('ReplicatedStorage').Cradle)",
"",
"local ${0:$TM_FILENAME_BASE} = Cradle:CreateService({",
"\tName = \"${0:$TM_FILENAME_BASE}\",",
"\tClient = {",
"someEvent = Cradle.RemoteComm.RemoteEvent.new(),",
"someTableProperty = Cradle.RemoteComm.RemoteProperty.new({}),",
"someNumberProperty = Cradle.RemoteComm.RemoteProperty.new(123),",
"},",
"})",
"",
"",
"function ${0:$TM_FILENAME_BASE}:Start()",
"\t",
"end",
"",
"",
"function ${0:$TM_FILENAME_BASE}:Init()",
"\t",
"end",
"",
"",
"return ${0:$TM_FILENAME_BASE}",
""
],
"description": "Cradle Service template"
}
Code Result
local Cradle = require(game:GetService('ReplicatedStorage').Cradle)

local ExampleService = Cradle:CreateService({
Name = script.Name,
Client = {
someEvent = Cradle.RemoteComm.RemoteEvent.new(),
someTableProperty = Cradle.RemoteComm.RemoteProperty.new({}),
someNumberProperty = Cradle.RemoteComm.RemoteProperty.new(123),
},
})

function ExampleService:Start() end

function ExampleService:Init() end

return ExampleService

Cradle Controller

Reference Cradle, create a controller, and return the controller.

Snippet
"Cradle Controller": {
"prefix": ["cradlecontroller"],
"body": [
"local Cradle = require(game:GetService('ReplicatedStorage').Cradle)",
"",
"local ${0:$TM_FILENAME_BASE} = Cradle:CreateController({ Name = \"${0:$TM_FILENAME_BASE}\" })",
"",
"",
"function ${0:$TM_FILENAME_BASE}:Start()",
"\t",
"end",
"",
"",
"function ${0:$TM_FILENAME_BASE}:Init()",
"\t",
"end",
"",
"",
"return ${0:$TM_FILENAME_BASE}",
""
],
"description": "Cradle Controller template"
}
Code Result
local Cradle = require(game:GetService("ReplicatedStorage").Cradle)

local ExampleController = Cradle:CreateController({
Name = script.Name,
})

function ExampleController:Start() end

function ExampleController:Init() end

return ExampleController

Lua Class

A standard Lua class.

Snippet
"Class": {
"prefix": ["class"],
"body": [
"local ${0:$TM_FILENAME_BASE} = {}",
"${0:$TM_FILENAME_BASE}.__index = ${0:$TM_FILENAME_BASE}",
"",
"",
"function ${0:$TM_FILENAME_BASE}.new()",
"\tlocal self = setmetatable({}, ${0:$TM_FILENAME_BASE})",
"\treturn self",
"end",
"",
"",
"function ${0:$TM_FILENAME_BASE}:Destroy()",
"\t",
"end",
"",
"",
"return ${0:$TM_FILENAME_BASE}",
""
],
"description": "Lua Class"
}
Code Result
local MyClass = {}
MyClass.__index = MyClass

function MyClass.new()
local self = setmetatable({}, MyClass)
return self
end

function MyClass:Destroy()

end

return MyClass