Cheerp is a C++ to js/wasm transpiler. Screeps is a programming videogame.
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
and static
client
and cheerp
namespacesNode
/Object
int32_t
vs double
vs float
as a typeI seem to get either:
NaN
0
1
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...
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
.
Classes declared in the client
namespace should have no member fields.
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;
}
Also, you don't need to use volatile here.