Total War: WARHAMMER III

Total War: WARHAMMER III

MP Sync
 This topic has been pinned, so it's probably important
BlackJackGear  [developer] 3 Jun @ 2:51pm
Documentation
Documentation
Documentation for modders.

All functions are accessed through the global object "bjg_mp_sync"
Example:
local success, info = bjg_mp_sync:bjg_add_sync_table("my_table_of_units")

bjg_add_sync_event(id, msg, header)
This function adds an event to the sync queue which will be sent to all players.
It will not send the event immediately, by itself the sync runs every 10 seconds, if you want to send immediately use bjg_sync_now() after adding the event.
id
The numeric id to identify the event by.
1000-10000 reserved for internal use.
Example: 123
msg
The string to send to every player.
This usually contains the information you need to send to all players.
Example: "lots of interesting information bla bla whatever"
header (Optional)
This is the header by which you can identify events from your mod.
Should be a short string to avoid overhead.
You don't have to include this parameter, in that case it will send the msg as is, and you can use the id to identify your event.
It is however recommended to include it to avoid overlap with other mods using the same id.
Example: "my_modname" OR nil
Return_1 success
Returns a boolean indicating success or failure
Example: true
Return_2 info
Returns a string giving additional information about the outcome
Example: "table is not blocked, adding event to queue"
Example
Add event to queue:
local success, info = bjg_mp_sync:bjg_add_sync_event(123, "info I want to send to every player", "my_header")
Add and send immediately:
local success, info = bjg_mp_sync:bjg_add_sync_event(123, "info I want to send to every player", "my_header") success, info = bjg_mp_sync:bjg_sync_now()
Read the listener part at the end on how to receive these messages.

bjg_add_sync_table(table_name, manual_sync, ignore_local, set_context)
This function adds a global lua table to the list of tables to keep in sync.
Global lua tables included in the list will be kept in sync across all players.
Sync interval is every 10 seconds.
Calling this function triggers an initial sync immediately.
!Your table_name and keys CANNOT include-> # <-this character otherwise bad things will happen!
!Other character sequences to avoid in general are-> #|# #;# #=# <-these three are used as control codes!
table_name
The name of the global table as a string.
Example: "bjg_test_table"
manual_sync (Optional)
A boolean indicating whether the table should be excluded from automatic sync.
You will have to sync it manually with bjg_sync_table_now(table_name) every time you make changes.
Example: true
ignore_local (Optional)
If this boolean is true any values which have the local players faction name somewhere in their key path, will not be written to the global table.
For example in table:
bjg_example_table = { player_A_faction_name = { value_1 = 123, value_2 = 5000 }, player_B_faction_name = [ value_3 = 1111111, value_4 = "abc" ] }
If ignore_local is true and an update event for value_1 is sent then in player As game instance the global table will NOT be updated, but in player Bs game instance it will.
This is because in the key path for value_1 (player_A_faction_name => value_1) the faction_name of player A is included.
Example: true
set_context (Optional)
If this boolean is true, after every sync a cco script object with the tables name and the entire table included as TableValue will be set.
This is useful, if info from the table is used in ui elements.
Example: true
Return_1 success
Returns a boolean indicating success or failure
Example: false
Return_2 info
Returns a string giving additional information about the outcome
Example: "table already exists: bjg_test_table"
Example
Basic
bjg_test_table = { key1 = "value_1", key2 = { "value_2", "value_3", }, key3 = 123 } local success, info = bjg_mp_sync:bjg_add_sync_table("bjg_test_table")

Advanced:
bjg_test_table = { key1 = "value_1", key2 = { "value_2", "value_3", }, key3 = 123 } local success, info = bjg_mp_sync:bjg_add_sync_table("bjg_test_table", false, true, true)

bjg_mp_sync:bjg_remove_sync_table(table_name)
This function removes a table from the list of tables to keep in sync.
table_name
The name of the global table as a string.
Example: "bjg_test_table"
Return_1 success
Returns a boolean indicating success or failure
Example: true
Return_2 info
Returns a string giving additional information about the outcome
Example: "removed table: bjg_test_table"
Example
local success, info = bjg_mp_sync:bjg_remove_sync_table("bjg_test_table")

bjg_sync_now()
This function triggers a sync and resets the next automatic sync event to occur in 10 seconds.
Return_1 success
Returns a boolean indicating success or failure
Example: false
Return_2 info
Returns a string giving additional information about the outcome
Example: "sync is blocked"
Example
local success, info = bjg_mp_sync:bjg_sync_now()

bjg_sync_table_now(table_name)
This function triggers a sync event for a specific table and resets the next automatic sync event to occur in 10 seconds.
This function is needed to sync tables that have automatic sync disabled.
table_name
The name of the global table as a string.
Example: "bjg_test_table"
Return_1 success
Returns a boolean indicating success or failure
Example: false
Return_2 info
Returns a string giving additional information about the outcome
Example: "table does not exist: bjg_test_table"
Example
local success, info = bjg_mp_sync:bjg_sync_table_now("bjg_test_table")

bjg_stop_sync()
This function is used to block the sync globally.
Return_1 success
Returns a boolean indicating success or failure
Example: true
Return_2 info
Returns a string giving additional information about the outcome
Example: "sync stopped"
Example
local success, info = bjg_mp_sync:bjg_stop_sync()

bjg_resume_sync()
This function is used to unblock the sync globally.
Return_1 success
Returns a boolean indicating success or failure
Example: true
Return_2 info
Returns a string giving additional information about the outcome
Example: "sync resumed"
Example
local success, info = bjg_mp_sync:bjg_resume_sync()

Listener: BjgMpSync
This listener can be used to receive the events added via bjg_add_sync_event()
Context functions
Use context:id() to get the id you sent with the event.
The string is accessd with context:msg()
!The header is added at the front of the msg, you have to parse it out like in the example below!
Example
core:add_listener( "BJG_EXAMPLE_LISTENER", "BjgMpSync", function(context) return context:msg():starts_with("my_header") end, function(context) local str = context:msg():sub(#"my_header" + 1) out("ID: " .. tostring(context:id())) out("MSG with header: " .. context:msg()) out("MSG no header: ".. str) end, true )

The mod has an mct option to enable logging.
Logs will be written to bjg_mp_sync.txt in the games main folder.
Last edited by BlackJackGear; 5 Jun @ 10:31am