Skip to content

Commit

Permalink
lua basic script execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Mormert committed Jul 15, 2023
1 parent e62fd0b commit e74c8ef
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
5 changes: 4 additions & 1 deletion engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ add_library(engine STATIC
"jleExplicitInclude.cpp"
"jlePhysics.cpp"
"jlePhysicsDebugDrawer.cpp"
"cRigidbody.cpp")
"cRigidbody.cpp"
"cLuaScript.cpp")

if (BUILD_EDITOR)
target_sources(engine PRIVATE
Expand Down Expand Up @@ -128,6 +129,8 @@ target_include_directories(engine SYSTEM PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/3rd

target_link_libraries(engine PUBLIC zlib)

add_subdirectory(3rdparty/lua)
target_link_libraries(engine PUBLIC vanilla_lua)

if (NOT BUILD_EMSCRIPTEN) # Native build, excluding some Emscripten-embedded libraries else()

Expand Down
34 changes: 34 additions & 0 deletions engine/cLuaScript.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2023. Johan Lind

#include "cLuaScript.h"

cLuaScript::cLuaScript(jleObject *owner, jleScene *scene) : jleComponent(owner, scene) {}

void
cLuaScript::start()
{
_lua = std::make_shared<sol::state>();

_lua->open_libraries(sol::lib::base,
sol::lib::math,
sol::lib::string,
sol::lib::coroutine,
sol::lib::package,
sol::lib::debug,
sol::lib::io,
sol::lib::table,
sol::lib::os);

_lua->set_function("LOGE", [](std::string a) { LOGE << a; });

try {
_lua->script_file(_scriptPath.getRealPath());
} catch (std::exception &e) {
LOGE << "Error running lua script:" << e.what();
}
}

void
cLuaScript::update(float dt)
{
}
32 changes: 32 additions & 0 deletions engine/cLuaScript.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2023. Johan Lind

#pragma once

#include "jleComponent.h"
#include <sol2/sol.hpp>

class cLuaScript : public jleComponent
{
JLE_REGISTER_COMPONENT_TYPE(cLuaScript)
public:
explicit cLuaScript(jleObject *owner = nullptr, jleScene *scene = nullptr);

template <class Archive>
void
serialize(Archive &ar)
{
ar(CEREAL_NVP(_scriptPath));
}

void start() override;

void update(float dt) override;

protected:
jlePath _scriptPath;

std::shared_ptr<sol::state> _lua;
};

CEREAL_REGISTER_TYPE(cLuaScript)
CEREAL_REGISTER_POLYMORPHIC_RELATION(jleComponent, cLuaScript)

0 comments on commit e74c8ef

Please sign in to comment.