Difference between revisions of "SteamVR/Environments/Scripting/Lua Scripting Intro"

From Valve Developer Community
Jump to: navigation, search
m (Rectus moved page Destinations/Scripting/Lua Scripting Intro to SteamVR/Environments/Scripting/Lua Scripting Intro: Moved scripting section to match the rest of the SteamVR Home pages)
(Updated links and references to match the other SteamVR Home pages)
 
Line 1: Line 1:
[[Destinations]]
+
This tutorial shows how to create interactive features using [[VScript]] Lua scripts.
  
 
== Lua Scripting Overview ==
 
== Lua Scripting Overview ==
Line 5: Line 5:
 
Generally, you will need to combine some [[Inputs_and_Outputs| 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.
 
Generally, you will need to combine some [[Inputs_and_Outputs| 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 [[Destinations/Getting_Started| Destinations Getting Started Tutorial]].
+
For an introduction to creating your own addon and using Hammer, refer to the [[SteamVR/Environments/Getting_Started| Getting Started Tutorial]].
For an introduction to creating brush and point entities in Hammer, you can start with the [[Destinations/Turret_Tutorial| Destinations Turret Tutorial]].
+
For an introduction to creating brush and point entities in Hammer, you can start with the [[SteamVR/Environments/Turret_Tutorial| Turret Tutorial]].
  
 
For an introduction to the Lua scripting language, you can refer to these links:
 
For an introduction to the Lua scripting language, you can refer to these links:
Line 13: Line 13:
 
* [http://www.lua.org/pil/contents.html Programming In Lua - Online]
 
* [http://www.lua.org/pil/contents.html Programming In Lua - Online]
  
Lua scripting connects with Destinations through the [[Destinations/Scripting/API| Scripting API]] and through [[Dota_2_Workshop_Tools/Scripting/Listening_to_game_events | listening to game events]].
+
Lua scripting connects with SteamVR Home through the [[SteamVR/Environments/Scripting/API| Scripting API]] and through [[Dota_2_Workshop_Tools/Scripting/Listening_to_game_events | listening to game events]].
  
 
== Getting Started ==
 
== Getting Started ==
Line 19: Line 19:
 
[[File:arcade_toss_hammer.png|320px|thumb|right| The arcade_toss map in Hammer.]]
 
[[File:arcade_toss_hammer.png|320px|thumb|right| The arcade_toss map in Hammer.]]
  
For this documentation, I will be referencing an addon for Destinations called Arcade Toss (previously known as “Arcade Roll”).   
+
For this documentation, I will be referencing an addon for SteamVR Home called Arcade Toss (previously known as “Arcade Roll”).   
If you launch the Destinations 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.
+
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 <code>\game\steamtours_addons\arcade_roll\scripts\vscripts</code>.   
 
Open the map in Hammer and open the Lua file associated with it in <code>\game\steamtours_addons\arcade_roll\scripts\vscripts</code>.   
 
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.
 
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.
Line 94: Line 94:
 
This will create a new <code>Log: Script</code> 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.
 
This will create a new <code>Log: Script</code> 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.
  
 +
[[Category:SteamVR]]
 +
[[Category:SteamVR Home]]
 
[[Category:Scripting]]
 
[[Category:Scripting]]

Latest revision as of 15:25, 23 June 2019

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:

Lua scripting connects with SteamVR Home through the Scripting API and through listening to game events.

Getting Started

The arcade_toss map in Hammer.

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 \game\steamtours_addons\arcade_roll\scripts\vscripts. 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 game\scripts\vscripts directory.

../game/steamtours_addons/your_addon/scripts/vscripts/addon_game_mode.lua

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 function:

require( "lua_filename_here" )

Initializing Functions

The addon_game_mode.lua will need a function called Activate that automatically gets called and this will help you initialize your script.

Put a print function in it for now:

--[[
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.

The 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.

Script Debugging

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: Default tab 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 Visibility tab (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.