So I am just starting to use GDNative with C++. I would also like to know how I can use a string in my code. I have compiled the godot-cpp from Github. here is my code:
//main.cpp
#include "Interpreter.h"
extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options * o) {
godot::Godot::gdnative_init(o);
}
extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options * o) {
godot::Godot::gdnative_terminate(o);
}
extern "C" void GDN_EXPORT godot_nativescript_init(void* handle) {
godot::Godot::nativescript_init(handle);
godot::register_class<godot::Interpreter>();
}
//Interpreter.h
#ifndef INTERPRETER_H
#define INTERPRETER_H
#include <Godot.hpp>
#include <Node.hpp>
//#include <string>
//#include <string.h>
#include <Python.h>
#include <vector>
#include <numpy/arrayobject.h>
//#include <Variant.hpp>
namespace godot {
class Interpreter : public Node
{
public:
static void _register_methods();
Interpreter();
~Interpreter();
//std::string code;
void runcode();
void _init();
void _process();
private:
bool isRunning = false;
};
}
#endif
//Interpreter.cpp
#include "Interpreter.h"
using namespace godot;
PyObject* myfuzz(PyObject* self, PyObject* args) {
if (!PyArg_ParseTuple(args, ":fuzz"))
return NULL;
return Py_None;
}
PyObject* PyInit_emb() {
static PyMethodDef EmbMethods[] = {
{"fuzz", myfuzz, METH_VARARGS, "Make fuzz sound"},
{NULL, NULL, 0, NULL}};
static PyModuleDef EmbModule = {
PyModuleDef_HEAD_INIT, "AEInternal", NULL, -1, EmbMethods,
NULL, NULL, NULL, NULL
};
return PyModule_Create(&EmbModule);
}
void Interpreter::_register_methods() {
register_method("_process", &Interpreter::_process);
register_method("_init", &Interpreter::_init);
//register_property<Interpreter, std::string>("code", &Interpreter::code, "");
}
Interpreter::Interpreter() {
}
Interpreter::~Interpreter() {
}
void Interpreter::runcode() {
if (!isRunning) {
isRunning = true;
//PyRun_SimpleString(code.c_str());
}
}
void calledStart() {
PyImport_AppendInittab("AEInternal", &PyInit_emb);
Py_Initialize();
if (_import_array()) {
}
}
void Interpreter::_init() {
calledStart();
}
void Interpreter::_process() {
}