Skip to content

Instantly share code, notes, and snippets.

@Petethegoat
Created April 26, 2020 00:55
Show Gist options
  • Save Petethegoat/90c8f6cfbcfa0945d3adf1cbf90d7433 to your computer and use it in GitHub Desktop.
Save Petethegoat/90c8f6cfbcfa0945d3adf1cbf90d7433 to your computer and use it in GitHub Desktop.
#include "TES3Birthsign.h"
#include "LuaManager.h"
#include "TES3ObjectLua.h"
namespace mwse {
namespace lua {
void bindTES3Birthsign() {
// Get our lua state.
auto stateHandle = LuaManager::getInstance().getThreadSafeStateHandle();
sol::state& state = stateHandle.state;
// Start our usertype. We must finish this with state.set_usertype.
auto usertypeDefinition = state.create_simple_usertype<TES3::Birthsign>();
usertypeDefinition.set("new", sol::no_constructor);
// Define inheritance structures. These must be defined in order from top to bottom. The complete chain must be defined.
usertypeDefinition.set(sol::base_classes, sol::bases<TES3::BaseObject>());
setUserdataForBaseObject(usertypeDefinition);
// Basic property binding.
usertypeDefinition.set("name", sol::readonly_property(&TES3::Birthsign::getName));
usertypeDefinition.set("texturePath", &TES3::Birthsign::texturePath);
usertypeDefinition.set("spells", sol::readonly_property(&TES3::Birthsign::spellList));
// Description may need to be loaded from disk, handle it specially.
usertypeDefinition.set("description", sol::readonly_property(
[](TES3::Birthsign& self) -> sol::object
{
auto& luaManager = mwse::lua::LuaManager::getInstance();
auto stateHandle = luaManager.getThreadSafeStateHandle();
sol::state& state = stateHandle.state;
// If the description is already loaded, just return it.
if (self.description) {
return sol::make_object(state, self.description);
}
// Otherwise we need to load it from disk, then free it.
else {
char* description = self.loadDescription();
if (description) {
// We loaded successfully, package, free, then return.
sol::object value = sol::make_object(state, description);
self.freeDescription();
return value;
}
}
return sol::nil;
}));
// Finish up our usertype.
state.set_usertype("tes3birthsign", usertypeDefinition);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment