DarkflameServer/dLua/dLua.h
wincent 5f689104af Initial work on LUA scripting.
This LUA scripting implementation uses the sol header only C++ wrapper for the native LUA libraries.

The API does not follow the original LU scripting. Each instance of a script is in its own LUA-State, and has 'self' as a global. There are hooks to all the implemented CppScript methods, as well as a subset of the entity functionallity. Has to be expanded upon.

This is local work which has been sitting for awhile; thought someone might like to take a look at it.
2022-04-13 19:51:14 +02:00

61 lines
1.6 KiB
C++

#pragma once
#include "sol/sol.hpp"
#include "dCommonVars.h"
class Entity;
namespace dLua
{
void LoadScript(Entity* entity, const std::string& name);
Entity* GetEntity(const sol::state& lua, const std::string& key);
Entity* GetEntityOrThrow(const sol::state& lua, const std::string& key);
Entity* GetEntity(const LWOOBJID& id);
Entity* GetEntityOrThrow(const LWOOBJID& id);
Entity* GetEntity(sol::object object);
Entity* GetEntityOrThrow(sol::object object);
LWOOBJID GetEntityID(sol::object object);
LWOOBJID GetEntityIDOrThrow(sol::object object);
template<typename T>
T TableGet(const sol::table& table, const std::string& key)
{
sol::object object = table[key];
if (!object.valid())
throw std::runtime_error("TableGet: Missing key");
if (!object.is<T>())
throw std::runtime_error("TableGet: Invalid key");
return object.as<T>();
}
template<typename T>
T TableGet(const sol::table& table, const std::string& key, const T& defaultValue)
{
sol::object object = table[key];
if (!object.valid())
return defaultValue;
if (!object.is<T>())
throw std::runtime_error("TableGet: Invalid key");
return object.as<T>();
}
template<typename T>
bool TryTableGet(const sol::table& table, const std::string& key, T& out)
{
sol::object object = table[key];
if (!object.valid())
return false;
if (!object.is<T>())
throw std::runtime_error("TableGet: Invalid key");
out = object.as<T>();
return true;
}
}