Destinations

Destinations

Not enough ratings
Setting Up Your Level Script
By SparkyMcSparks
A good starting point for your map VScript.
   
Award
Favorite
Favorited
Unfavorite
Level Script Overview
Building upon the basics to Scripting in Source 2, what we'll be doing is using the level script for setting up callbacks for states in the game, and creating another LUA script to store logic for actual gameplay.

For the example below in this guide, my Addon is called "my_test" and my map name is "example".
Main Script
For the level script the engine looks for under the same name as your map:
...\Destinations\game\steamtours_addons\YOUR_ADDON_NAME\scripts\vscripts\map_scripts\YOUR_MAP_NAME.lua

We'll be using this script snippet[pastebin.com] which sets up callbacks.

NOTE: Pay attention to Line 17 which creates a script_logic entity which will point to a new LUA script where we'll be storing actual gameplay, the second parameter is the name of the file we will be creating in the next step.
ScriptLogicEntity = SpawnScriptEnt( "MapLogic", "example_logic" )

AllTriggers = {} ScriptLogicEntity = nil ScriptLogicScope = nil HasUpdateFunc = false ----------------------------------------------------------- function VRMapPrint( msg ) print( "VR_MAP_MAIN: " .. msg ) end ----------------------------------------------------------- function OnInit() VRMapPrint( "OnInit()" ) --Create the logic_script entity that controls the logic for the map ScriptLogicEntity = SpawnScriptEnt( "MapLogic", "example_logic" ) ScriptLogicScope = ScriptLogicEntity:GetPrivateScriptScope() VRMapPrint( "Created script logic entity" ) CallScriptLogicFunction( "OnInit" ) if vlua.contains( ScriptLogicScope, "OnUpdate" ) then HasUpdateFunc = true end end ----------------------------------------------------------- function OnPrecache( context ) VRMapPrint( "OnPrecache()" ) --PrecacheResource( "particle", "*.vpcf", context ) --PrecacheResource( "soundfile", "*.vsnd", context ) --PrecacheResource( "sound", "*.vsnd", context ) --PrecacheResource( "particle_folder", "particles", context ) CallScriptLogicFunction( "OnPrecache", context ) end ----------------------------------------------------------- function OnActivate() VRMapPrint( "OnActivate()" ) CallScriptLogicFunction( "OnActivate" ) end ----------------------------------------------------------- function OnGameplayStart() VRMapPrint( "OnGameplayStart()" ) CallScriptLogicFunction( "OnGameplayStart" ) end ----------------------------------------------------------- function OnPlayerSpawned() VRMapPrint( "OnPlayerSpawned()" ) ScriptSystem_AddPerFrameUpdateFunction( OnUpdate ) CallScriptLogicFunction( "OnPlayerSpawned" ) end ----------------------------------------------------------- function OnHMDAvatarAndHandsSpawned() VRMapPrint( "OnHMDAvatarAndHandsSpawned()" ) CallScriptLogicFunction( "OnHMDAvatarAndHandsSpawned" ) end ----------------------------------------------------------- function OnShutdown() VRMapPrint( "OnShutdown()" ) CallScriptLogicFunction( "OnShutdown" ) end ----------------------------------------------------------- function OnUpdate() if VRMapPrint == false then return end if HasUpdateFunc then CallScriptLogicFunction( "OnUpdate" ) end end ----------------------------------------------------------- function SpawnScriptEnt( entName, scriptName, pos, ang ) pos = pos or Vector(0, 0, 0) ang = ang or QAngle(0, 0, 0) local spawnTable = { classname = "logic_script", targetname = entName, vscripts = scriptName, origin = pos, angles = ang } local spawnedEnt = SpawnEntityFromTableSynchronous( "logic_script", spawnTable ) return spawnedEnt end ----------------------------------------------------------- function CallScriptLogicFunction( functionName, params ) if vlua.contains( ScriptLogicScope, functionName ) then if params == nil then ScriptLogicScope[functionName]( ScriptLogicScope ) else ScriptLogicScope[functionName]( ScriptLogicScope, params ) end else VRMapPrint( "Logic ent doesn't contain function named: " .. functionName ) end end
Logic Script
For the logic script, the engine by default will start looking for additional LUA files in the vscripts folder, NOT the map_scripts folder.


So we'll want to make a new script here: ...\Destinations\game\steamtours_addons\YOUR_ADDON_NAME\scripts\vscripts\NAME_FROM_LAST_STEP.lua

This script will be longer and can't find in the guide as a whole, so we'll break down important parts in pieces. The full script can be viewed here[pastebin.com].

Entity Handles

At line 26 is an enum of sorts that setups entities whose handles are needed by the script, with some key / value pairs for what will be handled.

For example:
Triggers = { TestTriggerTouched = { Name = "test_trigger", OutputName = "OnStartTouch", RedirectFunction = "OnTestTriggerTouched", Disable = false, Ent = nil } },

We setup a handle for Trigger entity types, and for our first (and only entry) we identify it with a label TestTriggerTouched

Name key is set to "test_trigger", which matches with the Name of our entity in Hammer level editor as seen in the image to the side.

The OutputName works similar to Source 1, where you define what type of event to catch -- for this example, when the Player touches the trigger.

The RedirectFunction is like the callbacks, in that the function gets called when the OutputName event is triggered. If we look in our level logic script, we have two prints that will happen:
----------------------------------------------- function OnTestTriggerTouched( self, args ) VRPrint( "OnTestTriggerTouched()" ) VRPrint( "Test Trigger Worked" ) end
2 Comments
Classixz Aug 6, 2016 @ 12:30pm 
Nice Sparky :-)
Sharp Jul 29, 2016 @ 6:09am 
Neato