Makinom - Game Toolkit for Unity
  • Features
  • Showcase
  • Guide
    • Documentation
    • Tutorials
    • Extensions
    • API
  • Makinom 1
    • Tutorials
    • Plugins
    • API
  • Support
  • Forum
  • Get Makinom

Guide

Getting Started

  • Introduction
  • First Steps
  • Game Starters
  • Components Overview
  • Upgrading a Makinom 1 Project

Editor

  • Makinom Editor Overview
  • Node Editor
  • Asset Sources
  • Editor Section
  • Base/Control Section
  • Game Section
  • UI Section
  • Templates Section

Features

  • Schematics
  • Dialogue Importer
  • Schematic Nodes
  • Formula Nodes
  • Languages
  • Input Keys
  • Audio and Music
  • Game States
  • Pooling
  • Saving Game Objects
  • Scene Objects
  • Save Games
  • Variables
  • Selected Data

Machines

  • Machine Components Overview
  • Animation Machine
  • Animation State Machine
  • Application Machine
  • Auto Machine
  • Collision Machine
  • Global Machine
  • Interaction Machine
  • Render Machine
  • Tagged Machine
  • Template Machine
  • Tick Machine
  • Trigger Machine

UI System

  • UI System Overview
  • UI Layers
  • UI Layouts
  • UI Boxes
  • HUDs
  • Flying Texts
  • Text Codes
  • Unity UI Module
  • HUDs: Content Providers
  • HUDs: Conditions
  • HUDs: Click Actions

Scripting

  • Scripting Overview
  • Code Extensions
  • Custom Save Data
  • Custom Component Save Data
  • Custom Nodes
  • Starting a Schematic

Advanced Topics

  • Build Troubleshooting
  • Performance Optimizations
  • Home
  • Guide
  • Documentation
  • Scripting
  • Custom Component Save Data

Custom Component Save Data

Table of Contents
  • When will the data be stored/loaded?
  • The IComponentSaveData interface
    • GetSaveKey Function
    • SaveGame Function
    • LoadGame Function
  • Code example
  • Saving Complex Data

Store component data attached to a game object, keeping it between scene changes and save it with Makinom’s save game system.

This is done by implementing the IComponentSaveData interface in your component’s class.

You can also add custom save data (outside of components) – see the custom save data documentation for details.

When will the data be stored/loaded? #

The game object needs to be a prefab instance registered with a prefab saver, or use a Game Object Saver component (and be part of the scene’s setup, i.e. not spawned during play). Learn more in this documentation.

The data of all components implementing the IComponentSaveData attached to a game object or any of its children will be stored when the game object is destroyed, including scene changes.

The data is loaded when returning to the scene – again, when it was saved as a prefab saver or via a Game Object Saver component.

When using ORK Framework, components attached to game objects will also be saved – see this ORK documentation for details.

The IComponentSaveData interface #

The IComponentSaveData interface extends the ISaveData interface that’s the basis for all save data in Makinom (and also used in the custom save data handling).

This interface has 3 functions (2 inherited from the ISaveData interface) that need to be implemented into your class:

GetSaveKey Function #

This function returns the save key used to store the data of the component.

public string GetSaveKey()

The save key mustn’t contain any spaces, e.g. save key is not allowed, but save_key is allowed.

Only one component data can be stored per key, i.e. if you want to store multiple components of the same class, you should implement a save key field to define a save key in the inspector of the component and return that key in the function.

SaveGame Function #

This function is called when the component data is stored/saved. It returns a DataObject, which holds all the data that you want to save.

public DataObject SaveGame()

LoadGame Function #

This function is called when a component data is loaded.

public void LoadGame(DataObject data)

The DataObject passed as parameter contains the data that is saved. It’s crucial to make a check for null on the data object before using it, since it could be null if no data was found for the save key – in that case, you can use the load game function to do default initialization.

Code example #

Here’s a small code example on how the custom component save data functionality can be used.

using UnityEngine;
using GamingIsLove.Makinom;

public class ComponentSaveTest : MonoBehaviour, IComponentSaveData
{
	// save key used to store different components of this class
	public string saveKey = "";


	// save values
	public bool toggle = false;

	public float number = 0;

	public string text = "";


	public string GetSaveKey()
	{
		return saveKey;
	}

	// called when the component data is saved/stored
	public DataObject SaveGame()
	{
		DataObject data = new DataObject();

		data.Set("toggle", this.toggle);
		data.Set("number", this.number);
		data.Set("text", this.text);

		return data;
	}

	// called when the component data is loaded
	public void LoadGame(DataObject data)
	{
		if(data != null)
		{
			data.Get("toggle", ref this.toggle);
			data.Get("number", ref this.number);
			data.Get("text", ref this.text);
		}
	}
}

Saving Complex Data #

You can also save the data from classes and class arrays within your custom save game data by implementing the ISaveData interface in the class. This allows creating complex save game structures.

Naturally you can also save classes within classes, within classes … there is no depth limit.

See the custom save data documentation for details.

Scripting
Share This Article :
  • Facebook
  • Twitter
  • LinkedIn
  • Pinterest
Updated on June 30, 2022
Table of Contents
  • When will the data be stored/loaded?
  • The IComponentSaveData interface
    • GetSaveKey Function
    • SaveGame Function
    • LoadGame Function
  • Code example
  • Saving Complex Data
Sitemap
  • Features
  • Showcase
  • Guide
    • Documentation
    • Tutorials
    • Extensions
    • API
  • Makinom 1 Hub
    • Tutorials
    • Plugins
    • API
  • Support
  • Forum
  • Get Makinom
  • Contact
  • Blog
  • ORK Framework
  • gamingislove.com
Categories
  • Makinom 1 (97)
    • Tutorial (97)
      • 2D Platformer (14)
      • 2D Roguelike (8)
      • Breakout (6)
      • How-to (34)
      • Match 3 (6)
      • Schematic (4)
      • Scripting (6)
      • Space Shooter (11)
      • Survival Shooter (8)
  • News (11)
  • Release (68)
Search

© 2015 Gaming is Love e.U.

Disclosure: This site may contain affiliate links, which means I may receive a commission if you click a link and purchase something that I have recommended. While clicking these links won’t cost you any money, they will help me fund my development projects while recommending great assets!