#Debugging
1 messages Β· Page 1 of 1 (latest)
aye
are you able to hover the values?
what values u think im talking about
make sure the breakpoint actually hit though
i think it did
well how do i know it did
it'll be
π aye thats one good step
im hovering
π
jesus
ok which values can you see
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
yes we know this for sure from the earlier NUllRef
yes
LocalRow/DefaultCompany π€£
SaveFolder "C:/Users/myname/AppData/LocalLow/DamseyDiou/Eternal Dusk\saves" string
like this ?
yes
saves is the folder
can you also send script again I lost it
alright
^ yea see SAV file
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
what in notepad?
its a textfile
{"PlayerPosition":{"x":373.24957275390627,"y":19.842966079711915,"z":367.2515563964844}}
yeah
printyprint :ahem:
no becasue you don't have that
what am i supposed to be doing
can u also screenshot ur editor that has this script
yeah
and verify by searching hierarchy t:SaveLoad
ah yes you have to stop the debugger
press stop button
in VS
π₯
did you do this
where it is
search that inside the hierarchy
... eh how
there should be at least 1
its the 1 u screenshot us
maybe you have dyslexia or
ok I'm trying to think why it would be null. lemme test something..
also send your other scripts you copied
i was also testing something
theres only one
only one other script
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;
}
}
yeah
i knew it was my issue
i just didn't know what
notice i never blamed you, the creator of the tutorial
wait did i say that
i meant "to the best of my abilities"
but my abilities are pretty shit
no worries
so, what did i do
it's literally 1 line missing
oh no
ok let me see if I can make you see it
what is it?
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
alright
if we look at how we set the path inside save
my brain is at 3% power rn
omg
do you see what I mean now?
did i put them the wrong way round?
no no
or...
hint : filename
in my head
but i thought it can't be that
but why is one camelcase and the other lowercase
wdym
filename and fileName
just me being tired at 3AM
oh
there is no difference at all
i know
functionally
but i was confused
yay!
5D chess moves
roughly how large would the file size of like, 1000 different variables be
i know its different every time
a few megabytes maybe
it really is much better then playerprefs aint it π
level count, settings, etc
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
π€·ββοΈ
what?
what kind of setting doom the user
all i have is sensitivity and inversion
thats all i need
the rest is on the player
video settings i suppose
yeah i don't know how to do those π
you never put the wrong resolution on a game and it was black screen or bigger than your supported monitors?
on any game
not really
well games today are different
most games only allow the size of the monitor maximum
so thats not possible
and if it is, i can just emulate a resolution
not true always.
if the game window is bigger than the resolution on your monitor it's cutoff menu and can't click stuff
i know what you're saying
but i can do this
π€·ββοΈ 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
just saying playerprefs is annoying for a front-end user to mess with settings intsead of just going into file
the average person doesn't know how to do either
what would the best way to save which gameobjects in a list are active
depends what do you want to store, it can just be a simple bool or just only add the quantity to save
i have a lot ofpickupables, i want to store which ones have been picked up
this one can get complex actually.
you would need to store them as items with unique iDs
that makes sense
like you can store what was picked up easily
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
yeahhh
yeah thats the purpose
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...
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
would it not just be best to have a lot of bools
would that not be the best way to do it
easier would be to only tell the spawner how many to spawn instead
so if spawner spawns normally 10 items.
you pick up 8 then next time you load it only spawns 2
i wanna spawn them i random places but idk how without just having tons of checkpoints
but then they might be in the same place as one you already picked up
either that or math..
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
exactly now you see how it quickly grows to knowiing much more about the item itself
what would that do?
you need to store the item itself
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
how would you tell which one to set active false π
as i just said
you store in a bool on save, whether or not its active
then on load, setactive(thebool)
would that work?
each item needs to have that bool tho
wasPickedUp or something
I mean you can, but it gets messy
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
when the item saves itself with the bool, how does it tell whos who (json file onloading)
right
and a bool for each item
would that work?
i would think that is literally the most optimised it can get
if it was that simple people would do it
because everything beyond that is, like, more
well whats the problem with it
i see no issue
i have done this now
it works
why don't people just do that
huh ? do what
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
show me
how you store it
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
you don't see anything wrong with that?
it works
it doesn't scale well
thats all i need
well no
its unoptimised, i said
but it works
and i only need 3
so, im not bothered
alr but that should answer this #1137392799524859935 message
it doesn't
?
because they scale well
oh
interesting
lists / collections are used
yeah
but dictionary lets you store more complex data
just learn those first then put it on a saving context
so would it be like...
it's like a very powerful key / value collection
how is it different to a list
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
so dictionary you can make the Key anything
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
how?
i thought it was just string then int
what is T
type
oh
lemme see the code
ammoboxes[item] = true;
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
item is a KeyvaluePair
not GameObject
yeah
because its a thing on dictionary
but how do i edit the value
and how do i assign the gameobject
so many questions
you can just loop through the Keys
what?
foreach (var gameObject in ammoBox.Keys)
{
ammoBox[gameObject] = true;
}```
oh i see
well i don't want to change the gameobject and im not sure why thats allowed
but either way
you're not changing the gameObject
you can name it whatever you want
gameObject is the Key
and is type GameObject
foreach (bool item in ammoboxes.Values)
{
ammoboxes[item] = true;
}
well it stops working when i do this
because it's wrong
well cheers mate
you're using the value as a key
well what do i do then
why did you change it from what i've sent at all
i have not
myarray[index]
you should probably learn these things before attempting your own ways to make save stuff
collections are the core concept of saving
it's okay to not know if you don't know, just don't make it confusing on yourself
they're pretty similar to lists too
list is an array
i do know them
alright, so....
thats why .Count instead of .Length
i gotta figure this out now
foreach (var item in ammoboxes.Keys)
{
ammoboxes[item] = item.activeSelf;
}
would it be this?
wait no
storing as GameObject is worthless
foreach (var item in ammoboxes.Keys)
{
ammoboxes[item] = item == null;
}
but...
how do i assign the ammoboxes to it
wdym assign ammoboxes
how do i add the gameobjects themselves to the dictionaries
but...
im really confused
im trying to get whether or not the gameobject is null
how is it gonna know what gameobject to do?
hey remember i mentioned why this is complex ?
storing them as unique item is the whole thing
so what do i do?
give them unique ids
store the whole class / info about the item
like
Dictionary<string, ItemData> ammoBox
no shit
save systems range in complexities
you can make it simpler but requires good thought out of your particular use case
why is all this necessary just to get a list of whether or not an item is active
how else would you do it
