godot-statecharts
godot-statecharts copied to clipboard
Mechanism to sync chart state from authority
using state charts in my multiplayer game. the chart works just fine on the server. but (among other things) i'd like the state to be synced to clients so i can view it in StateChartDebugger.
EDIT: I just noticed StateChartSerializer and SerializedStateChart.to_string() but there appears to be no way to convert it back.
If you would like to use the StateChartSerializer, you can do it like this:
# convert to serialized, this will give you a resource
var serialized = StateChartSerializer.serialize(chart)
# send it over (this is specific for your network code)
# apply to an existing state chart on the client side
StateChartSerializer.deserialize(serialized, chart)
You can find a bit more detail on how this works in the documentation. That being said, this might not be the most efficient way of debugging your state charts. It is mainly intended as a way of loading and saving games and being able to restore a state chart to a certain state after loading the game. As with networking, I'm not really sure if one wants to synchronize the state chart itself or if it is actually better to just have the callbacks being called by a RPC. So that way the state chart would only run on the authority, and the clients would just get notified when anything of substance happens.
If you want to only debug your StateChart on the server-side, you can use the remote debugger that is built-in to Godot and use the debugger integration of the plugin. To make this work, you first need to make sure that your build is exported with the editor feature, so we activate the editor integration of the State Charts plugin:
Then you will need to export this with debug enabled. So you need to tick the checkbox in the export dialog:
Then you will need to set up Godot so it listens for incoming debugger connections:
Finally, you need to find out on which interface Godot is listening, and you can find this in the editor settings:
Make sure this is a port and IP address that your server can reach. And now you can start your server with the Remote Debug argument and it should connect to your editor, and then you can watch the state chart in the editor.
# You will need to replace the IP and port with the ones from your settings dialog.
your_exported.exe --remote-debug tcp://192.168.1.254:6007
https://github.com/user-attachments/assets/23057a6e-7c85-44c3-a2db-067a27adfb0e
Also make sure that you have marked each state chart with "track in editor" that you want to see in the debugger.
I hope this helps.
Thank you for all this information, it should be in the docs, but it's not what I wanted >.<
If you would like to use the
StateChartSerializer, you can do it like this:# convert to serialized, this will give you a resource var serialized = StateChartSerializer.serialize(chart) # send it over (this is specific for your network code) # apply to an existing state chart on the client side StateChartSerializer.deserialize(serialized, chart)
the "send it over (this is specific for your network code)" is the part I wanted. Which i did end up discovering one does with var_to_bytes_with_objects(), sending it via rpc, and then bytes_to_var_with_objects(). I'm aware of the security implications; the way i've done it, it's only a problem if the server is already pwned. A safe version if this built in to SerializedStateChartState would be nice.
I think networking in general is extremely specific to the game that you're developing, and there is not really a good way of building this into the library. Whatever I would build in would work for one game or two and would totally not work for others. So I think I'll keep the networking out of this library and leave this to the developers the games.