#Debugging

1 messages Β· Page 1 of 1 (latest)

red wind
#

yo

silent hazel
#

aye

red wind
#

are you able to hover the values?

devout ivy
#

men

#

uhhhh

#

hover the values?

#

what does that mean

red wind
#

ok

#

take a step back and think

#

we're here using debugger right

#

to debug inside VS

devout ivy
#

yeah

#

do you mean hover my mouse over it

red wind
#

what values u think im talking about

devout ivy
#

yeah i get it now

#

but hover might be a method

red wind
#

make sure the breakpoint actually hit though

devout ivy
#

i think it did

red wind
#

dont think

#

be sure

devout ivy
#

well how do i know it did

red wind
#

told you earlier lol

#

arrow + highlights

devout ivy
silent hazel
#

it'll be

devout ivy
#

yeah

#

its got that

#

its doing it

silent hazel
#

πŸ‘ aye thats one good step

red wind
#

ok so now hover over the values such as loadedData

#

saveFileScript

#

etc

devout ivy
#

im hovering

red wind
#

you can keep expanding them to see what is what

silent hazel
#

real quick nav..

#

how do we skip to the next point?

red wind
silent hazel
#

step forward i think they call it

#

yea

devout ivy
#

my entire life i thought it was LocalRow

#

its LocalLow

#

damn

silent hazel
#

πŸ˜„

red wind
#

jesus

devout ivy
#

how is this helping tho

#

yeah im seeing values

red wind
silent hazel
#

ur problem is code is supposed to assign a value and the error is there is no value..

#

ur looking for values taht are missing or not what they're supposed to be

devout ivy
#

loadeddata is null

red wind
devout ivy
#

yes

red wind
#

expand SaveFilesystem

#

what is the path

silent hazel
#

LocalRow/DefaultCompany 🀣

devout ivy
#

i can't say without, doxxing myself

#

but its what its supposed to be

silent hazel
#

dont say the user name

#

we dont care what that is

devout ivy
#

SaveFolder "C:/Users/myname/AppData/LocalLow/DamseyDiou/Eternal Dusk\saves" string

red wind
devout ivy
silent hazel
#

i remember ur folder screenshot show its a .sav file tho

#

not just saves

red wind
#

saves is the folder

silent hazel
#

maybe thats a clue

#

ahh ok gotcha

red wind
devout ivy
silent hazel
#

^ yea see SAV file

devout ivy
#
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SaveLoad : MonoBehaviour
{
    [SerializeField] private string fileName;

    [SerializeField] private MouseLook plrscrip;
    [SerializeField] private GameObject plr;

    private StaticDataSAve saveFileSystem;

    private void Start()
    {
        Debug.Log(saveFileSystem.ReadFile<SaveData>(fileName));
        Load();
    }
    private void Awake()
    {
        saveFileSystem = new();
    }
    [ContextMenu("Save")]
    public void Save()
    {
        var newSaveData = new SaveData();

        newSaveData.PlayerPosition = plr.transform.position;

        saveFileSystem.SaveFile(fileName, newSaveData);
    }
    [ContextMenu("Load")]
    public void Load()
    {
        var loadedData = saveFileSystem.ReadFile<SaveData>(fileName);

        plr.transform.position = loadedData.PlayerPosition;

    }
}
[Serializable]
public class SaveData
{
    public Vector3 PlayerPosition;
}
#

its in a weird state

#

but yk

red wind
#

open the file

#

show me file

devout ivy
#

what in notepad?

red wind
#

sure

#

is just a json

#

is a textfile

devout ivy
#

its a textfile

#

{"PlayerPosition":{"x":373.24957275390627,"y":19.842966079711915,"z":367.2515563964844}}

red wind
#

formatted * text file

devout ivy
#

yeah

silent hazel
#

printyprint :ahem:

devout ivy
#

i know

#

i know because of minecraft

silent hazel
#

button that opens AppData OP

devout ivy
#

do i click that

#

is this an instruction

red wind
#

no becasue you don't have that

