
I’m using the index from Notes.getNotebookTabs() to update the last notebook tab, and instead of updating that one, it replaces the next to last with the new contents

Provide the code plz

so i wrote this function which takes in json, but can be any arbitrary string and attempts to replace the contents of the tab called “Game Config”
function updateNotebook(json)
for _,data in pairs(Notes.getNotebookTabs()) do
if data.title == "Game Config" then
Notes.editNotebookTab({
index = data.index,
body = json,
})
break
end
end
end
Works perfect for me.
Try to check if indexes are not ordered.
print( data.index, " => ", _ )P.S. Whitespace is a thing by using tabs via copy-paste

data.index is 0 indexed while _ is 1 indexed, but i think that’s the be expected? the indexes are in the correct order though

Overall, I said that this is not a permanent bug. That is, if you create a new simple table (as I did) with a single button and use your function, then everything will be fine. Therefore, until you find the reason (steps to reproduce), the devs are unlikely to be able to fix the bug.

So I compared the TabStates part of json file from a fresh save to the one I had, and turns out it was missing an id 0 tab called “Rules”. after adding that in the code works now, but in the absence of that tab existing i feel like the code should still work?

The following code demonstrates some of the weirdness.
function onLoad(saved_data)
do
local tabs = Notes.getNotebookTabs()
for i = #tabs, 1, -1 do
Notes.removeNotebookTab(tabs[i].index)
end
end
Notes.addNotebookTab({title = "Tab 1", body = "Body 1"})
Notes.addNotebookTab({title = "Tab 2", body = "Body 2"})
Notes.addNotebookTab({title = "Tab 3", body = "Body 3"})
Notes.addNotebookTab({title = "Tab 4", body = "Body 4"})
log(Notes.getNotebookTabs())
for _, data in pairs(Notes.getNotebookTabs()) do
if data.title == "Tab 2" then
Notes.removeNotebookTab(data.index)
end
end
log(Notes.getNotebookTabs())
for _, data in pairs(Notes.getNotebookTabs()) do
if data.title == "Tab 4" then
Notes.editNotebookTab({
index = data.index,
body = "New body 4",
})
end
end
endThe end result is 3 tabs:
Looking at the code, it appears that this happens because tts keeps both a list and dictionary of tabs, and is consitent in which one it uses to index.
For example, editNotebookTab gets the current state from the list of tabs, but then updates the tab corresponding to the dictionary of tabs.
Similarly,
function onLoad(saved_data)
for _, data in pairs(Notes.getNotebookTabs()) do
log(data.title)
Notes.removeNotebookTab(data.index)
end
for _, data in pairs(Notes.getNotebookTabs()) do
log(data.title)
end
enddeletes the even positioned tabs, since it delets based on index in the list of tabs, not the dictionary of tabs.

It looks like this behavior is preserved over saves, as the order of tabs in the save file determines the order of tabs, but the dictionary index is preserved in the save file, but doesn’t impact the order of tabs.

Also encountered that problem.
When inspecting the save file, this is what the notebooks section looks like:
"0": {
"title": "Options",
"body": "",
"id": 0
},
"1": {
"title": "White",
"body": "",
"id": 1
},
"3": {
"title": "Red",
"body": "",
"id": 3
},
"6": {
"title": "Green",
"body": "",
"id": 6
}If I wanted to change the notebook “Red”, the index getNotebookTabs() returns is 2. Using that values doesn’t work though. Using the id value of the notebook (3) works and changes the content. However there’s no way to get the id of the notebook via scripts.
