Tencent / puerts

PUER(普洱) Typescript. Let's write your game in UE or Unity with TypeScript.
Other
5.07k stars 707 forks source link

[UE] Bug: 如何向下转型 #1866

Open chengdz opened 3 weeks ago

chengdz commented 3 weeks ago

前置阅读 | Pre-reading

Puer的版本 | Puer Version

0.2.6

UE的版本 | UE Version

0.2.6

发生在哪个平台 | Platform

All

错误信息 | Error Message

C++层代码:

class Person { public: Person() {} virtual ~Person() {} };

class Student : public Person { public: Student() {} virtual ~Student() {} public: void study() {} };

class School { public: School() {} ~School() {} public: void addPerson(Person pPerson) { m_persons.push_back(pPerson); } Person getPerson(int index) { return m_persons[index]; } private: std::vector<Person*> m_persons; };

// module declaration begin

UsingCppType(Person); UsingCppType(Student); UsingCppType(School);

static void Init() {

puerts::DefineClass<Person>()
    .Constructor()
    .Register();

puerts::DefineClass<Student>()
    .Constructor()
    .Method("study", MakeFunction(&Student::study))
    .Register();

puerts::DefineClass<School>()
    .Constructor()
    .Method("addPerson", MakeFunction(&School::addPerson))
    .Method("getPerson", MakeFunction(&School::getPerson))
    .Register();

}

PESAPI_MODULE(puertsTest, Init)

JS层代码:

let _school = new School();

let _student = new Student();

_school.addPerson(_student);

let temp = _school.getPerson(0); temp.study();

这种情况会报:Uncaught TypeError TypeError: temp.study is not a function 错误

问题重现 | Bug reproduce

这种情况会报:Uncaught TypeError TypeError: temp.study is not a function 错误

chexiongsheng commented 3 weeks ago

c++类型不一定能动态类型识别(得有虚函数而且编译没加-fno-rtti)。 所以并不能自动处理这种情况。 所以得你手动处理。 通过Object.setPrototypeOf。