silent hazel
#

no its an example

#

but it shows my appdata is like urs

devout ivy
#

what am i supposed to be doing

silent hazel
#

im spectating..

#

ur debugging

#

nav is helping troubleshoot

red wind
devout ivy
#

yeah

red wind
#

and verify by searching hierarchy t:SaveLoad

devout ivy
#

oop, unity won't open

#

oh i need to dismiss the pause thing

red wind
#

press stop button

#

in VS

devout ivy
silent hazel
#

πŸŸ₯

devout ivy
#

i know

#

lol

#

ignore the thing on the right

red wind
devout ivy
#

that looks a bit like someone's mary

#

i promise its not that

devout ivy
#

what

#

oh

#

yeah lemme do that

red wind
#

search that inside the hierarchy

devout ivy
#

thats nothing

#

there's nothing

silent hazel
#

... eh how

red wind
#

there should be at least 1

silent hazel
#

its the 1 u screenshot us

red wind
#

you should only get 1 gameobject yes ?

devout ivy
#

oh

#

hierarchy

#

my brain is literally quark sized

red wind
#

maybe you have dyslexia or

devout ivy
#

yeah there's only one

#

lol

red wind
#

ok I'm trying to think why it would be null. lemme test something..

#

also send your other scripts you copied

silent hazel
#

i was also testing something

devout ivy
#

only one other script

red wind
#

SaveFileSystem

devout ivy
#
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class StaticDataSAve
{
    public string SaveFolder => Path.Combine(Application.persistentDataPath, "saves");
    public string FileExtension = ".sav";

    public StaticDataSAve()
    {
        if (!Directory.Exists(SaveFolder))
            Directory.CreateDirectory(SaveFolder);
    }
    public void SaveFile<T>(string fileName, T data)
    {
        fileName = Path.Combine(SaveFolder, fileName) + FileExtension;
        var dataString = JsonUtility.ToJson(data);
        if (File.Exists(fileName))
        {
            //damseydiou is awesome
        }

        using var file = File.Open(fileName, FileMode.Create);
        using var writer = new StreamWriter(file);
        writer.Write(dataString);
        Debug.Log($"Written File to {fileName}");
    }

    public T ReadFile<T>(string filename)
    {
        if (!File.Exists(filename))
        {
            //file doesn't exist, show error
            return default(T);
        }

        using var file = File.Open(filename, FileMode.Open);

        using var reader = new StreamReader(file);

        var dataString = reader.ReadToEnd();

        var data = JsonUtility.FromJson<T>(dataString);
        Debug.Log($"Loaded data from {filename} | ");
        return data;
    }
}

red wind
#

yup

#

there is your issue

#

shit we need the digiholic counter

devout ivy
#

yeah

#

i knew it was my issue

#

i just didn't know what

#

notice i never blamed you, the creator of the tutorial

red wind
#

"I did copy exactly like tutorial"

#

i know im just joking

devout ivy
#

wait did i say that

#

i meant "to the best of my abilities"

#

but my abilities are pretty shit

red wind
#

no worries

devout ivy
#

so, what did i do

red wind
#

it's literally 1 line missing

devout ivy
#

oh no

red wind
#

ok let me see if I can make you see it

devout ivy
#

what is it?

red wind
#

lets stay on this script

#

you said saving works right?

devout ivy
#

yeah

#

i think so

red wind
#

good

#

yes it does i know for a fact

#

compare the first lines of save with load

devout ivy
#

alright

#

is it the exclamation mark

red wind
#

nah lol

#

think about it this way

#

it cant find the object

#

that means the path is probably wrong

#

so knowing that is the issue , something is wrong with the path

devout ivy
#

alright

red wind
#

if we look at how we set the path inside save

devout ivy
#

my brain is at 3% power rn

red wind
#

its literally the first line

#

of save

devout ivy
#

omg

red wind
#

do you see what I mean now?

devout ivy
#

did i put them the wrong way round?

red wind
#

no no

devout ivy
#

or...

