#Python API - Run script with current object directly included

14 messages · Page 1 of 1 (latest)

sly portal
#

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 !

gritty islandBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

wintry dome
#

@sly portal A couple stuff to start with

#

try to make most of those variables private unless you're going for a struct

#

And if you wanna create an object that's only used by one function, it probably should not be defined in the default constructor

#

I'm talking about model

sly portal
#

ok

#

I agree

wintry dome
#

Now for adding C/C++ bindings you should probably ask in the Python server imo

#

But it's not inappropriate to ask here either

#

Just that they might be able to help better

sly portal
#

ok. Thank you.

#

have a nice day

wintry dome
#

same