如何与Cheerp/js中的外部变量交互? [英] How do I interface with extern variables from Cheerp/js?
问题描述
Cheerp 是js/wasm Transpiler的C ++. Screeps 是一款编程视频游戏.
Cheerp is a C++ to js/wasm transpiler. Screeps is a programming videogame.
如何阅读 Game.time
我已编译的C ++代码中的变量?(在蠕动中)
How do I read in the Game.time
variable from my transpiled C++ code? (in screeps)
#include <cheerp/client.h>
#include <iostream>
using namespace std;
namespace client {
class Game : public Object {
public:
static volatile double time;
};
extern volatile Game &Game;
}
void webMain() {
cout << __TIME__ << ": The current time is: " << client::Game.time << endl;
}
我尝试了多种变体形式:
I have tried any number of variations on:
-
extern
,volatile
和static
- 引用和指针
-
client
和cheerp
命名空间 - 从
Node
/Object
继承 -
int32_t
与double
与float
作为类型
extern
,volatile
andstatic
- references and pointers
- both
client
andcheerp
namespaces - Inheriting from
Node
/Object
int32_t
vsdouble
vsfloat
as a type
我似乎得到了其中一个:
I seem to get either:
-
NaN
-
0
-
1
- 已编译代码中的致命类型处理错误
如何在我的C ++代码中正确地与Javascript对象和变量交互?至少可以说,cheerp文档非常稀少...
How do I correctly interface with Javascript Objects and variables from within my C++ code? The cheerp documentation is VERY sparse to say the least...
注意:cheerp从未真正生成正确的Javascript.关于 Game
对象的处理方式始终存在一些不一致之处,并且在许多情况下,它错误地尝试将 Game.d
索引为数组而不是 Game.时间
.
Note: cheerp is never actually generating the proper Javascript. There's always some inconsistency as to how the Game
object is handled and in a lot of scenarios it's incorrectly trying to index Game.d
as an array instead of Game.time
.
推荐答案
在 client
名称空间中声明的类应该没有成员字段.
Classes declared in the client
namespace should have no member fields.
要访问外部JS对象的属性,您需要添加以 get _
和 set _
开头的方法,以分别读取和写入该属性:
To access external JS objects properties you need to add methods starting with get_
and set_
, to respectively read and write to the property:
#include <cheerp/client.h>
#include <iostream>
using namespace std;
namespace client {
class Game : public Object {
public:
double get_time();
};
extern Game &Game;
}
void webMain() {
cout << __TIME__ << ": The current time is: " << client::Game.get_time() << endl;
}
此外,您无需在此处使用volatile.
Also, you don't need to use volatile here.
这篇关于如何与Cheerp/js中的外部变量交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!