Skip to content

Commit

Permalink
Lua can access attached components on objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Mormert committed Jul 20, 2023
1 parent 1468e13 commit 5786344
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions engine/cLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ cLight::editorGizmosRender(bool selected)
}

void
cLight::registerLua(sol::state& lua, sol::table &table)
cLight::registerLua(sol::state &lua, sol::table &table)
{
lua.new_usertype<cLight>("cLight", "color", &cLight::_color);
lua.new_usertype<cLight>("cLight", sol::base_classes, sol::bases<jleComponent>(), "color", &cLight::_color);
}
6 changes: 6 additions & 0 deletions engine/cLightDirectional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ cLightDirectional::editorGizmosRender(bool selected)
gEngine->rendering().rendering3d().sendMesh(mesh, material, getTransform().getWorldMatrix(), _attachedToObject->instanceID(), false);
#endif // BUILD_EDITOR
}

void
cLightDirectional::registerLua(sol::state &lua, sol::table &table)
{

}
2 changes: 2 additions & 0 deletions engine/cLightDirectional.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class cLightDirectional : public cLight
ar(cereal::base_class<cLight>(this));
}

void registerLua(sol::state& lua, sol::table &table) override;

void update(float dt) override;

void editorGizmosRender(bool selected) override;
Expand Down
4 changes: 3 additions & 1 deletion engine/jleComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class jleComponent

[[nodiscard]] virtual std::shared_ptr<jleComponent> clone() const = 0;

virtual void registerSelfLua(sol::table &self) = 0;

template <class Archive>
void
serialize(Archive &archive)
Expand Down Expand Up @@ -64,7 +66,7 @@ class jleComponent
}

virtual void
registerLua(sol::state& lua, sol::table &table)
registerLua(sol::state &lua, sol::table &table)
{
}

Expand Down
3 changes: 3 additions & 0 deletions engine/jleLuaScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ jleLuaScript::setupLua(sol::table &self, jleObject *ownerObject)
if (result.valid()) {
self = result;
self["object"] = ownerObject;
for (auto &c : ownerObject->customComponents()) {
c->registerSelfLua(self);
}
} else {
sol::error err = result;
LOGE << "Failed starting script: " << err.what();
Expand Down
1 change: 1 addition & 0 deletions engine/jleTypeReflectionUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public:
\
public: \
std::shared_ptr<jleComponent> clone() const override { return std::make_shared<component_name>(*this); } \
void registerSelfLua(sol::table &self) override { self[componentName()] = this; } \
\
private:

Expand Down

0 comments on commit 5786344

Please sign in to comment.