I recreated your program in the
Blockly playground, using the variable
num instead of
i and
j, and then saved it to JSON, which gave me the serialised state that I have attached to this message as
example.json; I was then able to successfully reload this JSON and the program was recreated with
num as expected:
The playground's save button does this (paraphrased) to serialise the state of the workspace:
var state = Blockly.serialization.workspaces.save(workspace);
var json = JSON.stringify(state, null, 2);
That produces JSON that includes a variable model for num:
{
"blocks": {
"languageVersion": 0,
"blocks": [
{
"type": "variables_set",
// …additional detail omitted.
"fields": {
"VAR": {
"id": "[v?.wo/07ZqKU5^ym%T|"
}
},
// …additional blocks omitted.
}
]
},
"variables": [
{
"name": "num",
"id": "[v?.wo/07ZqKU5^ym%T|"
}
]
}
The load button then does this (paraphrased) to deserialize the state of the workspace:
var state = JSON.parse(json);
Blockly.serialization.workspaces.load(state, workspace);
No additional calls should be required to make saving and loading variables work.
Can you check that your code is doing something similar, and also check which version of Blockly you are using? It's possible that at some point in the past there was a bug involving serialisation of variables that was subsequently fixed.
Best wishes,
Christopher