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,volatileandstatic- references and pointers
- both
clientandcheerpnamespaces - Inheriting from
Node/Object int32_tvsdoublevsfloatas a type
I seem to get either:
NaN01- fatal type handling errors in the compiled code
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
clientnamespace should have no member fields.To access external JS objects properties you need to add methods starting with
get_andset_, to respectively read and write to the property:Also, you don't need to use volatile here.