Discuss Scratch

iomartin
Scratcher
1 post

Sending a list to scratch with sensors

Hello,

I'm using ScratchIO to update sensors in Scratch, through some Java code.

I'm currently facing the challenge of sending a list of numbers to Scratch, and I'm faced with two options.

  1. Send each number at a time. Something along the lines of
    private ScratchIO sio = new ScratchIO();
    for (int i = 0; i < 100; i++) {
        sio.updateMsg("x", Integer.toString(x));
        sio.broadcastMsg(updateList);
    }
    
  2. Send all list as one and parse it inside Scratch to rebuild the list
    private ScratchIO sio = new ScratchIO();
    String str = "";
    for (int i = 0; i < 100; i++) {
        str += i + ",";
    }
    sio.updateMsg("x", Integer.toString(str));
    sio.broadcastMsg(updateList);
    

What is more efficient? I'm afraid sending several messages must have some performance issues, but at the same time how efficient will Scratch be in parsing the string? My lists should be around 100 elements long.

Thanks
MathWizz
Scratcher
100+ posts

Sending a list to scratch with sensors

Socket performance is so insignificant when dealing with a client as slow as Scratch and it would be even slower to parse it in scratch, so I think the first option should be the faster of the two.

running Chromium 42.0.2311.90 with Flash Player 15.0.0.189 on Arch Linux 3.19.5-1-ck
MathWizzJsScratch && sb.js & Amber (coming soon! maybe)
DigiTechs
Scratcher
500+ posts

Sending a list to scratch with sensors

If you want to reduce socket usage (If you're doing something like what Magnie's doing with his multiplayer space game, whatever it's called), you could make some function thing to receive an infinite ammount of values and do something like this (Giving example in Lua, it's easier for me):

function MultipleSensors(...) --Defines function 'MultipleSensors' with a variable ammount of arguments
local sensors = {...} --Set 'sensors' to a table of those arguments
local sensorreturn = 'sensor-update' --The start for returning
for _, v in pairs(sensors) do --for every item in the arguments to the function run the below code VVV
for sensvar, value in pairs(v) do --For every item in 'v' (it will use a table for every argument) get the index and value of it
sensorreturn = sensorreturn..' "'..sensvar..'" "'..value..'"' --set 'sensorreturn' to the value of sensorreturn concatonated with "[[]value of sensvar]" "[[]value of value]" (where '[[]value of sensvar]' is the sensor to update, and '[[]value of value]' is the value to set the sensor to)
end
end
return sensorreturn --Returns the value of 'sensorreturn' at the end of the function
end

local tosend = MultipleSensors({Hey="Hi!"}, {Goodbye=1})
It should be something like that

I do, in fact, have my own site; it's here.
I'm also working on a thing called Fetch. Look at it here!
@thisandagain pls explain. @thisandagain pls explain. @thisandagain pls explain. @thisandagain pls explain. @thisandagain pls explain.

Powered by DjangoBB