Is there an Example you would like to see?

WalterE
Posts: 3
Joined: Mon Jan 05, 2015 1:51 pm

Re: Is there an Example you would like to see?

Post by WalterE »

Hello Joel,
I starting working in a smal game like clash of clans, and need save and load transform and states from Gameobjects, i see how to upload textures, but need know how to upload the transforms and states of all gameobjects.can show me a little example?
Thanks

User avatar
Joel
Moodkie Staff
Posts: 3041
Joined: Wed Nov 07, 2012 10:32 pm

Re: Is there an Example you would like to see?

Post by Joel »

Hi Walter,

Generally uploading a Transform will be the same as uploading any Component. For example, modifying the Mesh example on the ES2Web page, uploading a Transform would look like this:
public IEnumerator UploadTransform(Transform t, string tag)
{
    string myURL = "http://www.server.com/ES2.php?webfilename=myFile.txt";
    ES2Web web = new ES2Web(myURL + "&tag=" + tag);
      
    // Start uploading our data and wait for it to finish.
    yield return StartCoroutine(web.Upload(t));
      
    if(web.isError)
    {
        // Enter your own code to handle errors here.
        // For a list of error codes, see http://www.moodkie.com/easysave/ES2Web.ErrorCodes.php
        Debug.LogError(web.errorCode + ":" + web.error);
    }
}
And then to download it and assign the data to an existing Transform you can do something like ...
public IEnumerator DownloadTransform(Transform t, string tag)
{
    string myURL = "http://www.server.com/ES2.php?webfilename=myFile.txt";
    ES2Web web = new ES2Web(myURL + "&tag=" + tag);
      
    // Start downloading our data and wait for it to finish.
    yield return StartCoroutine(web.Download());
      
    if(web.isError)
    {
        // Enter your own code to handle errors here.
        // For a list of error codes, see http://www.moodkie.com/easysave/ES2Web.ErrorCodes.php
        Debug.LogError(web.errorCode + ":" + web.error);
    }
      
    // Use ES2Web.Load(string tag, Component t) to load our data and assign it to the Transform.
    web.Load<Transform>("myTag", t);
}
The Saving and Loading from Web guide has some good tutorials also which can hopefully help you along too.

All the best,
Joel

zrexa
Posts: 1
Joined: Tue Jan 13, 2015 3:10 am

Re: Is there an Example you would like to see?

Post by zrexa »

Hey Joel,

I haven't worked with Easy Save much but I think it is the write asset for the job. I was wondering if you could give me some pointers on what I am trying to accomplish, hopefully it is possible with Easy Save. I plan on having a level with a bunch of dynamic game objects in it that can be moved around. I need to save the position and rotation of each of these objects when the character goes back and forth from this level. This seems pretty doable with Easy Save. My problem is the character can also add new objects to this level, so he can bring an object back from another level and leave it in this level, and I would then need to save and load this data in addition to the other game objects in the scene. Please let me know if you have any particular examples that might help me do this or if this is not possible. Basically I need to save the equivalent of an entire scene each time the character goes back and forth from this level. I was going to try and get a list of all gameobjects and in the level save that data and then recreate it. My problem is this list isn't static it will be adding and subtracting gameobjects. Just wondering if you knew if this was possible before I started work on it. Thank you.

User avatar
Joel
Moodkie Staff
Posts: 3041
Joined: Wed Nov 07, 2012 10:32 pm

Re: Is there an Example you would like to see?

Post by Joel »

Hi zrexa,

This tutorial on Saving, Loading and Instantiating prefabs should help. Although it only shows how to do so with a single prefab, it's easy to adapt (i.e. have multiple lists, one for each different type of prefab you're saving).

All the best,
Joel

SkaiCloud
Posts: 1
Joined: Fri Mar 27, 2015 7:53 am

Re: Is there an Example you would like to see?

Post by SkaiCloud »

Hello Joel Can you show me an example of this code please?

