simDisplayDialog fails when called in menuitem_selected

Typically: "How do I... ", "How can I... " questions
Post Reply
ysuga
Posts: 1
Joined: 21 Jan 2021, 08:48

simDisplayDialog fails when called in menuitem_selected

Post by ysuga »

I am V-rep user and now I am planning to use CoppeliaSim in University research activities.
Before that, I must upgrade my own v-rep Ext plugin to the current CoppeliaSim, but I have a problem around showing dialog.

The plugin is written in C++, and registers some customized menu items in the "simStart" function (simAddModuleMenuEntry).
When I select the menu item, sim_message_eventcallback_menuitemselected message is captured in simMessage function, and I would like simulator to pop up an Input dialog.

Code: Select all

if (message == sim_message_eventcallback_menuitemselected) {
  simInt handle = auxiliaryData[0];
  simInt state = auxiliaryData[1];
  if (handle == robotItemHandle) {
    std::cout << "My Menu Selected" << std::endl;
    dlgHandle = simDisplayDialog("Input Robot Item", "Input Robot Model Name", sim_dlgstyle_input, "", NULL, NULL, NULL);
  }
}
The global scope variable named "dlgHandle" becomes 0, but the simulator indicates the following message:

Code: Select all

[CoppeliaSim:error]   External call to simCallScriptFunction failed (sim.getDialogResult): Error in script function.
What is wrong? This routine was successful in V-rep. Please help me!

My environment:
IntelCPU PC + Windows 10 + Visual Studio 2017 (Compiled with x64)
CoppeliaSim 4.1.0 Edu

coppelia
Site Admin
Posts: 8446
Joined: 14 Dec 2012, 00:25

Re: simDisplayDialog fails when called in menuitem_selected

Post by coppelia »

Hello,

this is because we have reworked several part of the GUI. The sim_message_eventcallback_menuitemselected event is handled via the GUI thread. But you shouldn't normally call API functions via the GUI thread (that is still possible, but bad practice and might lead to similar problems in future). Instead, use a signal-slot mechanism to use the simulation thread, or a very simple way could also be something like:

Code: Select all

SIM_DLLEXPORT void* simMessage(int message,int* auxiliaryData,void* customData,int* replyData)
{
    static bool showdlg=false;

    if (message==sim_message_eventcallback_menuitemselected)
        showdlg=true;

    if (message==sim_message_eventcallback_instancepass)
    {
        if (showdlg)
        {
            int dlgHandle = simDisplayDialog("Input Robot Item", "Input Robot Model Name", sim_dlgstyle_input, "", NULL, NULL, NULL);
            printf("dlgHandle: %i\n",dlgHandle);
            showdlg=false;
        }
    }
    
    ...
    
}
Cheers

Post Reply