File System
Standard .NET file access is restricted to prevent rogue access to your files, this means you can not use System.IO.File or variants directly.
Standard .NET file access is restricted to prevent rogue access to your files, this means you can not use System.IO.File or variants directly.
Instead, s&box provides a BaseFileSystem for several virtual filesystems that can only access files within specific game directories.
File Systems
There are a few different File Systems available to use in your game. Each one writes and reads from a different location.
Data File System
FileSystem.Data is used to read and write data to a sub-directory specifically for the game that is currently running, i.e. C:\steam\steamapps\common\sbox\data\org\game\
Mounted File System
FileSystem.Mounted is an aggregate filesystem of all mounted content from the core game, the current game and its dependencies.
Organization File System
FileSystem.OrganizationData is a place to store user data across several games in your organization, i.e.
C:\steam\steamapps\common\sbox\data\org\
Reading and Writing Text
// If the file doesn't exist already then write some data to it
if ( !FileSystem.Data.FileExists( "player.txt" ) )
FileSystem.Data.WriteAllText( "player.txt", "Hello, world!" );
// Read the text back into a variable from the file
var hello = FileSystem.Data.ReadAllText( "player.txt" );
Reading and Writing Json
WriteJson and ReadJson will only work with Properties of your class unless directed not to - make sure the things you want to save are Properties!
public class PlayerData
{
public int Level { get; set; } // Will be serialized
public int MaxHealth { get; set; } // Will be serialized
public string Username; // Won't be serialized
public static void Save( PlayerData data )
{
FileSystem.Data.WriteJson( "player.json", data );
}
public static PlayerData Load()
{
return FileSystem.Data.ReadJson<PlayerData>( "player.json" );
}
}
Referenced API
Canonical API pages mentioned in this guide.
A subset of `Sandbox.FileSystem.OrganizationData` for custom gamemode data.
All mounted content.
A filesystem for custom data, per gamemode's organization.
A filesystem that can be accessed by the game.
A filesystem. Could be on disk, or in memory, or in the cloud. Could be writable or read only. Or it could be an aggregation of all those things, merged together and read only.
A filesystem that can be accessed by the game.
Variables have a name and type, and are local to each invocation of an `Facepunch.ActionGraphs.Variable.ActionGraph`. They are assigned with a `!:NodeLibrary.SetVar` node, and read with `!:NodeLibrary.GetVar`.
Provides global access to core game state, utilities, and operations for S&box. The `Sandbox.Game` class exposes static properties and methods to query and control the running game, such as checking if the game is running, getting your steamid, taking screenshots, and managing game sessions.