C
C#•2y ago
_MRV-MD_

❔ Trying to save location data using JSON. Need help!

I am very new to coding, and I need to make 3 different scripts (or one script with all 3 functions) for an assignment, but am very stuck on one part. I need to be able to store object locations, player, NPC and world space into JSON. I managed to get a stats script to save, but referencing the location and saving is the barier. Any ideas? I attach my script for the inventory.
16 Replies
Angius
Angius•2y ago
What doesn't work about it? Also, Unity I presume?
DKMK100
DKMK100•2y ago
can't you just copy over the location from the original object to your own save data object?
_MRV-MD_
_MRV-MD_OP•2y ago
The inventory script works, just wondering if I can tweak it to add location too. I don't know how to reference it properly in order to get it to save. That's the thing, I don't know.how to write it 😅. I can understand code, and know how things work, but for the life of me I can't write it out
ascpixi
ascpixi•2y ago
as you already have SaveToJson and LoadFromJson, i'm pretty sure you can simply assign the Vector3 to the object before serializing it, and then assign it back to the target object after de-serializing it
_MRV-MD_
_MRV-MD_OP•2y ago
What would that look like? Again, I can't write proper lines to save my life
ascpixi
ascpixi•2y ago
dunno if this counts as spoon-feeding, but i hope not... ^^'> assuming this is the object you want to serialize
this.Location = theObjectYouWantToReadTheLocationFrom.position;
this.Location = theObjectYouWantToReadTheLocationFrom.position;
then after you de-serialize it, you'd just do the opposite oh wait you're serializing a single field of a object well i'd save it all to a single json file, something like playerdata.json i recommend using JSON.NET instead since it's much more powerful if you do need to serialize something complex with the default JsonUtility, it only serializes fields meaning you'd have to do something like PlayerStateSnapshot that would copy all the data that you need to save the methods that this class would implement would be as follows:
class PlayerStateSnapshot
-> static void Restore(Player target)
-> static PlayerStateSnapshot Save(Player target)
-> string Serialize()
class PlayerStateSnapshot
-> static void Restore(Player target)
-> static PlayerStateSnapshot Save(Player target)
-> string Serialize()
with JSON.NET you would be able to avoid this though
_MRV-MD_
_MRV-MD_OP•2y ago
I see. I'll try it and come back if anything goes wrong. Thanks for the input 🤝
_MRV-MD_
_MRV-MD_OP•2y ago
Got this so far, but its telling me "gameObject" cannot be found, missing assembly reference? What have I got wrong?
_MRV-MD_
_MRV-MD_OP•2y ago
Can I replace it with a certain tag? So I can just tag NPC and Player and have it take those coords and store it
ascpixi
ascpixi•2y ago
uuuh, well, that's a field are you sure you familiarized yourself with C#? i'd recommend learning OOP + C# before going into unity :p the syntax for a field member is <modifiers> <type> <name> = <object initialization, appended to the constructor>
_MRV-MD_
_MRV-MD_OP•2y ago
see this is for my college course, I'm more of an art guy, but this module is mandatory 😅 Im just tryin to pass the module so I can go further into design next year
ascpixi
ascpixi•2y ago
oh, i see
ascpixi
ascpixi•2y ago
if you have some time to spare take a look at https://www.youtube.com/watch?v=GhQdlIFylQ8
freeCodeCamp.org
YouTube
C# Tutorial - Full Course for Beginners
This course will give you a full introduction into all of the core concepts in C# (aka C Sharp). Follow along with the course and you'll be a C# programmer in no time! ⭐️ Contents ⭐️ ⌨️ (0:00:00) Introduction ⌨️ (0:01:18) Installation & Setup ⌨️ (0:05:03) Drawing a Shape ⌨️ (0:17:23) Variables ⌨️ (0:30:06) Data Types ⌨️ (0:37:17) Working With S...
ascpixi
ascpixi•2y ago
if not then the sololearn.com course is also pretty good :p do you have experience with other langs?
_MRV-MD_
_MRV-MD_OP•2y ago
We touched on java in the first year, but that was almost 2 years ago
Accord
Accord•2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?