Skip to content

Commit

Permalink
Lua "class system" for serialization of Lua types and Lua component s…
Browse files Browse the repository at this point in the history
…cript execution, some refactor
  • Loading branch information
Mormert committed Mar 24, 2024
1 parent 15f319a commit 900a4b7
Show file tree
Hide file tree
Showing 16 changed files with 1,187 additions and 617 deletions.
3 changes: 2 additions & 1 deletion engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ add_library(engine STATIC
"jle3DRenderer.cpp"
"jle3DGraph.cpp"
"cCameraFPV.cpp"
"jleLuaClass.cpp"
"jleMesh.cpp"
"cMesh.cpp"
"jleSkybox.cpp"
Expand All @@ -103,7 +104,6 @@ add_library(engine STATIC
"cRigidbody.cpp"
"cLuaScript.cpp"
"jleLuaScript.cpp"
"jleLuaScriptComponent.cpp"
"jleFileIndexer.cpp"
"jleLuaEnvironment.cpp"
"jleGLError.cpp"
Expand All @@ -121,6 +121,7 @@ add_library(engine STATIC
if (JLE_BUILD_EDITOR)
target_sources(engine PRIVATE
"editor/jleEditor.cpp"
"editor/jleImGuiCerealArchive.cpp"
"editor/jleEditorResourceViewer.cpp"
"editor/jleEditorResourceViewer.cpp"
"editor/jleEditorSettingsWindow.cpp"
Expand Down
106 changes: 50 additions & 56 deletions engine/cLuaScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
#include "jleGameEngine.h"
#include "jleLuaEnvironment.h"

#if JLE_BUILD_EDITOR
#include "editor/jleImGuiExtensions.h"
#include "editor/jleEditor.h"
#endif

#if JLE_BUILD_IMGUI
#include <ImGui/imgui.h>
#endif
Expand All @@ -31,48 +26,32 @@ JLE_EXTERN_TEMPLATE_CEREAL_CPP(cLuaScript)
void
cLuaScript::start()
{
if (!_scriptRef) {
LOGE << "Can't start script since there is a reference issue";
runUpdate = false;
return;
}

_scriptRef->setupLua(_self, _attachedToObject);

if (!_specializationScript.empty()) {
sol::load_result fx = gEngine->luaEnvironment()->getState().load(_specializationScript);
if (!fx.valid()) {
sol::error err = fx;
LOGE << "Failed to load specialization script for " << _attachedToObject->instanceName() << ": "
<< err.what();
}

fx(_self);
if (!_isInitialized) {
initializeLuaComponent();
}

_scriptRef->startLua(_self);
}

void
cLuaScript::update(float dt)
{
if (runUpdate) {
try {
_scriptRef->updateLua(_self, dt);
} catch (std::exception &e) {
LOGE << "Error running lua update: " << e.what();
runUpdate = false;
}
const auto luaClass = gEngine->luaEnvironment()->getState()[_luaClass.luaClassName];

try {
sol::protected_function updateFunc = luaClass["update"];
updateFunc(_self, dt);
} catch (std::exception &e) {
LOGE << "Error running lua update: " << e.what();
}
}

void
cLuaScript::onDestroy()
{
const auto luaClass = gEngine->luaEnvironment()->getState()[_luaClass.luaClassName];

try {
if (_scriptRef) {
_scriptRef->onDestroyLua(_self);
}
sol::protected_function destroyFunc = luaClass["destroy"];
destroyFunc(_self);
} catch (std::exception &e) {
LOGE << "Error running lua destroy: " << e.what();
}
Expand All @@ -85,31 +64,46 @@ cLuaScript::getSelf()
}

void
cLuaScript::editorInspectorImGuiRender()
cLuaScript::initializeLuaComponent()
{
#if JLE_BUILD_IMGUI
if (!gEngine->isGameKilled()) {

ImGui::BeginGroupPanel("Lua Variables");

try {
sol::protected_function f = gEditor->luaEnvironment()->getState()["luaEditor"]["tableImGui"];
if (f.valid()) {
auto res = f(getSelf());
if (!res.valid()) {
sol::error err = res;
ImGui::Text("Lua Error: %s", err.what());
}
}
} catch (std::exception &e) {
ImGui::Text("Lua Error: %s", e.what());
const auto luaClass = gEngine->luaEnvironment()->getState()[_luaClass.luaClassName];

gEngine->luaEnvironment()->loadedLuaClasses()[_luaClass.luaClassName];

try{
sol::protected_function classConstructor = luaClass["new"];
if (classConstructor.valid()) {
_self = classConstructor(luaClass);
_self["object"] = object();

_isInitialized = true;
} else {
LOGE << "Failed to initialize Lua class component on cLuaScript " << _luaClass.luaClassName;
_isInitialized = false;
}
}catch(std::exception&e){
LOGE << "Failed to initialize Lua class component on cLuaScript " << _luaClass.luaClassName << " reason: " << e.what();
_isInitialized = false;
}

}

template <class Archive>
void
cLuaScript::serialize(Archive &ar)
{
try {
ar(CEREAL_NVP(_luaClass));

ImGui::EndGroupPanel();
if (!_isInitialized) {
initializeLuaComponent();
}

}else
{
ImGui::Text("Start the game to see Lua variables");
auto it = gEngine->luaEnvironment()->loadedLuaClasses().find(_luaClass.luaClassName);
if (it != gEngine->luaEnvironment()->loadedLuaClasses().end()) {
it->second.serializeClass(ar, _self);
}
} catch (std::exception &e) {
LOGE << "Failed to serialize cLuaScript";
}
#endif
}
25 changes: 8 additions & 17 deletions engine/cLuaScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,35 @@
#include "jleBuildConfig.h"

#include "jleComponent.h"
#include "jleLuaScriptComponent.h"
#include "jleLuaClassSerialization.h"
#include "jleLuaScript.h"
#include "jleResourceRef.h"

class cLuaScript : public jleComponent
{
JLE_REGISTER_COMPONENT_TYPE(cLuaScript)
public:

template <class Archive>
void
serialize(Archive &ar)
{
ar( CEREAL_NVP(_scriptRef), CEREAL_NVP(_specializationScript));
}
void serialize(Archive &ar);

void start() override;

void update(float dt) override;

void onDestroy() override;

sol::table& getSelf();
sol::table &getSelf();

bool runUpdate = true;
private:
void initializeLuaComponent();
bool _isInitialized{false};

void editorInspectorImGuiRender() override;
jleLuaClassSerialization _luaClass;

private:
jleResourceRef<jleLuaScriptComponent> _scriptRef;
std::string _specializationScript = "local self = ...;\n";
sol::table _self;

std::function<void(sol::table, float)> _updateLua;
std::function<void(sol::table)> _onDestroyLua;
};

JLE_EXTERN_TEMPLATE_CEREAL_H(cLuaScript)


CEREAL_REGISTER_TYPE(cLuaScript)
CEREAL_REGISTER_POLYMORPHIC_RELATION(jleComponent, cLuaScript)
3 changes: 2 additions & 1 deletion engine/editor/jleEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ jleEditor::updateEditorLoadedScenes(float dt)
void
jleEditor::update(float dt)
{
_fileIndexer->periodicSweep();
_fileIndexer->periodicSweepThreaded();
_luaEnvironment->loadNewlyAddedScripts();
jleGameEngine::update(dt);
if (isGameKilled()) {
JLE_SCOPE_PROFILE_CPU(updateEditorLoadedScenes)
Expand Down
Loading

0 comments on commit 900a4b7

Please sign in to comment.