Jump to content
  • 0

Calling python api with C ?


ledi12

Question

Version of Files XXX

Hi,

I'm trying to call some api functions from player module, like character position.

 

PyObject* obj = PyImport_ImportModule(const char *"player");
std::cout << obj.GetMainCharacterPosition() << std::endl;

I would appreciate any hint to to call python api in C.

 

Link to post

2 answers to this question

Recommended Posts

  • 0
Posted (edited)

Is not that easy as you think and also you need to make individual code for each function, if they've arguments and more.

Here's my method, i tested it and works fine.

Hidden Content

    	const auto& poModule = PyImport_ImportModule("player");
    	if (!poModule)
    		return false;
    
    	const auto& poFunc = PyObject_GetAttrString(poModule, "GetMainCharacterPosition");
    	if (!PyCallable_Check(poFunc))
    		return false;
    
    	const auto& poResult = PyObject_CallObject(poFunc, nullptr);
    	if (!poResult)
    		return false;
    
    	for (size_t i = 0; i < PyTuple_GET_SIZE(poResult); ++i)
    		std::cout << PyFloat_AsDouble(PyTuple_GetItem(poResult, i)) << std::endl;

 

Edited by VegaS™ (see edit history)
  • Love 2
Link to post
  • 0
Posted (edited)
On 7/9/2020 at 9:02 PM, VegaS™ said:

Is not that easy as you think and also you need to make individual code for each function, if they've arguments and more.

Here's my method, i tested it and works fine.

Looks fine to me :) To make it working I need to inject it as the dll first hmm ? 

Edited by VegaS™ (see edit history)
Link to post
Guest
This topic is now closed to further replies.


×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.