Left 4 Dead 2/Scripting/Expanded Mutation System/Appendix: Table Save Restore

From Valve Developer Community
Jump to navigation Jump to search

Saving and Restoring Tables across Map Transitions

The following functions can be used to save and restore table data across level transitions, round restarts, or team swaps:

  • SaveTable( string, table ): Stores a table in a location that will persist across level transitions, round restarts, or team swaps. The first parameter is a string that is used to identify the table. The second parameter is the table to save.
  • RestoreTable( string, table ): Retrieves a table from storage. The first parameter is a string that identifies the table you wish to retrieve. The second parameter is the destination for the restored data.

The game mode pvprace illustrates how the feature is used to save race times across level transitions and team swaps.

NOTE: When we say TABLE, we mean TABLE - this serializes to memory as key/value pairs, and thus an array is turned into a table with keys 0,1,2,etc and each key's value is the array entry. So really, you probably want to use this with tables only, and tables within tables, etc.

NOTE: When you exit the game and restart it, all this data goes away. This is all for within a single run

Saving and Loading Strings to Disk

This is a very remedial system to allow you to store strings out to disk and load them back, primarily for doing simple stats or high score lists and so on. It is _very_ basic.

  • StringToFile( file, string ): Stores the string into the file
  • FileToString( file ): Returns the string from the file, null if no file or file is too big.

The Scoring_* functions in sm_utilities.nut have some examples of using this to do a very simple top 4 score list, which you can use as an example, or just use in full if you prefer.

NOTE: This is stored on the SPECIFIC SERVER FOR THE SESSION - so your high scores or character data or other cool info will not stay with you as you move between servers.