Custom saver

Announcements, support questions, and discussion for Quest Machine.
Post Reply
GorkaGames
Posts: 178
Joined: Fri Sep 21, 2018 8:38 pm

Custom saver

Post by GorkaGames »

I'm trying to code my own custom saver.
It's a very easy one getting the enum state from a component <ChestInteractable> on the same gameobject on the saver, where we can find a variable with the actual state of the chest (bloqued, closed, open)
But i'm getting in troubles as I can't access to my class as the editor doesn't recognize my classes.
Could you help me a bit how do I start? And how do I write to serialize a easy enum variable?

Thanks.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace PixelCrushers
{

public class SaverChest : Saver
{
public enum EstadoChest { Bloqueado, Cerrado, Abierto };
public EstadoChest EstadoChestActual;


public override string RecordData()
{
/// This method should return a string that represents the data you want to save.
/// You can use SaveSystem.Serialize() to serialize a serializable object to a
/// string. This will use the serializer component on the Save System GameObject,
/// which defaults to JSON serialization.
///
gameObject.GetComponent<ChestInteractable>().estadoChest //Doesn't find the class :(


return string.Empty;
}

public override void ApplyData(string data)
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom saver

Post by Tony Li »

Hi,

Are you sure the SaverChest script is on the same GameObject that has the ChestInteractable component?

In any case, try the version below. It may provide more info. It assumes EstadoChest is defined somewhere else.

SaverChest.cs

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace PixelCrushers
{
    public class SaverChest : Saver 
    {
        public override string RecordData()
        {
            var chestInteractable = GetComponent<ChestInteractable>();
            if (chestInteractable == null)
            {
                Debug.LogError("SaverChest.RecordData: No encontré ChestInteractable en " + name, this);
                return string.Empty;
            }
            return ((int)chestInteractable.estadoChest).ToString();
        }

        public override void ApplyData(string data)
        {
            if (string.IsNullOrEmpty(data)) return;
            var chestInteractable = GetComponent<ChestInteractable>();
            if (chestInteractable == null)
            {
                Debug.LogError("SaverChest.ApplyData: No encontré ChestInteractable en " + name, this);
                return;
            }
            chestInteractable.estadoChest = (EstadoChest)SafeConvert.ToInt(data);
        }
    }
}
GorkaGames
Posts: 178
Joined: Fri Sep 21, 2018 8:38 pm

Re: Custom saver

Post by GorkaGames »

LOL, I didn't know you speak Spanish :)

Thnaks very much for the code, I wouldn't be able myself to write the Enum to String conversion.

But the issue I've got is that my class <ChestInteractable> that usually is accesible from all my scripts (I don't use any namespace for it) it's nos accesible from this saver class. I have tried just in case with wathever other class on my project and about the same. Visual Studio doesn't allow me to add GetComponent<ChestInteractable>() as it marks in red and says: class ChestInteractable didn't find it

Here you can see it:
https://1drv.ms/u/s!AgOs7p5LnVflkMAe9dKhy2-sT8LKCQ

Must be some restriction from your saver subclass or something.....

EDIT:
On the other hand, I can access to: public enum EstadoChest, (you can see on the screenshot) but not other classes on my project.
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom saver

Post by Tony Li »

Is ChestInteractable a MonoBehaviour? In other words:

Code: Select all

public class ChestInteractable : MonoBehaviour
{...
Can you share a screenshot of it?
GorkaGames
Posts: 178
Joined: Fri Sep 21, 2018 8:38 pm

Re: Custom saver

Post by GorkaGames »

This is the beginning..... But I can't see other "normal" MonoBehaviour classes either.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using Rewired;
using Opsive.UltimateCharacterController.Events;
using Opsive.UltimateCharacterController.Objects;
using Opsive.UltimateCharacterController.Traits;
using Opsive.UltimateCharacterController.Demo.BehaviorDesigner;
using BehaviorDesigner.Runtime;
using PixelCrushers;
using PixelCrushers.QuestMachine;


public class ChestInteractable : MonoBehaviour, IInteractableTarget, PixelCrushers.IMessageHandler
{
[Tooltip("Can the interactable be interacted with only once?")]
[SerializeField] protected bool m_SingleInteract;

public enum EstadoChest {Bloqueado, Cerrado, Abierto};
public EstadoChest EstadoChestActual = EstadoChest.Bloqueado;
public enum NPC {NoIdentificado,Player,Companion,Enemy};
public NPC NPC_actual = NPC.NoIdentificado;
public GameObject hud;
[Tooltip("Parametro a utilizar en Quest Machine en el Acive y en el Condition. Si queremos utilizar varios cofres ponemos un contador con el mensaje: ChestOpened")]
public string QuestParameterMessage = "IMPORTANTE: A rellenar, mismo que en Quest Machine";

private string QuestMessageToActivate = "ActivateChest";
private string QuestMessageOpened = "ChestOpened";
private string QuestMessageToDeativated = "ChestDeativated";

private bool m_HasInteracted;
private PlayerOpsive player;
private bool canInteract = true;
private bool esElPlayer;
private AIagent AgentAI;
private bool MandaMensajeChestOpened = true;
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom saver

Post by Tony Li »

Are you using Assembly Definitions?

If not, even if Visual Studio highlights it in red, does it work correctly in Unity?
GorkaGames
Posts: 178
Joined: Fri Sep 21, 2018 8:38 pm

Re: Custom saver

Post by GorkaGames »

Solved. I moved the script from templates to my script folder and works fine, I think it was something related to Assembly Definitions and Visual Studio.
Great
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom saver

Post by Tony Li »

Great! :-)
Post Reply