red wind
#

hint : filename

devout ivy
#

yeah

#

ok i was right with my first guess

#

the first line is missing

red wind
#

yes

#

where did you guess that

devout ivy
#

idk why one is all lowercase and one is pascalcase

#

thats strange

devout ivy
#

but i thought it can't be that

#

but why is one camelcase and the other lowercase

red wind
#

wdym

devout ivy
#

filename and fileName

red wind
#

just me being tired at 3AM

devout ivy
#

oh

red wind
#

there is no difference at all

devout ivy
#

i know

red wind
#

functionally

devout ivy
#

but i was confused

red wind
#

I'll make an update one better at some point

#

but yeah that should fix ur issue

devout ivy
#

HOLY MOTHER OF

#

OH MY

#

IT WORKS

#

LETS GOOOOOOOOOOOOOOO

#

I CAN SAVEEEEEEEEEEE

red wind
#

yay!

devout ivy
#

MY BRAIN IS VIBRATING

#

this is so effective

red wind
#

5D chess moves

devout ivy
#

its like

#

a couple kilbytes of file size

red wind
#

also very easy to adapt to database

#

if you ever wanna save online

devout ivy
#

roughly how large would the file size of like, 1000 different variables be

#

i know its different every time

red wind
#

a few megabytes maybe

devout ivy
#

but what is considered a large file size

#

jesus christ

#

this is so good

red wind
#

it really is much better then playerprefs aint it πŸ˜›

devout ivy
#

playerprefs still good

#

for like, simple things

#

but this is good for big things

red wind
#

yeah like online token keys mostly

#

or other temp save

devout ivy
#

level count, settings, etc

red wind
#

ehh those are good to do inside a file

#

imagine a game where you accidentally put the wrong settings now when you boot your user is screwed without going into registry file..

#

easier to change a config file

#

πŸ€·β€β™‚οΈ

devout ivy
#

what?

red wind
#

maybe im just too old

#

you never put the wrong settings on a game?

devout ivy
#

what kind of setting doom the user

#

all i have is sensitivity and inversion

#

thats all i need

#

the rest is on the player

red wind
#

video settings i suppose

devout ivy
#

yeah i don't know how to do those 😜

red wind
#

you never put the wrong resolution on a game and it was black screen or bigger than your supported monitors?

#

on any game

devout ivy
#

not really

red wind
#

well games today are different

devout ivy
#

most games only allow the size of the monitor maximum

#

so thats not possible

#

and if it is, i can just emulate a resolution

red wind
#

not true always.

devout ivy
#

i can trick my monitor into thinking its 4k

#

and it will work

red wind
#

if the game window is bigger than the resolution on your monitor it's cutoff menu and can't click stuff

devout ivy
#

i know what you're saying

devout ivy
red wind
#

πŸ€·β€β™‚οΈ just saying this was a thing back then where you were able to just change / or force different resolutions in most game through a .ini file

devout ivy
#

well then

#

im not even done with secondary school

red wind
#

just saying playerprefs is annoying for a front-end user to mess with settings intsead of just going into file

devout ivy
#

the average person doesn't know how to do either

#

what would the best way to save which gameobjects in a list are active

red wind
devout ivy
red wind
devout ivy
#

i gathered

#

so i wanna know the best way

red wind
#

you would need to store them as items with unique iDs

devout ivy
#

that makes sense

red wind
#

the problem is not putting the same objects there when you come back

#

this way you can't constatly pick up the same items each load

#

most inventory tutorials do this

#

lemme see

devout ivy
#

yeahhh

devout ivy
red wind
# devout ivy yeahhh

This series will teach you how to create a minecraft style inventory system in Unity 3D - this is includes stackable items, with a max stack size and the ability to split and combine stacks, and delete them from your inventory.

I show off everything we will be doing in Episode 0. I would love to expand this series if it's positively received wi...

β–Ά Play video
#

this might help

#

it's for chest but the same concept applies for anything like backpacks or just inventory in general

#

