Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 tasks done
chengdz opened this issue Oct 17, 2024 · 1 comment
Open
3 tasks done

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

chengdz opened this issue Oct 17, 2024 · 1 comment
Assignees
Labels
bug Something isn't working Unreal

Comments

@chengdz
Copy link

chengdz commented Oct 17, 2024

前置阅读 | 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 错误

@chengdz chengdz added bug Something isn't working Unreal labels Oct 17, 2024
@chexiongsheng
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Unreal
Projects
None yet
Development

No branches or pull requests

2 participants