如何将基类也注册到lua中? #43
Answered
by
LanderlYoung
ThisisGame
asked this question in
Q&A
-
#include <iostream>
#include <ScriptX/ScriptX.h>
using namespace script;
std::shared_ptr<script::ScriptEngine> createEngine() {
#if !defined(SCRIPTX_BACKEND_WEBASSEMBLY)
return std::shared_ptr<script::ScriptEngine>{new script::ScriptEngineImpl(),
script::ScriptEngine::Deleter()};
#else
return std::shared_ptr<script::ScriptEngine>{script::ScriptEngineImpl::instance(), [](void*) {}};
#endif
}
class Component:public script::ScriptClass{
public:
using script::ScriptClass::ScriptClass;
Component(): script::ScriptClass(script::ScriptClass::ConstructFromCpp<Component>{}) {}
virtual void Awake(){
std::cout<<"Component Awake"<<std::endl;
};
};
class Camera:public Component,public script::ScriptClass//warning: direct base 'script::ScriptClass' inaccessible in 'Camera' due to ambiguity [-Winaccessible-base]
{
public:
using script::ScriptClass::ScriptClass;//error: cannot inherit constructors from indirect base 'script::ScriptClass'
Camera(): script::ScriptClass(script::ScriptClass::ConstructFromCpp<Camera>{}) {}
void test_set(int a){
std::cout<<a<<std::endl;
}
};
int main() {
auto engine = createEngine();
script::EngineScope enter(engine.get());
// 测试c++继承
{
const auto componentDefine =
script::defineClass<Component>("Component")
.nameSpace("UnityEngine")
.constructor()
.instanceFunction("Awake", &Component::Awake)
.build();
engine->registerNativeClass<Component>(componentDefine);
const auto cameraDefine =
script::defineClass<Camera>("Camera")
.nameSpace("UnityEngine")
.constructor()
.instanceFunction("test_set", &Camera::test_set)
.instanceFunction("Awake", &Camera::Awake)
.build();
engine->registerNativeClass<Camera>(cameraDefine);
}
return 0;
}
|
Beta Was this translation helpful? Give feedback.
Answered by
LanderlYoung
Sep 21, 2021
Replies: 2 comments 1 reply
-
似乎你对C++不太熟悉,Component继承了ScriptClass,Camera继承了Component然后又继承了ScriptClass。 lua内部要继承的话,可以从纯Lua代码的metatable角度去实现继承,和继承普通的lua类一样 |
Beta Was this translation helpful? Give feedback.
1 reply
-
建议看看函数注释和详细的项目文档吧 看一下你的这段代码 class Camera:public Component,public script::ScriptClass
{
public:
using script::ScriptClass::ScriptClass;
Camera():Component(),script::ScriptClass(script::ScriptClass::ConstructFromCpp<Camera>{}) {}
};
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
LanderlYoung
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
建议看看函数注释和详细的项目文档吧
看一下你的这段代码
script::ScriptClass(script::ScriptClass::ConstructFromCpp<Camera>{})
不是这么用的,文档里其实强调的很强烈了。