#Emiel scene problem

1 messages · Page 1 of 1 (latest)

chrome relic
#

hello

sage ermine
#

hi

#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelLoad : MonoBehaviour
{
    public int iLevelToLoad;
    public string sLevelToLoad;

    public bool useIntegerToLoadLevel = false;

    // Start is called before the first frame update
    void Start()
    {
        SceneManager.LoadScene(PlayerPrefs.GetString("Level"));
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void OnTriggerEnter(Collider other)
    {
        GameObject collisionGameObject = other.gameObject;
        if (collisionGameObject.name == "Player")
        {
            LoadScene();
        }
    }
    void LoadScene()
    {
        Debug.Log("skjldf");
        if (useIntegerToLoadLevel)
        {
            SceneManager.LoadScene(iLevelToLoad);
            Debug.Log("skjldf");
        }
        else
        {
            //if (SceneManager.GetActiveScene().name == "Level2")
            //{
                PlayerPrefs.SetString("Level", sLevelToLoad);
                Debug.Log("skjldf");
                SceneManager.LoadScene(PlayerPrefs.GetString("Level"));
            
            PlayerPrefs.Save();
            //SceneManager.LoadScene(sLevelToLoad);
        }
    }
}
#

this is my code for now

#

but it doesnt work

chrome relic
#

you are trying to load a level

#

and play it for a while

#

and then load another level

sage ermine
#

if the level is finished

chrome relic
#

and you want that what you did in the previous level is saved when i reopen it

sage ermine
#

yes

chrome relic
#

that impossible

#

tho you can do it in the complex

#

ever heard of a word
"Game data"

sage ermine
#

np

chrome relic
#

a game data is a file/folder where the game stores its data as it progresses

sage ermine
#

i just want when i reopen the game it loads the level after all the levels i have finished

#

so for example you finish level1

#

and quit the game

#

then you reopen it

#

and you go to level2

chrome relic
#

and not again start from level 1

#

right?

sage ermine
#

no exactly

chrome relic
#

then?

sage ermine
#

not start from level1 but from level2

chrome relic
#

then you will either need to store your player prefs

sage ermine
#

thats what i did in the code

chrome relic
#

ever heard of binary data storage

sage ermine
#

no

chrome relic
#

player prefs wont work exactly as you want them to

sage ermine
#

oh

chrome relic
#

ever heard of json

sage ermine
#

yes

#

i am experienced in web dev

chrome relic
#

its a serializable format that can store data in a specific layout and you can parse it to get the required values of a class back

sage ermine
#

ok

chrome relic
#

well then lets write a json instead of binary

sage ermine
#

ok

chrome relic
#

how about a binary formatter

#

its more easier than json and binary

#

?

sage ermine
#

ok but what is it

chrome relic
#

a binary formatter will serialize your data which you provide it as input into microsoft specified serialized output

#

its also binary with a header

sage ermine
#

ok and what do i have to write in it

chrome relic
#

lets take as example

#

how many levels do your game has?

sage ermine
#

currently 3

chrome relic
#

ohk then

#

first of all lets write our binary class that can contain that value

#

so that its your file you always need a verifier

sage ermine
#

ok

chrome relic
#

i keep the verifier as a Long value

#

which is an Int64

sage ermine
#

ok

chrome relic
#

wait a while till i write the code

sage ermine
#

sure

chrome relic
#

here

#
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

public class BinaryData : MonoBehaviour
{
    public Int64 header = 817272;
    public UInt16 level = 0;

    public void SaveBinary(BinaryData data)
    {
        if (data == null)
            return;

        string saveBinaryPath = Application.persistentDataPath;
        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream ms = new MemoryStream();
        bf.Serialize(ms, data);

        byte[] playerdata = ms.ToArray();

        File.WriteAllBytes(saveBinaryPath + "PlayerData.Binary", playerdata);
    }
    public void LoadBinary(BinaryData retrievedData)
    {
        string BinaryPath = Application.persistentDataPath;
        string fullPath = BinaryPath + "PlayerData.Binary";

        byte[] PlayerData = File.ReadAllBytes(fullPath);
        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream ms = new MemoryStream();
        ms.Write(PlayerData, 0, PlayerData.Length);
        retrievedData = (BinaryData)bf.Deserialize(ms);
    }    
}```
#

your code

sage ermine
#

wow

chrome relic
#

that,s a class from which you can store your binary values and save it to the directory

#

let me guide on how to use it

#

or can you figure it out yourself

sage ermine
#

thanks for doing this

chrome relic
#

if you want to store more values then you can just add more fields

#

like add a field named

#

public string message;

#

and then save it as well to binary

sage ermine
#

can you tell me where i have to store the levels

chrome relic
#

i have already added a path

#
        string fullPath = BinaryPath + "PlayerData.Binary";```
sage ermine
#

oh ok

chrome relic
#

Application.persistentDataPath

#

tho you can modify it as well

sage ermine
#

ok

chrome relic
#

your choice

sage ermine
#

and how can i dynamically render the levels

chrome relic
#

you mean when you close a level and then reopen you are from where you were standing

#

?

sage ermine
#

yes

chrome relic
#

then it goes to more data

#

you can add more fields like vector3 for position when the game is closed

sage ermine
#

ok

chrome relic
#

and most game has an auto saving feature which means that as you progress the game saves the data

#

you can keep that as well

#

that,s the most basic

sage ermine
#

and where do i have to assign the script

chrome relic
#

its not a script

#

its a code

#

you will have to write a script using this class

#

that,s a binary class from which you can store your data and retrieve it

sage ermine
#

oooohhhh

chrome relic
#

if you dont know how to use it then you can ask in channel

sage ermine
#

so what does the script have to contain

chrome relic
#

i just had to help with the data save and load

#

i am deleting the thread so make sure you have saved