it's part of a series so it might be confusing to just jump into only this vid

#

but it talks about storing unique items and such

#

maybe you can adapt it to simpler version for yourself

devout ivy
#

would it not just be best to have a lot of bools

#

would that not be the best way to do it

red wind
#

easier would be to only tell the spawner how many to spawn instead

devout ivy
#

they aren't spawned, they're placed

#

but i can make it spawn

red wind
#

so if spawner spawns normally 10 items.
you pick up 8 then next time you load it only spawns 2

devout ivy
#

i wanna spawn them i random places but idk how without just having tons of checkpoints

devout ivy
devout ivy
#

or someone could be like "i saw the item, i'll come back later for that"

#

and its not there

#

should i not just have 10 - 20 bools for it

red wind
#

what would that do?

#

you need to store the item itself

devout ivy
#

well if i have a bool for each item which is "item.activeSelf"

#

and on load, item.SetActive(item.activeSelf)

#

or the bool that represents it

red wind
#

how would you tell which one to set active false πŸ™‚

devout ivy
#

as i just said

#

you store in a bool on save, whether or not its active

#

then on load, setactive(thebool)

#

would that work?

red wind
#

each item needs to have that bool tho

#

wasPickedUp or something

#

I mean you can, but it gets messy

devout ivy
#

is that not the most efficient way to do it?

#

OOH

#

could i have like a 10 digit long integer

#

comprised of 1 or 0 for each item

red wind
#

when the item saves itself with the bool, how does it tell whos who (json file onloading)

devout ivy
#

huh?

#

well each item would be different

#

item_01
item_02
etc etc

red wind
#

right

devout ivy
#

and a bool for each item

#

would that work?

#

i would think that is literally the most optimised it can get

red wind
#

if it was that simple people would do it

devout ivy
#

because everything beyond that is, like, more

#

well whats the problem with it

#

i see no issue

red wind
#

the only thing I can say is tryitandsee

devout ivy
#

alright i'll start with 3

#

i gtg but i'll get back to you with the results

devout ivy
#

ok im back

#

im about to try it

devout ivy
#

it works

#

why don't people just do that

red wind
devout ivy
#

the thing i said before

#

about having a bool for each item

#

to track if its been picked up

#

that works, if i pick up and item, save, and load, its not there anymore

devout ivy
#
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SaveLoad : MonoBehaviour
{
    [SerializeField] private string fileName;

    [SerializeField] private MouseLook plrScript;
    [SerializeField] private rifleit gunScript;
    [SerializeField] private GameObject plr;
    [SerializeField] private GameObject enemy;

    [SerializeField] private GameObject ammobox1;
    [SerializeField] private GameObject ammobox2;
    [SerializeField] private GameObject ammobox3;

    private StaticDataSAve saveFileSystem;

    private void Start()
    {
        Debug.Log(saveFileSystem.ReadFile<SaveData>(fileName));
        Load();
    }
    private void Awake()
    {
        saveFileSystem = new();
    }
    [ContextMenu("Save")]
    public void Save()
    {
        var newSaveData = new SaveData();

        newSaveData.PlayerPosition = plr.transform.position;
        newSaveData.Bullets = gunScript.bullets;
        newSaveData.EnemyPosition = enemy.transform.position;
        newSaveData.ammo1 = ammobox1 == null;
        newSaveData.ammo2 = ammobox2 == null;
        newSaveData.ammo3 = ammobox3 == null;

        saveFileSystem.SaveFile(fileName, newSaveData);
    }
    [ContextMenu("Load")]
    public void Load()
    {
        var loadedData = saveFileSystem.ReadFile<SaveData>(fileName);

        plr.transform.position = loadedData.PlayerPosition;
        enemy.transform.position = loadedData.EnemyPosition;
        gunScript.bullets = loadedData.Bullets;

        if (loadedData.ammo1) 
        { 
            Destroy(ammobox1);
        }
        if (loadedData.ammo2)
        {
            Destroy(ammobox2);
        }
        if (loadedData.ammo3)
        {
            Destroy(ammobox3);
        }
    }
}
[Serializable]
public class SaveData
{
    public Vector3 PlayerPosition;
    public Vector3 EnemyPosition;

