added activeFunc for active obj

This commit is contained in:
Vlad committed 2026-04-11 16:18:59 +06:00
1 parent 943b6b4d21
commit bc2997d86f
7 files changed
+97 -47

No files matched your search

+57 -28
View File
@@ -1,6 +1,7 @@
#include "ScriptEngine.h"
#include "Game.h"
#include <iostream>
#include <stdexcept>
#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>
@@ -148,6 +149,62 @@ namespace ZL {
}
}
void ScriptEngine::callActivateFunction(const std::string& functionName) {
if (!impl) {
throw std::runtime_error("[SCRIPT] Engine not initialized!");
}
if (functionName.empty()) {
throw std::runtime_error("[SCRIPT] Activate function name is empty!");
}
sol::state& lua = impl->lua;
std::cout << "[SCRIPT] Looking for activate function: " << functionName << std::endl;
sol::function activateFunc = lua[functionName];
if (!activateFunc.valid()) {
throw std::runtime_error("[SCRIPT] Lua function not found: " + functionName);
}
std::cout << "[SCRIPT] Found function! Calling: " << functionName << std::endl;
auto result = activateFunc();
if (!result.valid()) {
sol::error err = result;
throw std::runtime_error("[SCRIPT] Error executing " + functionName + ": " + std::string(err.what()));
}
std::cout << "[SCRIPT] Function executed successfully!" << std::endl;
}
void ScriptEngine::callItemPickupCallback(const std::string& objectName) {
if (!impl) {
std::cerr << "[SCRIPT] impl is null!" << std::endl;
return;
}
sol::state& lua = impl->lua;
// Try to find custom activate function first
sol::function activateFunc = lua["on_item_pickup"];
if (activateFunc.valid()) {
std::cout << "[SCRIPT] Callback found! Calling with argument: " << objectName << std::endl;
auto result = activateFunc(objectName);
if (!result.valid()) {
sol::error err = result;
std::cerr << "[SCRIPT] on_item_pickup callback error: " << err.what() << "\n";
}
else {
std::cout << "[SCRIPT] Callback executed successfully!" << std::endl;
}
}
else {
std::cout << "[SCRIPT] Fallback: on_item_pickup not found" << std::endl;
}
}
void ScriptEngine::showInventory(Game* game) {
std::cout << "[script] toggle_inventory called" << std::endl;
@@ -157,7 +214,6 @@ namespace ZL {
game->inventoryOpen = !isVisible;
// Update UI with current items only if showing
if (!isVisible) {
const auto& items = game->inventory.getItems();
std::string itemText;
@@ -176,31 +232,4 @@ namespace ZL {
}
}
void ScriptEngine::callItemPickupCallback(const std::string& objectName) {
if (!impl) {
std::cerr << "[SCRIPT] impl is null!" << std::endl;
return;
}
sol::state& lua = impl->lua;
std::cout << "[SCRIPT] Looking for callback: on_item_pickup" << std::endl;
sol::function pickup = lua["on_item_pickup"];
if (pickup.valid()) {
std::cout << "[SCRIPT] Callback found! Calling with argument: " << objectName << std::endl;
auto result = pickup(objectName);
if (!result.valid()) {
sol::error err = result;
std::cerr << "[SCRIPT] on_item_pickup callback error: " << err.what() << "\n";
}
else {
std::cout << "[SCRIPT] Callback executed successfully!" << std::endl;
}
}
else {
std::cerr << "[SCRIPT] on_item_pickup callback not found!" << std::endl;
}
}
} // namespace ZL