Half-Life: Alyx Workshop Tools/Lua Scripting/Hello Gordon

From Valve Developer Community
Jump to navigation Jump to search
Hello Gordon

If you have any programming experience, you know what is coming. The objective of this example is simply to show how you can get a script running on top of the source engine.

Before Getting Started

Create a new script

In order to execute code in game, we need the code in the right place for the Source Engine to find

Where to put the new script

The Source engine by default looks in scripts/vscripts and any folders inside for any scripts

  • Go to your scripts/vscripts folder and make a new folder for your scripts in this example the folder will be called HelloGordon
  • inside the folder create a new file called HelloGordon.lua (You can create a text file and change the end to .lua)

Write Code

  • Open your script in your favorite editor (sublime text/ notepad/ vs code) and input the following:
-- this code will simply print to the command line (that's how we know it is working)
print("Hello Gordon")
  • save the file.

Connect The Code in Hammer

To get the code that we wrote working with source, we need to connect it to entities in the engine that will trigger the execution.

Open a new (or existing) project

  • launch a new/existing addon
  • open from template or use an existing map

Create a logic_script entity

The logic_script entity will hold a reference to your script so that we can call it later

  • Using the entity tool (Shift + E) create an entity of any type somewhere on your map, double click it to open the settings
  • Change the entity to a logic script by typing logic_script in the class field
  • Give the script a name in the name field (i.e. MainLogic). This will be used to target this entity when we want to call the script.
  • Put the path to your script in the misc-> Entity Scripts field (i.e HelloGordon/HelloGordon.lua)
  • There is no need to include scripts/vscripts in the path since that is already the base path of where source searches for the scripts.

Up Next

  • coming soon

More Examples