    public int Bullets;

    public bool ammo1;
    public bool ammo2;
    public bool ammo3;
}
#

big code

#

its very unoptimised

red wind
#

you don't see anything wrong with that?

devout ivy
#

it works

red wind
#

it doesn't scale well

devout ivy
#

thats all i need

devout ivy
#

its unoptimised, i said

#

but it works

#

and i only need 3

#

so, im not bothered

red wind
devout ivy
#

it doesn't

red wind
#

?

devout ivy
#

other solutions are more complex

#

they have more too them

#

this answer is simple

red wind
#

because they scale well

devout ivy
#

and effective

#

i suppose i could do a list

#

an int

#

instead of bools

red wind
#

which is what doing dictionary is

#

a collection

devout ivy
#

oh

red wind
#

var1,var2,var3

#

doesnt scale well

devout ivy
#

interesting

red wind
#

lists / collections are used

devout ivy
#

yeah

red wind
#

but dictionary lets you store more complex data

devout ivy
#

but idk how i would do that...

#

a list of gameobjects and list of bools, maybe

red wind
#

just learn those first then put it on a saving context

devout ivy
#

so would it be like...

red wind
#

you haven't used dictionaries before right

#

learn those first

devout ivy
#

i thought i had

#

lemme google

red wind
#

it's like a very powerful key / value collection

devout ivy
#

how is it different to a list

red wind
#

because you can use more than the index

#

you can use anything to lookup something

#

do you know how player prefs works?

#

same concept

#

key value pair system

devout ivy
#

my brain is hotwatered rn

#

so bear with

red wind
#

so dictionary you can make the Key anything

devout ivy
#

for example, the key could be whether or not its null

#

?

red wind
#

PlayerPrefs.SetInt("Health" , 100);

#

have you used something like this beofre ?

devout ivy
#

well not health

#

but yes i know how they work

red wind
#

just an example

#

ok

devout ivy
#

i know how dictionaries are

#

but idk if you can have the value be bool

red wind
#

it can literally be anything

#

it can be a whole class

#

or struct

#

so you would store basically what makes a certain item what it is

devout ivy
#

oh my word

#

i never knew

#

this whole time i've been using dictionaries wrong

red wind
#

how?

devout ivy
#

i thought it was just string then int

red wind
#

Dictionary<type, type>

#

or Dictionary<T, T>

devout ivy
#

what is T

red wind
#

type

devout ivy
#

oh

red wind
#

see it says TKey

#

TValue

#

GetComponent is also <T>

devout ivy
#

interesting

#

wait i can't assign a value

#

can i assign a key?

red wind
#

wdym

#

you assign a value to a key

devout ivy
red wind
devout ivy
red wind
devout ivy
#

uhh

#

that does not work

red wind
#

snippet

#
//use codeblocks```
devout ivy
#
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SaveLoad : MonoBehaviour
{
    [SerializeField] private string fileName;

    [SerializeField] private MouseLook plrScript;
    [SerializeField] private rifleit gunScript;
    [SerializeField] private GameObject plr;
    [SerializeField] private GameObject enemy;

    [SerializeField] private Dictionary<GameObject, bool> ammoboxes = new Dictionary<GameObject, bool>();

    private StaticDataSAve saveFileSystem;

    private void Start()
    {
        Debug.Log(saveFileSystem.ReadFile<SaveData>(fileName));
        Load();
    }
    private void Awake()
    {
        saveFileSystem = new();
    }
    [ContextMenu("Save")]
    public void Save()
    {
        var newSaveData = new SaveData();

        newSaveData.PlayerPosition = plr.transform.position;
        newSaveData.Bullets = gunScript.bullets;
        newSaveData.EnemyPosition = enemy.transform.position;
        foreach (var item in ammoboxes)
        {
            ammoboxes[item] = true;
        }

        saveFileSystem.SaveFile(fileName, newSaveData);
    }
    [ContextMenu("Load")]
    public void Load()
    {
        var loadedData = saveFileSystem.ReadFile<SaveData>(fileName);

        plr.transform.position = loadedData.PlayerPosition;
        enemy.transform.position = loadedData.EnemyPosition;
        gunScript.bullets = loadedData.Bullets;

        foreach (var item in ammoboxes)
        {

        }
    }
}
[Serializable]
public class SaveData
{
    public Vector3 PlayerPosition;
    public Vector3 EnemyPosition;

