sim.callScriptFunction via zeroMQ

Typically: "How do I... ", "How can I... " questions
Post Reply
PvtSchneewitchen
Posts: 15
Joined: 12 Jul 2019, 09:37

sim.callScriptFunction via zeroMQ

Post 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

PvtSchneewitchen
Posts: 15
Joined: 12 Jul 2019, 09:37

Re: sim.callScriptFunction via zeroMQ

Post 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);

fferri
Posts: 1297
Joined: 09 Sep 2013, 19:28

Re: sim.callScriptFunction via zeroMQ

Post 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

Post Reply