Basic Usage
using PKHeX.Core;
// Load a save file from disk
string savePath = "path/to/your/save.sav";
if (SaveUtil.TryGetSaveFile(savePath, out SaveFile? sav))
{
Console.WriteLine($"Loaded {sav.Version} save file!");
Console.WriteLine($"Trainer: {sav.OT}");
Console.WriteLine($"Generation: {sav.Generation}");
}
else
{
Console.WriteLine("Failed to load save file.");
}
SaveUtil automatically detects the save file type and returns the appropriate SaveFile subclass (e.g., SAV7SM, SAV8SWSH, SAV9SV).byte[] saveData = File.ReadAllBytes("save.sav");
Memory<byte> data = new Memory<byte>(saveData);
if (SaveUtil.TryGetSaveFile(data, out SaveFile? sav, savePath))
{
Console.WriteLine($"Game Version: {sav.Version}");
Console.WriteLine($"Playtime: {sav.PlayTimeString}");
}
// Get a Pokémon to modify
PKM pk = sav.GetBoxSlotAtIndex(box: 0, slot: 0);
if (pk.Species != 0)
{
// Modify basic properties
pk.Nickname = "Charizard";
pk.Stat_Level = 100;
pk.CurrentFriendship = 255;
// Modify stats (IVs)
pk.IV_HP = 31;
pk.IV_ATK = 31;
pk.IV_DEF = 31;
pk.IV_SPA = 31;
pk.IV_SPD = 31;
pk.IV_SPE = 31;
// Modify EVs
pk.EV_ATK = 252;
pk.EV_SPE = 252;
pk.EV_HP = 6;
// Modify moves
pk.Move1 = 406; // Dragon Rush
pk.Move2 = 394; // Flare Blitz
pk.Move3 = 369; // Roost
pk.Move4 = 332; // Earthquake
// Set as shiny (example)
pk.PID = EntityPID.GetRandomPID(
pk.Species,
pk.Gender,
pk.Version,
pk.Nature,
pk.Form,
pk.ID32
);
Console.WriteLine("Modified Pokémon properties!");
}
Always validate modifications using PKHeX.Core’s legality checker to ensure your Pokémon are legal for online play.
// Write the modified Pokémon back to its slot
sav.SetBoxSlotAtIndex(pk, box: 0, slot: 0);
// Or write to a different slot
sav.SetBoxSlotAtIndex(pk, box: 1, slot: 5);
// Mark the save as edited (usually automatic)
Console.WriteLine($"Save edited: {sav.State.Edited}");
// Get the final save data with checksums updated
Memory<byte> finalData = sav.Write();
// Write to file
string outputPath = "path/to/modified_save.sav";
File.WriteAllBytes(outputPath, finalData.ToArray());
Console.WriteLine($"Save file written to {outputPath}");
Console.WriteLine($"Checksums valid: {sav.ChecksumsValid}");
Complete Example
Here’s a complete example that demonstrates the entire workflow:Program.cs
Working with Different Formats
Export Pokémon to File
Export individual Pokémon to.pk* files:
Import Pokémon from File
Key Classes Reference
SaveUtil
Main class for loading and detecting save file types
SaveFile
Base class for all save file types with box/party access
PKM
Abstract base class representing a Pokémon with all properties
EntityFormat
Utilities for creating PKM objects from raw data
Next Steps
Save File Concepts
Deep dive into save file structure and manipulation
Pokémon Entities
Learn about Pokémon data structures and properties
API Reference
Explore the complete API documentation