use of table.insert

Typically: "How do I... ", "How can I... " questions
Post Reply
Jonatas Teixeira
Posts: 28
Joined: 13 Oct 2023, 18:04

use of table.insert

Post by Jonatas Teixeira »

Hello,

I made a small script into a empty scene to test understand how to use tables to store data I'll use later in my research to pick and place random objects created on-the-fly. The code follows:

Code: Select all

function sysCall_init()
    sim = require('sim')

    -- Put some initialization code here
    -- sim.setStepping(true) -- enabling stepping mode
end

function sysCall_thread()

    
    -- Put your main code here, e.g.:
    --
    while not sim.getSimulationStopping() do
        if sim.getBufferSignal("mp") then
            mp=sim.getBufferSignal("mp")
            mp=sim.unpackTable(mp)
            if mp[table.getn(mp)]==nil then
                table.insert(mp,1)
            else
                table.insert(mp,mp[table.getn(mp)]+1)
            end
            print(mp[table.getn(mp)])
            print("n="..table.getn(mp))
            sim.setBufferSignal("mp",sim.packTable(mp))
        else 
            mp={}
            sim.setBufferSignal("mp",sim.packTable(mp))
        end
    end
end

-- See the user manual or the available code snippets for additional callback functions and details
I'm getting the following error after a couple simulation steps:
[/script:error] ...rogram Files/CoppeliaRobotics/CoppeliaSimEdu/lua/sim.lua:1850: invalid input data.
stack traceback:
[C]: in function 'error'
...rogram Files/CoppeliaRobotics/CoppeliaSimEdu/lua/sim.lua:1850: in function 'sim.unpackTable'
[string "/script"]:16: in function 'sysCall_thread'
stack traceback:
[C]: in function 'error'
...ogram Files/CoppeliaRobotics/CoppeliaSimEdu/lua/base.lua:464: in function <...ogram Files/CoppeliaRobotics/CoppeliaSimEdu/lua/base.lua:453>
sometimes I get it until the 5th row, sometimes 4th, sometimes I get 15 rows and sometimes 16...

Any ideas on what the problem is?
Jonatas Teixeira
Posts: 28
Joined: 13 Oct 2023, 18:04

Re: use of table.insert

Post by Jonatas Teixeira »

Alright,

I found a solution, just needed to use the step mode to avoid stepping between my if loops

Code: Select all

function sysCall_init()
    sim = require('sim')

    -- Put some initialization code here
    sim.setStepping(true) -- enabling stepping mode
end

function sysCall_thread()

    
    -- Put your main code here, e.g.:
    --
    while not sim.getSimulationStopping() do
        if sim.getBufferSignal("mp") then
            mp=sim.getBufferSignal("mp")
            mp=sim.unpackTable(mp)
            if mp[table.getn(mp)]==nil then
                table.insert(mp,1)
            else
                table.insert(mp,mp[table.getn(mp)]+1)
            end
            print(mp[table.getn(mp)])
            print("n="..table.getn(mp))
            sim.setBufferSignal("mp",sim.packTable(mp))
        else 
            mp={}
            sim.setBufferSignal("mp",sim.packTable(mp))
        end
        sim.step();
    end
end 
Post Reply