The VScript Book

Chapter 2.1: The logic_script Entity

The simplest way to run a script file is with a logic_script entity. This is a special entity you place in your map that has no physical presence; its only job is to run code.

When the map loads, the engine will find your logic_script and automatically run the code inside its specified script file (from top to bottom, one time). This makes it perfect for setting up map-wide logic, preparing puzzles, or starting timers when the level begins.

A Pre-made List of Contacts: The EntityGroup

A very powerful feature of the logic_script is its ability to be pre-loaded with handles to other entities. Think of it like giving your script a phonebook of important contacts it might need to call.

In Hammer, you'll see keyvalues named `EntityGroup00`, `EntityGroup01`, and so on. By filling these with the targetnames of other entities in your map, you make them instantly available to your script inside a special Array called `EntityGroup`.

Example: Opening a set of doors at map start.

In Hammer:

  • Create a logic_script.
  • Set its "VScript File" property to door_manager.nut.
  • Set its "EntityGroup00" property to exit_door_01.
  • Set its "EntityGroup01" property to exit_door_02.

In door_manager.nut:

// This script runs as soon as the logic_script spawns.
printl("Door Manager Script is now running!")

// The 'EntityGroup' array was automatically created for us
// because we filled it out in Hammer.
// EntityGroup[0] now holds a handle to "exit_door_01".
// EntityGroup[1] now holds a handle to "exit_door_02".

// We can loop through this pre-made list to control all the doors.
foreach(index, doorEnt in EntityGroup) {
    printl("Opening door #" + index)
    EntFireByHandle(doorEnt, "Open", "", 0, null, null)
}

This is much more efficient than writing separate Entities.FindByName() calls for every single door.