Utils - MSUTeam/MSU GitHub Wiki
File path: scripts/config/msu/utils.nut
The functions here allow you to more easily (de)serialize tables and arrays.
Use ::MSU.Serialization.serialize instead.
::MSU.Utils.serialize( _object, _out )
// _object is a table or array
// _out is the C++ object passed to squirrel onSerialize functions
Easily serializes _object
saving all of its data in the Battle Brothers save.
Will recursively serialize objects within a data structure such as a table. Note: All tables being serialized this way must have all of their keys be of the string
data type.
Use ::MSU.Serialization.deserialize instead.
::MSU.Utils.deserialize( _in )
// _in is the C++ object passed to squirrel onDeserialize functions
Deserializes a table or array serialized using ::MSU.Utils.serialize
. Note: All tables being deserialized this way must have all of their keys be of the string
data type.
// at the end of scripts/items/item onSerialize function
local mySerializedTable = {
Key = "asdf",
Num = 124
};
::MSU.Utils.serialize(mySerializedTable, _out);
// at the end of scripts/items/item onDeserialize function
local myDeserializedTable = ::MSU.Utils.deserialize(_in);
// myDeserializedTable should now be the same as mySerializedTable
A quick word about state management: It appears that states are created and destroyed as you switch between them.
This does not make it very convenient to check or utilize them.
If you need to know if the game is currently in x state,
you can use getActiveState
below, and then compare its className to the available states:
main_menu_state
, tactical_state
, world_state
Example:
if (::MSU.Utils.getActiveState().ClassName == "main_menu_state")
::MSU.Utils.getActiveState()
Returns a weakref to the currently active state: main_menu_state
, tactical_state
, world_state
::MSU.Utils.hasState(_id)
// _id is the filename of the state: `main_menu_state`, `tactical_state`, `world_state`
Returns true if the state is in the MSU table and is not null. Otherwise, returns false. States get destroyed when switched (mostly), so when going to world_state, main_menu_state ceases to exist.
::MSU.Utils.getState(_id)
// _id is the filename of the state: `main_menu_state`, `tactical_state`, `world_state`
Returns a weakref to the state if it exists. Beware: states are dynamically created and destroyed by the game. If the state does not currently exists, returns null.
A simple timer to benchmark code
::MSU.Utils.Timer(_id)
Starts a timer. Also returns the timer class instance, although you don't need to keep it at hand.
::MSU.Utils.Timer(_id).get(_msg = "");
Gets the currently elapsed time of the timer with _id
.
Prints to log.
Optionally prints a message.
::MSU.Utils.Timer(_id).silentGet();
Gets the currently elapsed time of the timer with _id
, without printing the time to the log.
::MSU.Utils.Timer(_id).stop(_msg = "");
Stops and deletes the timer. Prints to log. Optionally prints a message.
::MSU.Utils.Timer(_id).silentStop();
Stops and deletes the timer without printing to log. An equal functionality with the same syntax (except ::, so just MSU.Timer()) exists for JS.