FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat(NativeScripting): add Native Scripting plugin by Divengerss · Pull Request #95 · EngineSquared/EngineSquared · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
aa8e8ca
feat: Native scripting plugin
Divengerss Feb 14, 2025
2317236
style: apply linter
github-actions[bot] Feb 14, 2025
a1ee584
fix(NativeScripting): Missing registry assignment from Bind function
Divengerss Feb 16, 2025
898eb18
Merge branch '78-add-native-scripting-plugin' of github.com:EngineSqu…
Divengerss Feb 16, 2025
74df4a9
style: apply linter
github-actions[bot] Feb 16, 2025
0af508c
fix(NativeScripting): Avoiding registry reference in scripts
Divengerss Feb 18, 2025
512a4ad
fix(NativeScripting): No match for * operator from ScriptableEntity c…
Divengerss Feb 18, 2025
face4cc
feat(NativeScripting): Unit Tests & renaming resource to utils
Divengerss Feb 19, 2025
968e153
style: apply linter
github-actions[bot] Feb 19, 2025
19a7fc0
fix(NativeScripting): Compliance with coding style
Divengerss Feb 19, 2025
92bb0ee
Merge branch '78-add-native-scripting-plugin' of github.com:EngineSqu…
Divengerss Feb 19, 2025
21326f0
style: apply linter
github-actions[bot] Feb 19, 2025
913b2bd
fix(NativeScripting): Unit Test compilation error due to unique ptr
Divengerss Feb 19, 2025
2c31695
Merge branch '78-add-native-scripting-plugin' of github.com:EngineSqu…
Divengerss Feb 19, 2025
66cdeff
style: apply linter
github-actions[bot] Feb 19, 2025
ac66303
refactor(NativeScripting): fixing requested changes
Divengerss Feb 20, 2025
0543bd1
Merge branch '78-add-native-scripting-plugin' of github.com:EngineSqu…
Divengerss Feb 20, 2025
d9c4eae
style: apply linter
github-actions[bot] Feb 20, 2025
ac33103
refactor(NativeScripting): remove unused instantiation from tests
Divengerss Feb 20, 2025
3da3d19
Merge branch '78-add-native-scripting-plugin' of github.com:EngineSqu…
Divengerss Feb 20, 2025
9fdb89d
Merge branch 'main' into 78-add-native-scripting-plugin
Miou-zora Feb 20, 2025
dde7d6d
fix(NativeScripting): renaming registry to core
Divengerss Feb 20, 2025
8deb005
style: apply linter
github-actions[bot] Feb 20, 2025
122d7cd
fix(NativeScripting): init entities before running logic
Miou-zora Feb 20, 2025
File filter

Filter by extension

Filter by extension .cpp  (3) .hpp  (3) .lua  (2) dotfile  (1) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 2 additions & 0 deletions src/plugin/native-scripting/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.xmake/*
build/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include "ScriptableEntity.hpp"

namespace ES::Plugin::NativeScripting::Component {
/**
* Component used to allow native scripting for entities.
* Implementation is well explained and inspired from this Youtube video made by @TheCherno
* https://www.youtube.com/watch?v=iIUhg88MK5M&t=901s&pp=ygUbbmF0aXZlIHNjcmlwdGluZyB0aGUgY2hlcm5v
*/
struct NativeScripting {
std::unique_ptr<ES::Plugin::NativeScripting::Utils::ScriptableEntity> seInstance = nullptr;

std::function<void()> Instantiate;
std::function<void()> DestroyInstance;

std::function<void(ES::Plugin::NativeScripting::Utils::ScriptableEntity *)> OnCreate;
std::function<void(ES::Plugin::NativeScripting::Utils::ScriptableEntity *)> OnDestroy;
std::function<void(ES::Plugin::NativeScripting::Utils::ScriptableEntity *)> OnUpdate;

template <typename T> void Bind(ES::Engine::Core &core)
{
Instantiate = [this]() { seInstance = std::make_unique<T>(); };
DestroyInstance = [this]() { seInstance.reset(); };

OnCreate = [&core](ES::Plugin::NativeScripting::Utils::ScriptableEntity *instance) {
static_cast<T *>(instance)->OnCreate(core);
};
OnDestroy = [](ES::Plugin::NativeScripting::Utils::ScriptableEntity *instance) {
static_cast<T *>(instance)->OnDestroy();
};
OnUpdate = [&core](ES::Plugin::NativeScripting::Utils::ScriptableEntity *instance) {
static_cast<T *>(instance)->OnUpdate(core);
};
}
};
} // namespace ES::Plugin::NativeScripting::Component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "ScriptingSystem.hpp"