Code: Select all

    void SaveData()
    {
        using (ES2Writer write = ES2Writer.Create("MapData.txt"))
        {
            int i = transform.childCount; //Aproximately 4k Child Object
            for (int j = 0; j < i; j++ )
            {
                write.Write(this.transform.GetChild(j).name,"BlockNameTag"); //BlockName
                write.Write(this.transform.GetChild(j).transform.localPosition,"BlockTransformTag"); //BlockPosition
                write.Save(false);
            }

            write.Save();
            Debug.Log("Save Complete");
        }
    }

    IEnumerator LoadingData() ///I don't know how to load this file. This Only load 1 blocks
    {
        using (ES2Reader reader = ES2Reader.Create("MapData.txt"))
        {
            string name = reader.Read<string>("BlockNameTag");
            Vector3 pos = reader.Read<Vector3>("BlockTransformTag");
            yield return new WaitForFixedUpdate();
        }
    }

User avatar
Joel
Moodkie Staff
Posts: 3041
Joined: Wed Nov 07, 2012 10:32 pm

Re: Is there an Example you would like to see?

Post by Joel »

Hi there,

Unfortunately we can't make examples of specific implementations, because they would only be useful to one person. However, hopefully I can give you a point in the right direction here.

In your ES2Writer code, you're just overwriting the same tag over and over again, so only one thing will ever be saved to the file. And then when you load, you're only loading one item over the file rather than iterating over the data.

If you meant to write the data sequentially, you should ignore the tag entirely, and you should only call write.Save(false) once, after all of the data has been written to the file (so not inside the loop).

Something like this:
using (ES2Writer write = ES2Writer.Create("MapData.txt"))
{
    int i = transform.childCount; //Aproximately 4k Child Object

    for (int j = 0; j < i; j++ )
    {
        write.Write(this.transform.GetChild(j).name); //BlockName
        write.Write(this.transform.GetChild(j).transform.localPosition); //BlockPosition
    }
    
    write.Save(false);
    Debug.Log("Save Complete");
}
And then when you come to load, you want to load everything in the same order as you saved it, so you'll want to create the same for-loop as you did when saving:
using (ES2Reader reader = ES2Reader.Create("MapData.txt"))
{
    int i = transform.childCount;
    
    // Iterate over each child transform and read the name and position into it.	
    for (int j = 0; j < i; j++ )
    {
        this.transform.GetChild(j).name = reader.Read<string>(); //BlockName
        this.transform.GetChild(j).transform.localPosition = reader.Read<Vector3>(); //BlockPosition
        yield return new WaitForFixedUpdate();
    }
}
All the best,
Joel

jekosto
Posts: 1
Joined: Wed Apr 01, 2015 8:48 am

Re: Is there an Example you would like to see?

Post by jekosto »

hi. i try to implement login system on my game. it may be done with another php scripts and sql access. my question is how is most viable to manage my user account saving data. can i use folder structure to my saves base to username like - " /username1/savegame.txt" so all accounts to have separate saving?
Can i if i save them this way in identical structure later to do program to manipulate a single ,lets say "stat" on all accounts at once?

User avatar
Joel
Moodkie Staff
Posts: 3041
Joined: Wed Nov 07, 2012 10:32 pm

Re: Is there an Example you would like to see?

Post by Joel »

Hi there,

The best way I find is to prepend the user's username to the filename (i.e. username_savegame.txt) as folder structures are redundant on web.

With regards to then manipulating all files: you can get an array of all of the filenames in the database using ES2Web.GetFilenames, so you can just iterate over this array and modify each file accordingly.

All the best,
Joel

skytow
Posts: 1
Joined: Tue Apr 21, 2015 5:52 pm

Re: Is there an Example you would like to see?

Post by skytow »

Could you do a simple example of saving in a file on an Android device.

thanks in advance.

User avatar
Joel
Moodkie Staff
Posts: 3041
Joined: Wed Nov 07, 2012 10:32 pm

Re: Is there an Example you would like to see?

Post by Joel »

Hi there,

Easy Save is cross-compatible, so the code to save on Android is exactly the same as the code to save on any other platform. i.e. to save and load three variables from a file in the default save directory:
ES2.Save(123, "myFile.txt?tag=myInt");
ES2.Save("a string", "myFile.txt?tag=myString");
ES2.Save(new Vector3(1f,2f,3f), "myFile.txt?tag=myVector3");

int myInt = ES2.Load<int>("myFile.txt?tag=myInt");
string myString = ES2.Load<string>("myFile.txt?tag=myString");
Vector3 myVector3 = ES2.Load<Vector3>("myFile.txt?tag=myVector3");
All the best,
Joel

Post Reply