Skip to content

Commit

Permalink
load lua init script from file
Browse files Browse the repository at this point in the history
  • Loading branch information
arozx committed Jan 23, 2025
1 parent a7accaf commit b88e651
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ set(SANDBOX_SOURCES
add_executable(sandbox ${SANDBOX_SOURCES})
target_link_libraries(sandbox PRIVATE voxel-engine)

add_custom_command(TARGET sandbox POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/sandbox/assets/scripts
${CMAKE_BINARY_DIR}/assets/scripts
)

# Enable precompiled headers - specify C++ explicitly
target_precompile_headers(voxel-engine
PRIVATE
Expand Down
8 changes: 8 additions & 0 deletions sandbox/assets/scripts/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Initialize Lua environment
if not engine then
error("Engine API not available")
end

-- Set default terrain parameters
engine.setTerrainHeight(10.0)
engine.log("Lua environment initialized")
27 changes: 14 additions & 13 deletions sandbox/src/SandboxApp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "SandboxApp.h"
#include <imgui.h>
#include <fstream>
#include <sstream>

SandboxApp::SandboxApp() : Application() {
// Check script system first
Expand Down Expand Up @@ -27,24 +29,23 @@ SandboxApp::SandboxApp() : Application() {
});
}

// Initialize with default script
// Initialize with init.lua script
try {
const std::string initScript = R"(
-- Initialize Lua environment
if not engine then
error("Engine API not available")
end
-- Set default terrain parameters
engine.setTerrainHeight(10.0)
engine.log("Lua environment initialized")
)";
std::string scriptPath = "build/assets/scripts/init.lua";
std::ifstream scriptFile(scriptPath);
if (!scriptFile.is_open()) {
LOG_ERROR_CONCAT("Failed to open init.lua at path: ", scriptPath);
return;
}

std::stringstream scriptBuffer;
scriptBuffer << scriptFile.rdbuf();

if (!scriptSystem->ExecuteScript(initScript)) {
if (!scriptSystem->ExecuteScript(scriptBuffer.str())) {
LOG_ERROR("Failed to execute initialization script");
}
} catch (const std::exception& e) {
LOG_ERROR("Script initialization error: {}", e.what());
LOG_ERROR("Script initialization error: {", e.what(), "}");
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ namespace Engine {

virtual void OnImGuiRender() {}

const std::string& GetAssetPath() const { return m_AssetPath; }

protected:
/**
* @brief Initialize the application window
Expand Down Expand Up @@ -187,6 +189,8 @@ namespace Engine {
std::unique_ptr<LuaScriptSystem> m_ScriptSystem;

static Application* s_Instance;

std::string m_AssetPath = "../sandbox/assets/"; // Base path for all assets
};

// To be defined by client application
Expand Down

0 comments on commit b88e651

Please sign in to comment.