Embedding Lua in the Source Engine: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
No edit summary
 
(15 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{otherlang2
{{lang|title=Embedding Lua in the Source Engine|Embedding Lua in the Source Engine}}
|title= Embedding Lua in the Source Engine
|ru=Embedding_Lua_in_the_Source_Engine:ru
}}
{{orphan}}{{pov}}
<div style="margin:auto;text-align:center;">
<div style="margin:auto;text-align:center;">


'''[[User:Andrewmcwatters|Andrew C. P. McWatters]]''' ([mailto:me@andrewmcwatters.com me@andrewmcwatters.com]), 2010
'''Andrew Charles Parker McWatters''' ([mailto:andrew@andrewmcwatters.com andrew@andrewmcwatters.com]), 2010
<br />
<br />
Software Development Engineer
Software Development Manager, Planimeter


[[File:Source-logo.png|200px|link=|Source]][[File:Lua-Logo_128x128.png|128px|link=|Source]]</div>
[[File:Source-logo.png|200px|link=|Source]][[File:Lua-Logo_128x128.png|128px|link=|Source]]</div>
Line 17: Line 13:
Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.
Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.


You can find out more about Lua on their website, [http://www.lua.org/about.html here].
Find out more about Lua on [http://www.lua.org/about.html here].


== Overview ==
== Overview ==
Lua is currently [http://www.satori.org/2009/03/the-engine-survey-general-results/ the leading scripting language in games], and for Steam users, is synonymous with [[Garry's Mod]]. Unfortunately, Garry's Mod isn't open source, and as a result, we cannot use Team Garry's implementation of Lua for our and others' benefit. However, my project, {{HL2}} [http://code.google.com/p/hl2sb/ Half-Life 2: Sandbox] is currently the leading open source project dedicated to bringing the developer community a method for embedding Lua into their mod nearly seamlessly, and in this article I'll explain how.
Lua is currently [http://www.satori.org/2009/03/the-engine-survey-general-results/ the leading scripting language in games], and for Steam users, is synonymous with [[Garry's Mod]]. Unfortunately, Garry's Mod isn't open source. However, {{HL2}} [http://code.google.com/p/hl2sb/ Half-Life 2: Sandbox] is currently the leading open source project dedicated to bringing the developer community a method for embedding Lua into their mod nearly seamlessly, and this article will explain how.


[[Half-Life 2: Sandbox]] uses a pure variant of Lua, different than Garry's implementation of the language, which has restrictions on many libraries, and modified syntax for C++ styled comments. Half-Life 2: Sandbox's goal is to provide a neutral, clean, standardized and well documented code base for interfacing with Lua, unlike articles seen within the Valve Developer Community such as the [http://www.rasterbar.com/products/luabind.html LuaBind] oriented [[Lua tutorial]] or [[Adding Lua]] article.
[[Half-Life 2: Sandbox]] uses a pure variant of Lua, different than Facepunch's implementation of the language, which has restrictions on many libraries, and modified syntax for C++ styled comments. [[Half-Life 2: Sandbox]] goal is to provide a neutral, clean, standardized and well documented code base for interfacing with Lua, unlike articles seen within the Valve Developer Community such as the [http://www.rasterbar.com/products/luabind.html LuaBind] oriented [[Lua tutorial]] or [[Adding Lua]] article.


== Implementation ==
== Implementation ==
=== Files ===
=== Files ===
* [https://github.com/Noiwex/hl2sb-src/blob/master/src/game/shared/luamanager.cpp luamanager.cpp]
* [https://github.com/Planimeter/hl2sb-src/blob/master/src/game/shared/luamanager.cpp luamanager.cpp]
* [https://github.com/Noiwex/hl2sb-src/blob/master/src/game/shared/luamanager.h luamanager.h]
* [https://github.com/Planimeter/hl2sb-src/blob/master/src/game/shared/luamanager.h luamanager.h]
* [https://github.com/jaredballou/hl2sb/blob/master/bin/lua51.dll lua51.dll]


=== Installation ===
=== Installation ===
The Lua manager scripts found in the Half-Life 2: Sandbox source code were written generically so they could be added to any mod.
The Lua manager scripts found in the [[Half-Life 2: Sandbox]] source code were written generically so they could be added to any mod.


* If you're working with a mod's source code, rather than a plugin, the files above should be placed within '''src/game/shared'''
* While working with a mod's source code, rather than a plugin, the files above should be placed within '''src/game/shared'''
* In your project files, place both the '''luamanager.cpp''' and '''luamanager.h''' in the '''Source Files''' filter
* In the project files, place both the '''luamanager.cpp''' and '''luamanager.h''' in the '''Source Files''' filter
* Compile your binaries
* Compile the binaries
* Simply add the precompiled '''lua51.dll''' to your mod's '''bin''' folder, and now your mod has integrated Lua!
* Simply add the precompiled '''lua51.dll''' to the mod's '''bin''' folder!
 
=== Source 2013 Implementation ===
[https://www.moddb.com/members/kypt1 KYPT1]:
Make sure your mod uses [https://git-scm.com/ Git] versioning software so we can cherry-pick the commit from OpenMod to your project. (Note this only supports the MultiPlayer & TF2 2013 SDK).
== Installation of Git. ==
On {{Linux|4}} You should install it via your package manager (For example on Arch Linux you can run '''sudo pacman -Syu git'''). Then in your mod's directory run '''git init && git add . && git commit -m "Initial Git Commit"'''.
On {{windows|2}} You can install it via the .exe. Simply head to git's website and install it. Once you've done that head to your mod's directory and run '''git init && git add . && git commit -m "Initial Git Commit"'''
== Installation of Lua. ==
Once you've set up Git cherrypick the {{github|https://github.com/ThePixelMoon/OpenMod}} via the command:
'''git remote add openmod https://github.com/ThePixelMoon/OpenMod.git && git fetch openmod 1dffddd557c5a1bfeec2a8c2ddad6883b2935b87 && git cherry-pick 1dffddd557c5a1bfeec2a8c2ddad6883b2935b87'''
Resolve conflicts (if any) and commit the changes using '''git cherry-pick --continue'''.


'''Update:''' Half-Life 2: Sandbox has went through several revisions extending its Lua API greatly including many classes and libraries for real-world usage with Source. As a result of these expansions, you'll now find that to be able to use these classes, you'll need to add the individual class .cpp and .h files, which are all listed in luamanager.cpp by headers that correspond to .cpp files.


== External links ==
== External links ==
* [http://www.lua.org/about.html Lua: about]
* [http://www.lua.org/about.html Lua: about]
* [http://lua-users.org/wiki/BindingCodeToLua lua-users wiki: Binding Code To Lua]
* [http://lua-users.org/wiki/BindingCodeToLua lua-users wiki: Binding Code To Lua]
* [http://code.google.com/p/hl2sb/ Half-Life 2: Sandbox's google code page]
* [https://git-scm.com/ Git]


[[Category:Programming]]
[[Category:Scripting]]
[[Category:Scripting]]

Latest revision as of 07:00, 19 February 2025

English (en)Русский (ru)Translate (Translate)

Andrew Charles Parker McWatters (andrew@andrewmcwatters.com), 2010
Software Development Manager, Planimeter

SourceSource

What is Lua?

Lua is a powerful, fast, lightweight, embeddable scripting language.

Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.

Find out more about Lua on here.

Overview

Lua is currently the leading scripting language in games, and for Steam users, is synonymous with Garry's Mod. Unfortunately, Garry's Mod isn't open source. However, Half-Life 2 Half-Life 2: Sandbox is currently the leading open source project dedicated to bringing the developer community a method for embedding Lua into their mod nearly seamlessly, and this article will explain how.

Half-Life 2: Sandbox uses a pure variant of Lua, different than Facepunch's implementation of the language, which has restrictions on many libraries, and modified syntax for C++ styled comments. Half-Life 2: Sandbox goal is to provide a neutral, clean, standardized and well documented code base for interfacing with Lua, unlike articles seen within the Valve Developer Community such as the LuaBind oriented Lua tutorial or Adding Lua article.

Implementation

Files

Installation

The Lua manager scripts found in the Half-Life 2: Sandbox source code were written generically so they could be added to any mod.

  • While working with a mod's source code, rather than a plugin, the files above should be placed within src/game/shared
  • In the project files, place both the luamanager.cpp and luamanager.h in the Source Files filter
  • Compile the binaries
  • Simply add the precompiled lua51.dll to the mod's bin folder!

Source 2013 Implementation

KYPT1: Make sure your mod uses Git versioning software so we can cherry-pick the commit from OpenMod to your project. (Note this only supports the MultiPlayer & TF2 2013 SDK).

Installation of Git.

On Linux Linux You should install it via your package manager (For example on Arch Linux you can run sudo pacman -Syu git). Then in your mod's directory run git init && git add . && git commit -m "Initial Git Commit". On Windows Windows You can install it via the .exe. Simply head to git's website and install it. Once you've done that head to your mod's directory and run git init && git add . && git commit -m "Initial Git Commit"

Installation of Lua.

Once you've set up Git cherrypick the https://github.com/ThePixelMoon/OpenMod https://github.com/ThePixelMoon/OpenMod via the command: git remote add openmod https://github.com/ThePixelMoon/OpenMod.git && git fetch openmod 1dffddd557c5a1bfeec2a8c2ddad6883b2935b87 && git cherry-pick 1dffddd557c5a1bfeec2a8c2ddad6883b2935b87 Resolve conflicts (if any) and commit the changes using git cherry-pick --continue.


External links