playMaker

Author Topic: Custom save scene action  (Read 1908 times)

Seiryu33

  • Full Member
  • ***
  • Posts: 133
Custom save scene action
« on: September 23, 2019, 03:01:59 PM »
I don't know if this goes here. Okay so i'm dabbling in coding, and I'm trying to write an action that saves and loads the last scene that was played but at specific points. I was able to write an action for ES3 but it only saves and loads the CURRENT scene, I want to be able to load any scene. Here's the code I got from EasySave:

Code: [Select]
using System;
{
          [ActionCategory("Easy Save 3")]
          [Tooltip("Triggers Auto Save's Save Method.")
          public class ES3AutoSaveSave : FsmStateAction
                 public override void OnEnter()
                 {
                          GameObject.Find("Easy Save Manager").Get component<ES3AutoSaveMgr().Save();
                          }
}

              [ActionCategory("Easy Save 3")]
              [Tooltip("Triggers Auto Save's Load Method")
              public class ES3AutoSaveLoad : FsmStateAction
              {
                    public override void OnEnter()
                   {
                             GameObject.Find("Easy Save 3 Manager").Get component<ES3AutoSaveMgr>.Load();

How would I modify this to work with scenes instead of objects. Would replacing GameObject with SceneIndex work? Or if there's a better script that would make it more efficient that would be helpful.
« Last Edit: September 25, 2019, 12:18:59 PM by djaydino »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Custom save scene action
« Reply #1 on: September 25, 2019, 12:31:11 PM »
Hi.
I Wraped you code so its easier to see.

But i do not think that you can load / save when the scene is not active.
and you can not load/save on a Scene, only on an object in the scene.

Can you explain more on what do you need to save /load
and why you need to be able to load it in a different scene?

Maybe there is a different solution

Seiryu33

  • Full Member
  • ***
  • Posts: 133
Re: Custom save scene action
« Reply #2 on: September 26, 2019, 06:17:33 PM »
I'm basically trying to write an action to load the last scene that was played. I've run through several tutorials but I don't think any if the scripts I saw will work with Playmaker. So for instance whenever the main main loads a new game I want to be able to save and load the last scene saved from the main menu not just the scores and things of that nature.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Custom save scene action
« Reply #3 on: September 27, 2019, 05:55:20 AM »
Hi,
to get the last scene :

Before Exiting last scene, do : 'Get Scene Name' and save the string (use 'Last Scene' as  Tag/Key for example).
Use a general 'filename'  (call it 'General' for example).
Also Save whatever you need to save from that scene and use the scene name as 'filename'

@ the start of the same scene use 'Get Scene Name'.
Then use 'File Exists' with the 'Last Scene' Variable.
If exists :
Load everything you need and set 'filename' with the 'Last Scene' Variable.

From another scene you can get data using the scene name as the file name.
and the General filename to get the 'Last Scene'

Edit: Before Exiting last scene you actually do not need to do 'Get Scene Name' when you do this at the start.

Seiryu33

  • Full Member
  • ***
  • Posts: 133
Re: Custom save scene action
« Reply #4 on: September 27, 2019, 01:36:09 PM »
Do I leave the Scene Reference field blank?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Custom save scene action
« Reply #5 on: September 28, 2019, 08:11:43 AM »
Hi.
Get Scene name should be 'active scene'

To save, if you use easy save 3 actions, you need to 'check' Override Default Settings.
and instead of file name use the 'Path' Variable.
IF you need to add the .es3 extension you can use 'Build String'

Seiryu33

  • Full Member
  • ***
  • Posts: 133
Re: Custom save scene action
« Reply #6 on: September 30, 2019, 08:28:37 AM »
So using the Custom Action Tool, I wrote a couple of new actions but they don't load scenes at all which is why I don't commonly do traditional coding. These were from a tutorial but I don't understand why they don't work.

First Action:

Code: [Select]
using System.Collections:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace HutongGames.Playmaker.Actions
{


          [ActionCategory(ActionCategory.Scene)]
          public class LoadSaveScene : FsmStateAction
          {
                 private int sceneToContinue;

                  public void ContinueGame()
                  {


                          sceneToContinue = Playerprefs.GetInt("SavedScene");

                          if (sceneToContinue != 0)
                                 SceneManager.LoadScene(sceneToContinue);
                           else
                                  SceneManager.LoadScene(0);
                }
          }
}

Second Action:

Code: [Select]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace HutonGames.Playmaker.Actions
{

         [ActionCatrgory(ActionCategory.Scene)]
         public class SaveSceneAs : FsmStateAction
         {

                private int current cane index;

                Public void LoadTitleScreen()
                {

                       currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
                       PlayerPrefs.SetInt("SavedScene", CurrentSceneIndex);
                       SceneManager.LoadScene(0);
           
                 }
          }
}

The first action is scripted to load the last saved scene and the second is scripted to saves the last scene. Any guesses on why the scenes aren't loading?

Edit: I checked the scripts in the Project folder and they each had the error message "No Monobehaviour scripts in the files or their names do not match the file name." The error only turns up after I place the scripts after namespace HutongGames.Playmaker.Actions. Is there a line of code I'm missing?
« Last Edit: October 01, 2019, 08:26:27 AM by djaydino »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Custom save scene action
« Reply #7 on: October 01, 2019, 08:37:52 AM »
Hi.
I updated your post again so the code is readalbe. please use the # button to wrap the code :)

The methods on the scripts you made are never called so they will not work.

you need to add this :

Code: [Select]
public override void OnEnter()
{
                        DoMethod();

Finish();
}

Replace DoMethod() with ContinueGame() and LoadTitleScreen()

Also have a look at this tutorial to understand better how to make actions :)