void ES::Plugin::NativeScripting::System::UpdateScripts(ES::Engine::Core &core)
{
core.GetRegistry().view<ES::Plugin::NativeScripting::Component::NativeScripting>().each(
[&core](auto entity, auto &nsComponent) {
if (!nsComponent.seInstance.get())
{
nsComponent.Instantiate();
nsComponent.seInstance->entity = entity;
nsComponent.OnCreate(nsComponent.seInstance.get());
}

nsComponent.OnUpdate(nsComponent.seInstance.get());
});
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "Engine.hpp"

#include "NativeScripting.hpp"

namespace ES::Plugin::NativeScripting::System {
void UpdateScripts(ES::Engine::Core &core);
} // namespace ES::Plugin::NativeScripting::System
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "Entity.hpp"

namespace ES::Plugin::NativeScripting::Utils {
class ScriptableEntity {
public:
template <typename T> T &GetComponent(ES::Engine::Core &core) { return entity.GetComponents<T>(core); }

ES::Engine::Entity entity;
};
} // namespace ES::Plugin::NativeScripting::Utils
62 changes: 62 additions & 0 deletions src/plugin/native-scripting/tests/ScriptTest.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <gtest/gtest.h>

#include "Core.hpp"
#include "Entity.hpp"
#include "NativeScripting.hpp"
#include "ScriptingSystem.hpp"

class speedManager : public ES::Plugin::NativeScripting::Utils::ScriptableEntity {
public:
void OnCreate([[maybe_unused]] const ES::Engine::Core &core) { std::cout << "OnCreate called" << std::endl; }

void OnUpdate(ES::Engine::Core &core)
{
auto view = core.GetRegistry().view<float>();

for (auto entity : view)
{
auto &speed = view.get<float>(entity);
speed = 3.0f;
}
}

void OnDestroy()
{
// Left empty as OnDestroy does not perform anything here
}
};

static void InitPlayer(ES::Engine::Core &core)
{
float speed = 1.0f;

ES::Engine::Entity playerEntity(core.CreateEntity());
auto &scriptComponent = playerEntity.AddComponent<ES::Plugin::NativeScripting::Component::NativeScripting>(core);

scriptComponent.Bind<speedManager>(core);

core.GetRegistry().emplace<float>(playerEntity, speed);
}

TEST(NativeScripting, speedManagerScript)
{
ES::Engine::Core core;

core.RegisterSystem<ES::Engine::Scheduler::Startup>(InitPlayer);
core.RunSystems();

core.RegisterSystem<ES::Engine::Scheduler::Update>(ES::Plugin::NativeScripting::System::UpdateScripts);
testing::internal::CaptureStdout();

core.RunSystems();

std::string output = testing::internal::GetCapturedStdout();
EXPECT_EQ(output, "OnCreate called\n");

auto view = core.GetRegistry().view<float>();
for (auto entity : view)
{
auto speed = view.get<float>(entity);
EXPECT_FLOAT_EQ(speed, 3.0f) << "Speed should be 3.0f after OnUpdate()";
}
}
7 changes: 7 additions & 0 deletions src/plugin/native-scripting/tests/main.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <gtest/gtest.h>

int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
46 changes: 46 additions & 0 deletions src/plugin/native-scripting/xmake.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
add_rules("mode.debug", "mode.release")
add_requires("entt")

includes("../../engine/xmake.lua")

target("PluginNativeScripting")
set_kind("static")
set_languages("cxx20")
add_packages("entt")
set_policy("build.warning", true)

add_deps("EngineSquaredCore")

add_files("src/**.cpp")
add_includedirs("src", {public = true})
add_includedirs("src/component", {public = true})
add_includedirs("src/utils", {public = true})
add_includedirs("src/system", {public = true})

for _, file in ipairs(os.files("tests/**.cpp")) do
local name = path.basename(file)
if name == "main" then
goto continue
end
target(name)
set_kind("binary")
if is_plat("linux") then
add_cxxflags("--coverage", "-fprofile-arcs", "-ftest-coverage", {force = true})
add_ldflags("--coverage")
end
set_default(false)
set_languages("cxx20")
add_links("gtest")
add_tests("default")
add_packages("glm", "entt", "gtest")

add_deps("PluginNativeScripting")
add_deps("EngineSquaredCore")

add_files(file)
add_files("tests/main.cpp")
if is_mode("debug") then
add_defines("DEBUG")
end
::continue::
end
2 changes: 2 additions & 0 deletions xmake.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ includes("src/plugin/camera/xmake.lua")
includes("src/plugin/colors/xmake.lua")
includes("src/plugin/input/xmake.lua")
includes("src/plugin/math/xmake.lua")
includes("src/plugin/native-scripting/xmake.lua")
includes("src/plugin/object/xmake.lua")
includes("src/plugin/physics/xmake.lua")
includes("src/plugin/scene/xmake.lua")
Expand Down Expand Up @@ -34,6 +35,7 @@ target("EngineSquared")
add_deps("PluginUtils")
add_deps("PluginVkWrapper")
add_deps("PluginWindow")
add_deps("PluginNativeScripting")
add_deps("UtilsLog")

set_policy("build.warning", true)
Expand Down

Back | FazBrowse Home | New Git URL