Custom Class problem

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
Locked
hgulgen
Posts: 7
Joined: Tue Mar 26, 2019 5:34 pm

Custom Class problem

Post by hgulgen »

Hello I am new on coding Can you give me an example How can I save custom class like below ?

Code: Select all

using UnityEngine;
using System.Collections;
using System; //This allows the IComparable Interface


//Bu kod silahlar için hangi dataları saklayacağını belirliyor.
public class Naau_WeaponData : IComparable<Naau_WeaponData>
{
    public string WeaponName;
    public bool OwnIt;
    public bool Active;
    public int power;

    public Naau_WeaponData()
    {
    }

    public Naau_WeaponData(string newName, bool newOwnIt,bool NewActive,int NewPower)
    {
        WeaponName = newName;
        OwnIt = newOwnIt;
        Active = NewActive;
        power = NewPower;
    }

    public int CompareTo(Naau_WeaponData other)
    {
        if (other == null)
        {
            return 1;
        }

        //Return the difference in power.
        return power - other.power;
    }

}

I created this type but It still give error.

Code: Select all

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


public class ES2UserType_Naau_WeaponData : ES2Type
{
    public override void Write(object obj, ES2Writer writer)
    {
        Naau_WeaponData data = (Naau_WeaponData)obj;
        // Add your writer.Write calls here.
        writer.Write(data.WeaponName);
        writer.Write(data.Active);
        writer.Write(data.OwnIt);
        writer.Write(data.power);
    }

    public override object Read(ES2Reader reader)
    {


     

        Naau_WeaponData data = new Naau_WeaponData();
        Read(reader, data);
        return data;
    }

    public override void Read(ES2Reader reader, object c)
    {
        Naau_WeaponData data = (Naau_WeaponData)c;
        // Add your reader.Read calls here to read the data into the object.
        string WeaponName = reader.Read<string>();
        bool Active = reader.Read<bool>();
        bool OwnIt = reader.Read<bool>();
        int power = reader.Read<int>();

    }

    /* ! Don't modify anything below this line ! */
    public ES2UserType_Naau_WeaponData() : base(typeof(Naau_WeaponData)) { }
}

I couldnt find any example I actually find usefull informations but ı couldnt solve it. How Can I save this kind of data. I hope I am clear :.(
User avatar
Joel
Moodkie Staff
Posts: 4844
Joined: Wed Nov 07, 2012 10:32 pm

Re: Custom Class problem

Post by Joel »

Hi there,

When you are reading your variables, you are reading them into local variables, which will do nothing. You need to read them into the relevant fields of the instance.

i.e.
public override void Read(ES2Reader reader, object c)
    {
        Naau_WeaponData data = (Naau_WeaponData)c;
        // Add your reader.Read calls here to read the data into the object.
        data.WeaponName = reader.Read<string>();
        data.Active = reader.Read<bool>();
        data.OwnIt = reader.Read<bool>();
        data.power = reader.Read<int>();

    }
Note that this can be done automatically using the Manage Types window, so you do not need to write code:
https://docs.moodkie.com/easy-save-2/gu ... her-types/

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
hgulgen
Posts: 7
Joined: Tue Mar 26, 2019 5:34 pm

Re: Custom Class problem

Post by hgulgen »

Thanks for your answer.I use to plan es3 for saving this data but When I create as custom type as in documentation , It gives me error but I created a new project , I almost did same thing , It is working in new project. I can created succesfull custom type. But if use in my project, It gives error.

Error is below:
Assets\Easy Save 3\Types\ES3Type_Naau_WeaponData.cs(52,23): error CS7036: There is no argument given that corresponds to the required formal parameter 'newName' of 'Naau_WeaponData.Naau_WeaponData(string, bool, bool, int)'

What does it mean ? Help can be save my life Thanks :.(
hgulgen
Posts: 7
Joined: Tue Mar 26, 2019 5:34 pm

Re: Custom Class problem

Post by hgulgen »

Yes I guess , I solved my problem , As you said because of local variables. It is working very well.
Locked