我试图以下面的方式进行操作,但它不起作用,它返回一个怪异的对象。 我的C ++中的Code Bellow:
#include <emscripten/bind.h>
#include <vector>
using Player = int;
using Action = int64_t;
struct PlayerAction {
Player player;
Action action;
bool operator==(const PlayerAction& other) const {
return player == other.player && action == other.action;
}
};
std::vector<PlayerAction> getAction() {
std::vector<PlayerAction> action;
PlayerAction action1 = {0, 2};
PlayerAction action2 = {1, 4};
action.push_back(action1);
action.push_back(action2);
return action;
}
// Bindings
EMSCRIPTEN_BINDINGS(my_module) {
emscripten::value_object<PlayerAction>("PlayerAction")
.field("player", &PlayerAction::player)
.field("action", &PlayerAction::action);
emscripten::function("getAction", &getAction);
emscripten::register_vector<PlayerAction>("vector<PlayerAction>");
}
HTML和JS:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Exemplo Emscripten</title>
</head>
<body>
<script>
var Module = {
onRuntimeInitialized: function() {
console.log(Module.getAction());
}
}
</script>
<script src="exemplo.js"></script>
</body>
</html>
现在这是一个非常古老的问题,但是答案是您要获得的Emscripten对象不是JavaScript数组,而是一种特殊的向量对象,您需要使用get()访问元素。