Jump to content
  • Register Now

    Sign up and take advantage of the features of our community! Some forums are only accessible to our members!

  • 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.


  • Recently Browsing

    No registered users viewing this page.

  • Topics

  • Posts

    • Hy, I have a problem with the compile this system ! I have problem with this : [TABLES.H]   typedef struct SShopItemTable { DWORD vnum; BYTE count; TItemPos pos; // PC 상점에만 이용 long long price; // PC, shop_table_ex.txt 상점에만 이용 BYTE display_pos; // PC, shop_table_ex.txt 상점에만 이용, 보일 위치. long alSockets[ITEM_SOCKET_MAX_NUM]; TPlayerItemAttribute aAttr[ITEM_ATTRIBUTE_MAX_NUM]; DWORD price_type = 1;
    • There are no ui.py files in the release. I already changed the characterwindow.py as you can see in the screen, I just need the new imagebox class so that i can show a tooltip when i hover above the image. The picture is already from my client Learn how to read
    • Hello guys, how to solve this problem ?  this is problem   this is the fix     How to fix? 
×
×
  • 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.