Skip to content

Commit 263a039

Browse files
committed
Add support for changing LuaScriptLanguage's package_path and package_cpath using project settings
1 parent a5a8d66 commit 263a039

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- `LuaState.LoadMode` enum for specifying the Lua load mode: text, binary or any
99
- `LuaState.do_buffer` and `LuaState.load_buffer` methods for loading Lua code from possibly binary chunks
1010
- `LuaState.package_path` and `LuaState.package_cpath` properties for accessing the value of Lua's [`package.path`](https://www.lua.org/manual/5.4/manual.html#pdf-package.path) and [`package.cpath`](https://www.lua.org/manual/5.4/manual.html#pdf-package.cpath)
11+
- Advanced project settings for setting the `LuaScriptLanguage` state's `package_path` and `package_cpath` properties
1112

1213
### Changed
1314
- The GDExtension is now marked as reloadable

src/script-language/LuaScriptLanguage.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "../utils/function_wrapper.hpp"
3333

3434
#include <godot_cpp/classes/engine.hpp>
35+
#include <godot_cpp/classes/project_settings.hpp>
3536
#include <godot_cpp/classes/reg_ex.hpp>
3637
#include <godot_cpp/classes/reg_ex_match.hpp>
3738
#include <godot_cpp/classes/resource_loader.hpp>
@@ -40,6 +41,19 @@
4041

4142
namespace luagdextension {
4243

44+
constexpr char LUA_PATH_SETTING[] = "lua_gdextension/lua_script_language/package_path";
45+
constexpr char LUA_CPATH_SETTING[] = "lua_gdextension/lua_script_language/package_c_path";
46+
constexpr char LUA_CPATH_WINDOWS_SETTING[] = "lua_gdextension/lua_script_language/package_c_path.windows";
47+
constexpr char LUA_CPATH_MACOS_SETTING[] = "lua_gdextension/lua_script_language/package_c_path.macos";
48+
49+
static void add_project_setting(ProjectSettings *project_settings, const String& setting_name, const Variant& initial_value, bool is_basic = false) {
50+
if (!project_settings->has_setting(setting_name)) {
51+
project_settings->set_setting(setting_name, initial_value);
52+
}
53+
project_settings->set_initial_value(setting_name, initial_value);
54+
project_settings->set_as_basic(setting_name, is_basic);
55+
}
56+
4357
String LuaScriptLanguage::_get_name() const {
4458
return "Lua";
4559
}
@@ -57,6 +71,17 @@ void LuaScriptLanguage::_init() {
5771
LuaScriptMethod::register_lua(state);
5872
LuaScriptProperty::register_lua(state);
5973
LuaScriptSignal::register_lua(state);
74+
75+
// Register project settings
76+
ProjectSettings *project_settings = ProjectSettings::get_singleton();
77+
add_project_setting(project_settings, LUA_PATH_SETTING, "res://?.lua;res://?/init.lua");
78+
add_project_setting(project_settings, LUA_CPATH_SETTING, "!/?.so;!/loadall.so");
79+
add_project_setting(project_settings, LUA_CPATH_WINDOWS_SETTING, "!/?.dll;!/loadall.dll");
80+
add_project_setting(project_settings, LUA_CPATH_MACOS_SETTING, "!/?.dylib;!/loadall.dylib");
81+
82+
// Apply project settings (package.path, package.cpath)
83+
lua_state->set_package_path(project_settings->get_setting_with_override(LUA_PATH_SETTING));
84+
lua_state->set_package_cpath(project_settings->get_setting_with_override(LUA_CPATH_SETTING));
6085
}
6186

6287
String LuaScriptLanguage::_get_type() const {

0 commit comments

Comments
 (0)