Porting Game to Nintendo Switch

Announcements, support questions, and discussion for the Dialogue System.
n_hagialas
Posts: 46
Joined: Wed May 03, 2017 4:34 pm

Porting Game to Nintendo Switch

Post by n_hagialas »

Hey Tony,

We're looking into the process of porting to switch and were wondering if you had any documentation on how the save system's saving and loading might work on there, or if you have any general documentation for Switch porting for the Dialogue System for Unity.

Are there any major things to look out for when porting to Nintendo Switch as well that I haven't mentioned? Any help would be appreciated!

Thanks!
-Nik
User avatar
Tony Li
Posts: 23251
Joined: Thu Jul 18, 2013 1:27 pm

Re: Porting Game to Nintendo Switch

Post by Tony Li »

Hi Nik!

I can only provide general direction here since Nintendo details are under NDA (as are the other console platforms).

The nice thing about the Dialogue System's save system is that you only need to replace the SavedGameDataStorer component. As long as your replacement is a subclass of SavedGameDataStorer, the rest of the save system will work happily with it, without requiring any other changes.

Make a subclass of SavedGameDataStorer, PlayerPrefsSavedGameDataStorer, or DiskSavedGameDataStorer. Put this subclass on your Save System GameObject in place of PlayerPrefsSavedGameDataStorer or DiskSavedGameDataStorer. (If you're using the Dialogue System, the Save System component may be on your Dialogue Manager GameObject.)

Use the original class's base methods for Standalone builds (e.g., Windows/Mac/Linux). But add "#if UNITY_SWITCH" conditional code for Switch that runs instead of the base code when you compile for Switch.

Unity doesn't have built-in PlayerPrefs support on Switch. However, you can download a PlayerPrefs package from the Nintendo dev forum that adds PlayerPrefs for Switch. The only major difference is that you need to call a method to initialize PlayerPrefs first on Switch. However, you can use PlayerPrefs this way to save locally on the Switch and Standalone.

Alternatively, you can use a subclass of DiskSavedGameDataStorer to save to local disk files on Standalone, add conditional code for Switch that uses the Nintendo SDK API to save to the cloud.
n_hagialas
Posts: 46
Joined: Wed May 03, 2017 4:34 pm

Re: Porting Game to Nintendo Switch

Post by n_hagialas »

Yes, that is understood about the NDA, and thanks for the direction!

1. So you mentioned the PlayerPrefsSavedGameDataStorer and then also the Save System component in your 3rd paragraph, it is mainly PlayerPrefsSavedGameDataStorer that needs to be replaced though right? (We have both of these classes on our Dialogue Manager GameObject)
2. When creating a subclass, would it just be a blank class that just inherits from PlayerPrefsSavedGameDataStorer? Is that all that is needed? And using it, what would that look like?
3. When replacing PlayerPrefsSavedGameDataStorer, can we just disable it and add a new component or does it need to be removed and the new subclass component added?

Thanks!
User avatar
Tony Li
Posts: 23251
Joined: Thu Jul 18, 2013 1:27 pm

Re: Porting Game to Nintendo Switch

Post by Tony Li »

Hi,

The only component you need to replace is whatever SavedGameDataStorer component is on the Dialogue Manager GameObject (e.g., PlayerPrefsSavedGameData). You have to remove the original PlayerPrefsSavedGameDataStorer, not just disable it.

It might be easiest if you made a subclass of PlayerPrefsSavedGameDataStorer or DiskSavedGameDataStorer and added "override" methods to override the relevant methods. For example, if you extend PlayerPrefsSavedGameDataStorer, it might start something like:

Code: Select all

public class CustomSavedGameDataStorer : PlayerPrefsSavedGameDataStorer
{
#if UNITY_SWITCH && !UNITY_EDITOR
        // PlayerPrefsSwitch plugin requires explicit initialization and termination:
        private void Start() => PlayerPrefsSwitch.PlayerPrefsSwitch.Init();
        private void OnDestroy() => PlayerPrefsSwitch.PlayerPrefsSwitch.Term();
#endif
}
If you extend DiskSavedGameDataStorer to save to disk on Standalone but the fs API on Switch:

Code: Select all

public class CustomSavedGameDataStorer : DiskSavedGameDataStorer
{
    public override void StoreSavedGameData(int slotNumber, SavedGameData savedGameData)
    {
#if UNITY_SWITCH && !UNITY_EDITOR
        // Your NDA code here to save a local file using the Nintendo fs API
#else
        base.StoreSavedGameData(slotNumber, savedGameData);
#endif
    }
}
n_hagialas
Posts: 46
Joined: Wed May 03, 2017 4:34 pm

Re: Porting Game to Nintendo Switch

Post by n_hagialas »

Thanks Tony, I'll give this a try and report back! Much appreciated!

-Nik
n_hagialas
Posts: 46
Joined: Wed May 03, 2017 4:34 pm

Re: Porting Game to Nintendo Switch

Post by n_hagialas »

Hey Tony,

Working through this now.. Starting off, we replaced the player prefs saved storer with a disk storer, got that to work, then replaced that disk storer with a custom disk storer containing the #If Switch code you provided. Thank you for this!

Say the player hasn't saved to device on Switch yet, how can we get the currently loaded data that is 'to be saved'? How can we access the serialized data that would be saved, if say, we used the default disk save on windows. I plan to push this string of save data to Switch.
User avatar
Tony Li
Posts: 23251
Joined: Thu Jul 18, 2013 1:27 pm

Re: Porting Game to Nintendo Switch

Post by Tony Li »

Hi Nik,

If you're making a subclass of DiskSavedGameDataStorer or a custom SavedGameDataStorer subclass, override the StoreSavedGameData() method. This method receives the saved game data in an object of type SavedGameData.

However, if you ever need to access the Save System's current saved game data in memory, you can use SaveSystem.currentSavedGameData. To tell the Save System to update this info from the savers in the scene, call SaveSystem.RecordSavedGameData(). However, the Save System will generally take care of all of this for you, and you use need to implement the SavedGameDataStorer virtual methods.
n_hagialas
Posts: 46
Joined: Wed May 03, 2017 4:34 pm

Re: Porting Game to Nintendo Switch

Post by n_hagialas »

The method to save that I need to call takes 2 string parameters, one is the "data" and the other is the location. Is there a way to stringify the SavedGameData that is passed into the StoreSavedGameData() method's 2nd parameter?
User avatar
Tony Li
Posts: 23251
Joined: Thu Jul 18, 2013 1:27 pm

Re: Porting Game to Nintendo Switch

Post by Tony Li »

Yes!

Code: Select all

string s = SaveSystem.Serialize(savedGameData);
This will actually serialize it using whatever DataSerializer component is on the SaveSystem component's GameObject. By default it's JsonDataSerializer, so it will serialize savedGameData into a JSON string using JsonUtility.
n_hagialas
Posts: 46
Joined: Wed May 03, 2017 4:34 pm

Re: Porting Game to Nintendo Switch

Post by n_hagialas »

Wow thank you for the swift reply on the weekend!

Ok that makes sense, I was looking for that function!
So subsequently, when loading, we would perform a deserialize into a SavedGameData object and then do we need to run another method to apply the data to the SaveSystem? Like:

Code: Select all

SaveSystem.Deserialize<SavedGameData>(data);
Or something along these lines?

Code: Select all

public override SavedGameData RetrieveSavedGameData(int slotNumber)
    {
        Debug.LogError("TESTING SWITCH LOAD...");

#if UNITY_SWITCH && !UNITY_EDITOR
        // Load a local file using the Nintendo fs API
        // Load() happens here
        // Do I still need to apply anything to the SaveSystem here?
#else
        return base.RetrieveSavedGameData(slotNumber);
#endif
    }
Post Reply