This article relates to the game "Half-Life: Alyx". Click here for more information.
This article relates to the workshop tools for "Half-Life: Alyx". Click here for more information.
This article's documentation is for Source 2. Click here for more information.

How Lua Works With The Api

From Valve Developer Community
Jump to: navigation, search

How to Make API Calls with Lua with Half Life : Alyx

Note.png Note:  As HL: Alyx is a relatively new game, some API functions have not yet been implemented (see scripting api). This may affect the possible scope of your scripts.

The main reason for creating Lua scripts in the first place is to interact with the game engine in a meaningful way. Api calls are used to get or update the current state of the engine (i.e. the game you are playing).

  • For example one could find a particular NPC/Entity in the map and call a function on it.

What Does It Mean to Make an API Call?

Each of the functions in the Scripting API (see below) are callable from any point in a Lua script. This means that an equivalent function exists in the source engine and can be triggered from an outside source (lua code).

  • The Lua code itself lives outside of the engine and can be interpreted at run-time.
  • When one makes an API call from Lua, they are only actually indirectly calling any code. The actual code implementation is in the engine itself.
  • Scripting API


Basic Example - API call vs Lua function

See the following basic example:

recordedServerTime = Time()
print(recordedServerTime)
  • Time() is an API call, the lua code asks the Source engine for the amount of time the server has been running. It does not keep track of time in the script.
  • print() is a lua function, it does not need to talk to the api to work


Possible Reasons Why Some API Calls Won't Work

  • Due to the way Half Life : Alyx was developed, certain API calls were simply not implemented perhaps due to lack of need for them in the first place (speculation)
  • Similar logic to that of scripts could be created with hammer logical entities (i.e. logic_auto, logic_branch, etc...)

More Lua Scripting:

Lua Scripting