Lua

From NS2 community wiki
Jump to: navigation, search

Contents

What is Lua?

Lua is an extension programming language that is widely used as a scripting language in applications. Due to its simple syntax, it is used in games such as Garry's Mod, World of Warcraft, Far Cry and many more.

Within Natural Selection 2, the majority of the game code will be written in Lua.
As well as providing a convenient environment for the NS2 team to code in, it will allow users to create modifications of the game.

Visit the official Lua site or the associated Wikipedia article for more information.

Documentation

Warning Warning: The documentation of Lua within Natural Selection 2 is far from complete. Information may be incorrect and its reliability of data should not be trusted.

Example: Key Input

local function SendKeyEvent(key, down)
    if key == InputKey.Q and down then
        Shared.Message('Hello World')
    end
end
Event.Hook("SendKeyEvent", SendKeyEvent)

File paths

There are four URL-style path prefixes available to Lua scripts:

game://
The base game directory (ns2) with active mod directories laid on top. Scripts cannot write to this directory.
config://
The per-user config directory, located within the current Windows user profile.
cache://
The directory in which pathing data and compiled shaders are stored. In current builds it's inside the config directory.
temp://
Unknown location.

Printing to the console

The standard Lua print() function doesn't print to the game console. Use one of these instead:

Shared.Message(string)
Prints to the console without decoration.
DebugPrint(string)
Prints to the console with the name of the current VM and a timestamp.
Personal tools