How To Fix XML Input Fields not Synchronizing to All Clients in Tabletop Simulator – If your XML UI is out of sync with all of what is written, there is an easy fix. There is also a generic Boilerplate for XML UI.
How To Fix XML Input Fields not Synchronizing to All Clients in Tabletop Simulator
If you want an InputField in your XML UI to sync with all clients, it must have an “onValueChanged” handler and an “id” field, e.g.
<InputField id=”4″ onValueChanged=”onValueChanged” />
The Lua code for the object must then implement this handler, and this handler must update the “Text” attribute of the InputField. For example:function onValueChanged(player, value, id)
self.UI.setAttribute(id, “Text”, value)end
Note that this is why the element must have an “id” so that the code can map back to its attribute. Also note that `setValue` will NOT work here, even though you might expect it to.
This behavior is somewhat unexpected. I have submitted a bug report here if you wish to vote or comment for support:
Boilerplate for a synchronized, stored XML user interface
Attached here is some XML UI standard that implements onSave and onLoad and synchronizes the above values so that updates can be seen by all players.
It’s basically the basis of the record sheets for the Gensou Narratograph mod. You can also use it as a springboard.
XML:
<Default>
<InputField onValueChanged=”valueChanged” />
<Toggle onValueChanged=”toggleChanged” />
</Default>
<!– Here your user interface… –>
Lua:
local state = {
inputs = {},
switches = {}
}
applyState() function
for id, v in pairs (state.inputs) do
self.UI.setAttribute(id, “Text”, v)
self.UI.setValue(id, v)
end
for id, v in pairs (state.toggles) do
self.UI.setAttribute(id, “isOn”, v or nil)
end
end
function valueChanged(player, value, id)
state.inputs[id] = value
self.UI.setValue(id, value)
— If you update the value, it does not update the content because Lmao.
self.UI.setAttribute(id, “Text”, value)
end
function toggleChanged(player, isOn, id)
state.toggles[id] = isOn
self.UI.setAttribute(id, “isOn”, isOn or nil)
end
function onLoad(save_state_string)
if save_state_string ~= “” then
status = JSON.decode(store_status_string)
applyState()
end
end
onSave() function
return JSON.encode(status)
end
This uses the `Defaults` block to apply these controls to all input elements by default.
Classes defined in `Defaults` blocks can also define `onValueChanged` attributes, which is useful if you have multiple elements that do similar things.
Elements or classes that need more or different functionality can of course supply their own onValueChanged handlers that call `valueChanged` to achieve saving and synchronization.
Thanks for reading our post on How To Fix XML Input Fields not Synchronizing to All Clients in Tabletop Simulator, make sure always to drop comments, and don’t also forget that suggestions are allowed.
Written By: Capellan