Page 1 of 1

sim.callScriptFunction via zeroMQ

Posted: 10 Sep 2024, 07:26
by PvtSchneewitchen
I have a problem passing arguments to sim.callScriptFunction in my c++ application via zeroMQ. I'm working with the RemoteApiCLient and jsoncons library.
When i build a json array with jsoncons and pass it to the respective function this function will receive just the first element of the array

C++ Code

Code: Select all

if ( !m_pSimClient )
   return false;
auto sim = m_pSimClient->getObject().sim();

json argsRet( json_array_arg );
json args( json_array_arg );
args.push_back("1");
args.push_back("2");
args.push_back("3");

int nScriptHandle = sim.getScript( sim.scripttype_customization, "Script_GUI" );
argsRet = sim.callScriptFunction( "start", nScriptHandle, args );
CoppeliaSim Script_GUI

Code: Select all

function start(args)
    print(args)
end
Print output:
1

Re: sim.callScriptFunction via zeroMQ

Posted: 10 Sep 2024, 08:59
by PvtSchneewitchen
Fixed the problem by myself:

Code: Select all

if ( !m_pSimClient )
   return false;
auto sim = m_pSimClient->getObject().sim();

json argsRet( json_array_arg );
json args( json_array_arg );
args.push_back("1");
args.push_back("2");
args.push_back("3");

//-----------------------------------
json callArgs( json_array_arg );
callArgs.push_back( args );
//-----------------------------------

int nScriptHandle = sim.getScript( sim.scripttype_customization, "Script_GUI" );
argsRet = sim.callScriptFunction( "start", nScriptHandle, callArgs);

I don't know if this is desired behaviour but in the examples.cpp (CoppeliaSimEdu\programming\zmqRemoteApi\clients\cpp) you mentioned following code that didn't work for me:

Code: Select all

int script = sim.getObject("/path/to/scriptObject");
auto args = json::array();
args.push_back("Hello");
args.push_back("Paul");
args.push_back(21);
auto reply = sim.callScriptFunction("functionName", script, args);

Re: sim.callScriptFunction via zeroMQ

Posted: 13 Sep 2024, 08:36
by fferri
PvtSchneewitchen wrote: 10 Sep 2024, 07:26

Code: Select all

json args( json_array_arg );
args.push_back("1");
first arg passed to function
PvtSchneewitchen wrote: 10 Sep 2024, 07:26

Code: Select all

args.push_back("2");
second arg passed to function
PvtSchneewitchen wrote: 10 Sep 2024, 07:26

Code: Select all

args.push_back("3");
int nScriptHandle = sim.getScript( sim.scripttype_customization, "Script_GUI" );
argsRet = sim.callScriptFunction( "start", nScriptHandle, args );
CoppeliaSim Script_GUI

Code: Select all

function start(args)
    print(args)
end
function is getting only first arg.

Use multiple args to get all args:

Code: Select all

function start(arg1, arg2, arg3)
    print(arg1) -- "1"
    print(arg2) -- "2"
    print(arg3) -- "3"
end