| name | lua-binding-reference |
| description | Quick reference for Cataclysm-BN Lua binding macros, Luna usertypes, libraries, documentation, type patterns, build steps, and common errors. |
Lua Binding Quick Reference
Comprehensive reference for adding Lua bindings to Cataclysm: Bright Nights.
Luna Documentation Macros
Add to src/catalua_luna_doc.h:
LUNA_VAL( my_type, "MyType" )
LUNA_DOC( my_class, "MyClass" )
LUNA_ENUM( my_enum, "MyEnum" )
LUNA_ID( my_type, "MyType" )
LUNA_PTR_VAL( my_class, "MyClass" )
Utility Macros (in binding functions)
Define UT_CLASS before using:
#define UT_CLASS my_class
SET_MEMB( field_name )
SET_MEMB_RO( field_name )
SET_MEMB_N_RO( field, "lua_name" )
SET_FX( method_name )
SET_FX_T( method_name, signature )
SET_FX_N( method_name, "lua_name" )
SET_FX_N_T( method_name, "lua_name", sig )
#undef UT_CLASS
Luna Functions
Create Usertype
auto ut = luna::new_usertype<T>(
lua,
luna::no_bases,
luna::no_constructor
);
auto ut = luna::new_usertype<Derived>(
lua,
luna::bases<Base1, Base2>(),
luna::no_constructor
);
auto ut = luna::new_usertype<T>(
lua,
luna::no_bases,
luna::constructors<
T(),
T( int ),
T( int, std::string )
>()
);
auto cata::detail::reg_my_type( sol::state &lua ) -> void
{
auto ut = luna::new_usertype<T>( );
}
Set Members
luna::set( ut, "field", &T::field );
luna::set( ut, "field", sol::readonly( &T::field ) );
luna::set( ut, "prop",
sol::property( &T::get_prop, &T::set_prop )
);
luna::set( ut, "CONSTANT", 42 );
Set Functions
luna::set_fx( ut, "method", &T::method );
luna::set_fx( ut, "method",
sol::resolve<int() const>( &T::method )
);
luna::set_fx( ut, "create", &T::create_instance );
luna::set_fx( ut, "custom",
[]( T &self, int arg ) -> std::string { return self.internal_method( arg ); }
);
luna::set_fx( ut, "overloaded",
sol::resolve<int( std::string ) const>( &T::overloaded )
);
Operators
luna::set_fx( ut, sol::meta_function::equal_to, &T::operator== );
luna::set_fx( ut, sol::meta_function::less_than, &T::operator< );
luna::set_fx( ut, sol::meta_function::less_than_or_equal_to, &T::operator<= );
luna::set_fx( ut, sol::meta_function::addition, &T::operator+ );
luna::set_fx( ut, sol::meta_function::subtraction, &T::operator- );
luna::set_fx( ut, sol::meta_function::multiplication, &T::operator* );
luna::set_fx( ut, sol::meta_function::to_string,
[]( const T &obj ) -> std::string { return obj.to_string(); }
);
Libraries (Global Functions)
DOC( "Library description" );
luna::userlib lib = luna::begin_lib( lua, "mylib" );
luna::set_fx( lib, "func", &my_function );
luna::set( lib, "CONSTANT", VALUE );
luna::finalize_lib( lib );
Documentation
DOC( "Description line 1" );
DOC( "Description line 2" );
DOC_PARAMS(
"param1: Type - Description",
"param2: Type - Description",
"returns: Type - Description"
);
Common Type Patterns
string_id / int_id
reg_id<my_type, true>( lua );
reg_id<my_type, false>( lua );
local id = MyTypeId.new("my_id")
print(id:str()) -- Get string
print(id:is_valid()) -- Check validity
print(id:is_null()) -- Check if null
local obj = id:obj() -- Get underlying object
local int_id = id:int_id() -- Get int_id (if supported)
Enums
luna::set_enum<my_enum>( lua, {
{ "VALUE_ONE", my_enum::VALUE_ONE },
{ "VALUE_TWO", my_enum::VALUE_TWO }
});
Units Types
local m = Mass.from_gram(1000)
print(m:to_kilogram()) -- 1
local v = Volume.from_liter(1)
print(v:to_milliliter()) -- 1000
local e = Energy.from_joule(1000)
print(e:to_kilojoule()) -- 1
local a = Angle.from_degrees(180)
print(a:to_radians()) -- ~3.14
Time Types
local dur = TimeDuration.from_seconds(60)
local now = game.get_time()
local future = now + dur
Coordinates
local p = Tripoint.new(10, 20, 0)
print(p.x, p.y, p.z)
local offset = Tripoint.new(1, 0, 0)
local new_p = p + offset
Optional Values
std::optional<int> maybe_value() const;
local val = obj:maybe_value()
if val then
print("Got value:", val)
end
Collections
std::vector<item> get_items() const;
local items = char:get_items()
for i, item in ipairs(items) do
print(item:tname())
end
std::map<std::string, int> get_stats() const;
local stats = char:get_stats()
for key, value in pairs(stats) do
print(key, value)
end
Detached Pointers (Lua ownership)
luna::set_fx( ut, "remove_item",
[]( character &ch, item &it ) -> detached_ptr<item> { return detached_ptr<item>( ch.i_rem( &it ) ); }
);
luna::set_fx( ut, "add_item",
[]( character &ch, detached_ptr<item> it ) { ch.i_add( std::move( it ) ); }
);
Type Signature Examples
int() const
std::string( int ) const
bool( const std::string & ) const
void()
void( int, std::string )
int&()
static int create( std::string )
sol::resolve<int() const>( &T::get_value )
sol::resolve<int( std::string ) const>( &T::get_value )
Function Parameter Types
void func( int value );
void func( const std::string &str );
void func( int &out_value );
void func( item *it );
void func( int required, sol::optional<std::string> opt );
void func( sol::variadic_args va );
void func( sol::table config );
Registration Functions
namespace cata::detail {
auto reg_my_domain( sol::state &lua ) -> void;
}
auto cata::detail::reg_my_domain( sol::state &lua ) -> void
{
}
auto cata::detail::reg_all_bindings( sol::state &lua ) -> void
{
reg_my_domain( lua );
}
File Organization
src/
catalua.h/cpp - Main Lua interface
catalua_luna.h - Luna doc system
catalua_luna_doc.h - Type name mappings (LUNA_* macros)
catalua_bindings.h/cpp - Main bindings, units, constants
catalua_bindings_utils.h - Utility macros (SET_*, DOC)
catalua_bindings_ids.cpp - string_id/int_id bindings
catalua_bindings_*.cpp - Domain-specific bindings
Build and Test
cmake --build --preset linux-full --target cataclysm-bn-tiles
cmake --build build --target format
Lua Testing Patterns
local obj = MyClass.new()
local result = obj:method(arg)
print(obj.field)
obj.field = new_value
local derived = DerivedClass.new()
derived:base_method()
local maybe = obj:maybe_get()
if maybe then
print("Got:", maybe)
end
local items = obj:get_items()
for i, item in ipairs(items) do
print(i, item)
end
local player = game.get_player()
player:add_msg("Hello from Lua!")
Note: C++ side should use trailing return types: auto func() -> type { ... }
Common Issues and Solutions
| Issue | Solution |
|---|
luna_traits<T>::impl is false | Add LUNA_* macro to catalua_luna_doc.h |
| Linker error: undefined reference | Include the header file with implementation |
| Ambiguous function call | Use sol::resolve<signature>() |
| Type not copyable | Use luna::no_constructor and provide factory |
| Comparison operators missing | Implement operator== and operator< for type |
Build error with // *NOPAD* | Only use for reference/pointer returns |
Key References
- Integration Docs:
docs/en/mod/lua/explanation/lua_integration.md
- Style Guide:
docs/en/mod/lua/explanation/lua_style.md
- Luna System:
src/catalua_luna.h
- Examples:
src/catalua_bindings_creature.cpp, src/catalua_bindings_item.cpp
- Sol2 Docs: https://sol2.readthedocs.io/