hey. I'm creating a game engine to learn more about programming but I'm encountering an obstacle when trying to run a python file from c++. I have a c++ file with a class called Entity. In it I have a function, void runScript(), which should import a python 3 file with the current object directly imported. I tried using pybind11 but I can also use python3.11/Python.h!
The python file should look like this (without needing to importing entity):
print(entity.name)
entity.x += 1
entity.name = "New name"
This is my abbreviated c++ class:
/* entity */
class Entity {
public:
string name = "Entity";
int x = 0;
int y = 0;
int z = 0;
Vector3 position;
Color color;
float size = 1;
Vector3 rotation;
Vector3 scale = { 1, 1, 1 };
string script = "";
// Create a model object
Model model;
Entity(Color color = { 255, 255, 255, 255 }, Vector3 rotation = { 0, 0, 0 }, Vector3 scale = { 1, 1, 1 }, string name = "entity", int x = 0, int y = 0, int z = 0, Vector3 position = {0, 0, 0}, string script = "")
: color(color), size(size), rotation(rotation), scale(scale), name(name), x(x), y(y), z(z), position(position), script(script)
{
// initializeModel();
}
void runScript()
{
// TODO: run python script but importing this object directly (this)
}
};
std::string script will handle the script path (e.g. "../game/hi.py")
How can I run a python script from a c++ class but including, in the python script, the current object?
Like:
void runScript()
{
// Run the python script
// Include this object in the script
}
And:
for (const Entity& entity : entities_list)
{
entity.draw();
entity.runScript();
}
Feel free to ping me: @sly portal !