Rules regarding callScriptFunction according to script type

Typically: "How do I... ", "How can I... " questions
Post Reply
atoz
Posts: 57
Joined: 18 Oct 2013, 09:02

Rules regarding callScriptFunction according to script type

Post by atoz »

Hi,

It is not clear from the documentation when the sim.callScriptFunction can be used in each script type to call a function in another script. Can you help clear this up by filling in the following matrix?

Code: Select all

Caller / Callee    | Non-threaded child | Threaded child | Customisation | Main |
Non-threaded child |           O        |        O       |       O       |   O  |
Threaded child     |           X        |        O       |       X       |   X  |
Customisation      |           O        |        O       |       O       |   O  |
Main               |           O        |        O       |       O       |   O  |
Many thanks

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

Re: Rules regarding callScriptFunction according to script type

Post by coppelia »

Done!

More general:

Code: Select all

Caller / Callee    | Non-threaded  item  | Threaded  item  |
Non-threaded  item |           O         |        O        |
Threaded  item     |           X         |        O        |
where:
Non-threaded item could be: and where:
Threaded item is: Cheers

atoz
Posts: 57
Joined: 18 Oct 2013, 09:02

Re: Rules regarding callScriptFunction according to script type

Post by atoz »

Thank you. You mention that a non-threaded script can call a function in a threaded script. So why does the following cause an error:

Threaded script- Dummy:

Code: Select all

function add(x,y)
print("Threaded add: "..x+y)
end
Non-threaded script (customization/ child)- Dummy0:

Code: Select all

sim.callScriptFunction('add@Dummy',sim_scripttype_childscript ,3,5)
This results in the following error:

Code: Select all

Lua runtime error: [string "CUSTOMIZATION SCRIPT Dummy0"]:23: Failed calling script function. (sim.callScriptFunction)
--> Customization script temporarily disabled.
stack traceback:
	[C]: in function 'callScriptFunction'
	[string "CUSTOMIZATION SCRIPT Dummy0"]:23: in function <[string "CUSTOMIZATION SCRIPT Dummy0"]:22>
Btw, the same code does not cause an error in between non-threaded scripts. Can you clarify the problem here please?

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

Re: Rules regarding callScriptFunction according to script type

Post by coppelia »

The threaded child script needs to be initialized (i.e. its Lua state initialized). This kind of situation often happens when the threaded child script as finished its execution.
Try again with following:

Code: Select all

function add(x,y)
    print("Threaded add: "..x+y)
end

function sysCall_threadmain()
    while sim.getSimulationState()~=sim.simulation_advancing_abouttostop do
        sim.switchThread() -- resume in next simulation step
    end
end

function sysCall_cleanup()
    -- Put some clean-up code here
end
Cheers

atoz
Posts: 57
Joined: 18 Oct 2013, 09:02

Re: Rules regarding callScriptFunction according to script type

Post by atoz »

Looks like there is some magic happening in that while loop- I no longer get the error although it still appears when stopping the simulation. This is probably some race condition where the threaded script no longer exists yet the call is being made to the function in the script.

For my purpose, I would like to be able to call a function in the threaded script from a customisation script to alter a variable before the simulation starts running. It seems this is not possible using the sim.callScriptFunction since the threaded script is not initialised before the simulation runs. Is there another way to achieve this?

Cheers

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

Re: Rules regarding callScriptFunction according to script type

Post by coppelia »

Yes, the magic or purpose of the while loop is to keep the threaded script alive, so that its function can be called.

Calling script functions is often overrated and not needed. You can for instance tag an object appropriately from the customization script. And react to that tag in the threaded child script. For instance:

in your customization script, somewhere:

Code: Select all

objHandleOfThreadedChildScript=sim.getObjectHandle('myThreadedChildScriptObject')
local data={}
data.myString='hello world'
data.functionShouldRun=true
data.someOtherStuff={1,2,4.32,'good-bye'}
sim.writeCustomDataBlock(objHandleOfThreadedChildScript,'tagName',sim.packTable(data))
Then, your threaded child script could look like:

Code: Select all

function f()

end

function sysCall_threadmain()
    local objHandleOfThreadedChildScript=sim.getObjectHandle('myThreadedChildScriptObject')
    local data=sim.readCustomDataBlock(objHandleOfThreadedChildScript,'tagName')
    if data then
        data=sim.unpackTable(data)
        print(data.myString)
        if data.functionShouldRun==true then
            f()
        end
    end

    ...

end

function sysCall_cleanup()
end
Cheers

Post Reply