SteamVR/Environments/Scripting/Lua Scripting Intro
This tutorial shows how to create interactive features using VScript Lua scripts.
Lua Scripting Overview
Generally, you will need to combine some Entity Input/Output map logic (created and linked in Hammer) with some Lua scripting (created and edited in a text editor) to add gameplay elements to your Destination.
For an introduction to creating your own addon and using Hammer, refer to the Getting Started Tutorial. For an introduction to creating brush and point entities in Hammer, you can start with the Turret Tutorial.
For an introduction to the Lua scripting language, you can refer to these links:
For this documentation, I will be referencing an addon for SteamVR Home called Arcade Toss (previously known as “Arcade Roll”).
If you launch the SteamVR Workshop Tools, select "arcade_roll" and click on "Create New Addon from Selected", you will be able to access the vmap used to make it.
Open the map in Hammer and open the Lua file associated with it in
The Lua file has lots of comments that divide the script into each section and gives a better of idea of what the chunks of code do.
Default Lua Script File
Start by creating a file called
addon_game_mode.lua and placing it in your
This will automatically load when your map is loaded in your addon directory.
Additional Lua files can be loaded along with the
addon_game_mode.lua by using the
require( "lua_filename_here" )
The addon_game_mode.lua will need a function called
Activate that automatically gets called and this will help you initialize your script.
--[[ Your Addon Name Here ]] function Activate() print("Your Addon Name Here!") end
Now, when you load a map in your addon’s maps directory, it should show your addon name in the developer console.
Activate function is generally used to setup classes for the game mode to utilize.
function Activate() print("Activate!\n"); GameRules:GetGameModeEntity().CArcadeTossGameMode = CArcadeTossGameMode() GameRules:GetGameModeEntity().CArcadeTossGameMode:InitGameMode() end function CArcadeTossGameMode:InitGameMode() GameRules:GetGameModeEntity():SetThink( "OnThink", self, 1 ) end function CArcadeTossGameMode:OnThink() --print("I'm thinking!") return 1 end
This sets up the class for the game mode and starts a
Think function that will print a message to the console every second.
More examples of
Think functions can be found here.
I find it incredibly helpful to show only the log related to the script in the developer console.
- Click on the
+sign to the left of the
Log: Defaulttab in the developer console "Add a new log viewer with custom configured channels".
- Give it a name like "Script".
- The log viewer window will appear. Click on the
Visibilitytab (it should be the default tab) and scroll down to "VScript" and then click on OK.
This will create a new
Log: Script tab in your developer console that only shows VScript information. You'll be able to see your print messages here as well as any error messages that will help you debug.