    public int Bullets;
}
#

here is the whole code

red wind
#

not GameObject

devout ivy
#

yeah

#

because its a thing on dictionary

#

but how do i edit the value

#

and how do i assign the gameobject

#

so many questions

red wind
#

you can just loop through the Keys

devout ivy
#

what?

red wind
#
foreach (var gameObject in ammoBox.Keys)
    {
        ammoBox[gameObject] = true;
    }```
devout ivy
#

oh i see

#

well i don't want to change the gameobject and im not sure why thats allowed

#

but either way

red wind
#

you're not changing the gameObject

#

you can name it whatever you want

#

gameObject is the Key

#

and is type GameObject

devout ivy
#
        foreach (bool item in ammoboxes.Values)
        {
            ammoboxes[item] = true;
        }

well it stops working when i do this

red wind
#

because it's wrong

devout ivy
#

well cheers mate

red wind
#

you're using the value as a key

devout ivy
#

well what do i do then

red wind
#

why did you change it from what i've sent at all

devout ivy
#

because your code is changing the keys, no?

#

i wanna change the bool

red wind
#

no?

#

where do you see it's changing the key

#

that would throw an error

devout ivy
#

"(var gameObject in ammoBox.Keys)"

#

is that not going through the keys

red wind
#

exactly

#

then you're putting the keys inside lookup

devout ivy
#

OH

#

i get it now

red wind
#

you use an array before ?

#

it's the same thing

devout ivy
#

i have not

red wind
#

myarray[index]

#

you should probably learn these things before attempting your own ways to make save stuff

#

collections are the core concept of saving

devout ivy
#

oh wait

#

those

#

yeah i know the ones

#

i was thinking of something else

red wind
#

it's okay to not know if you don't know, just don't make it confusing on yourself

devout ivy
#

they're pretty similar to lists too

red wind
#

list is an array

devout ivy
#

i do know them

red wind
#

just modifiable

#

in size

devout ivy
#

alright, so....

red wind
#

thats why .Count instead of .Length

devout ivy
#

i gotta figure this out now

#
foreach (var item in ammoboxes.Keys)
        {
            ammoboxes[item] = item.activeSelf;
        }

would it be this?

#

wait no

red wind
#

storing as GameObject is worthless

devout ivy
#
foreach (var item in ammoboxes.Keys)
        {
            ammoboxes[item] = item == null;
        }
devout ivy
#

how do i assign the ammoboxes to it

red wind
#

wdym assign ammoboxes

devout ivy
#

how do i add the gameobjects themselves to the dictionaries

red wind
#

your value is a bool

#

you can only store true or false

devout ivy
#

but...

#

im really confused

#

im trying to get whether or not the gameobject is null

#

how is it gonna know what gameobject to do?

red wind
#

hey remember i mentioned why this is complex ?

devout ivy
#

yeah

#

somewhat

red wind
#

storing them as unique item is the whole thing

devout ivy
#

so what do i do?

red wind
#

give them unique ids

#

store the whole class / info about the item

#

like
Dictionary<string, ItemData> ammoBox

devout ivy
#

jesus

#

this is getting very complex

#

like, very very complex

red wind
#

no shit

devout ivy
#

would it not be better to do something simpler

#

why is all this necessary?

red wind
#

save systems range in complexities

#

you can make it simpler but requires good thought out of your particular use case

devout ivy
#

why is all this necessary just to get a list of whether or not an item is active