Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411273 Posts in 69323 Topics- by 58380 Members - Latest Member: bob1029

March 28, 2024, 02:10:21 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Serialization [Unity, C#]
Pages: [1] 2
Print
Author Topic: Serialization [Unity, C#]  (Read 2470 times)
DCoward
Level 0
**



View Profile WWW
« on: July 09, 2016, 06:10:04 PM »

Hey all, I've been having a difficult time getting the hang of serialization, and it might be because to implement it, you have a lot to account for, and I am getting a bit frustrated with it because the tutorials and documentation I find is getting a bit harder for me to understand.

Could I get some advice on how to think about it or maybe what is a good way to simplistically practice it so I can work up to using it on more complex things?

Using it primarily for saving/loading, right now, if that matters.
Logged

Creating a JRPG Judgment's Call.
I have a Patreon going, and it acts as a DevBlog of sorts.

https://www.patreon.com/JudgmentsCall

I'm on Tumblr too.

Gonna use it as a partial DevBlog as well.

Check that out here: http://judgmentscall.tumblr.com/
oahda
Level 10
*****



View Profile
« Reply #1 on: July 10, 2016, 02:40:14 AM »

Huh, thought Unity did all of this automatically so long as stuff was marked properly with [SerializeField] and [System.Serializable]?
Logged

DCoward
Level 0
**



View Profile WWW
« Reply #2 on: July 10, 2016, 08:06:06 AM »

Huh, thought Unity did all of this automatically so long as stuff was marked properly with [SerializeField] and [System.Serializable]?

I guess that's part of the problem then, I didn't find any good explanations of how Unity handles it, I guess, and the Unity Manual, good as it is for other stuff, is a bit harder for me to get because it's very wordy. That sort of stuff being really wordy is much harder for me to grasp, even when the code bits stare me in the face. :/

I mean, don't get me wrong, conceptually it makes plenty of sense... I think (I don't think I don't understand the purpose of serialization and why it is important), it's just what to put, why, what it does for the Serialization process, and where? That's sort of not exactly understood by me quite yet.

I think I need something a bit more concise that's Unity-based.
Logged

Creating a JRPG Judgment's Call.
I have a Patreon going, and it acts as a DevBlog of sorts.

https://www.patreon.com/JudgmentsCall

I'm on Tumblr too.

Gonna use it as a partial DevBlog as well.

Check that out here: http://judgmentscall.tumblr.com/
bdsowers
Level 3
***



View Profile WWW
« Reply #3 on: July 11, 2016, 07:18:52 AM »

Have you checked out the Unity write-up on serialization? http://blogs.unity3d.com/2014/06/24/serialization-in-unity/

It can be a bit dense, but it's really useful.

As Prinsessa alluded to, serialization can be as simple as marking your stuff Serializable... sometimes. It's important to note that Unity just can't serialize some things, though, but it may not spit out an error - it may just silently fail. For example, Dictionary objects can't be serialized. Polymorphism gets real tricky. This is when you have to roll up your sleeves and do some ugly work.
Logged

oahda
Level 10
*****



View Profile
« Reply #4 on: July 11, 2016, 07:47:09 AM »

Huh, didn't know that one about dictionaries. I've been lucky so far. I've serialised other containers but not dictionaries. Why don't those work? Weird.

Exactly how does your saving and loading work? What sort of things are you serialising and deserialising? Do you really need to serialise things as complex as you are now (if you are)? I guess it depends a lot on the game, but you aren't like, trying to serialise the whole scene state, are you? Or..?
Logged

bdsowers
Level 3
***



View Profile WWW
« Reply #5 on: July 11, 2016, 12:37:53 PM »

I've serialised other containers but not dictionaries. Why don't those work? Weird.

I may have to walk that back a little after doing some research. It seems that it's a problem specific to some serializers (eg: fails with Xml but might work for binary). And may be compounded by the platform - I had a lot of trouble serializing dictionaries on iPhone.

Anyway, that's a slight tangent.
Logged

DCoward
Level 0
**



View Profile WWW
« Reply #6 on: July 12, 2016, 11:44:10 PM »

Didn't mean to abandon thread, I've just been busy working.

So I got stuck on a few things, and I'd like some eyes on it if that's alright.

https://gist.github.com/alucard55/53122364a584e02f543fdde2b47838df

I'm trying to mess with the Load() function in GameControl so I can load the info back into the various stats and stuff, but I'm completely unsure of how to do that? And due to that, I haven't been able to test my Serialization at all (so if it's wrong, that's why -- haven't had a chance to test) so if you see an error I should correct, please let me know!
Logged

Creating a JRPG Judgment's Call.
I have a Patreon going, and it acts as a DevBlog of sorts.

https://www.patreon.com/JudgmentsCall

I'm on Tumblr too.

Gonna use it as a partial DevBlog as well.

Check that out here: http://judgmentscall.tumblr.com/
oahda
Level 10
*****



View Profile
« Reply #7 on: July 13, 2016, 12:01:57 AM »

This might be your problem: AFAIK public fields are automatically serialised by Unity, but private fields need to be explicitly marked before declaration by [SerializeField], and your relevant fields here seem to be private. Try that?

That is, you need to do this for all of them:

Code:
[SerializeField] private bool someField;
[SerializeField] private int someOtherField;
...
Logged

DCoward
Level 0
**



View Profile WWW
« Reply #8 on: July 13, 2016, 12:12:08 AM »

This might be your problem: AFAIK public fields are automatically serialised by Unity, but private fields need to be explicitly marked before declaration by [SerializeField], and your relevant fields here seem to be private. Try that?

That is, you need to do this for all of them:

Code:
[SerializeField] private bool someField;
[SerializeField] private int someOtherField;
...

I tried, but it still doesn't have anything as a suggestion for me in Load. I commented it out, but when trying to say
Code:
Health = data.health;
or
Code:
Health = data.profile.health;
or anything else, it doesn't recognize it as an option. So I was thinking that it might be that I'm not having GameControl talking to the rest of the scripts the right way? I'm not too sure.
Logged

Creating a JRPG Judgment's Call.
I have a Patreon going, and it acts as a DevBlog of sorts.

https://www.patreon.com/JudgmentsCall

I'm on Tumblr too.

Gonna use it as a partial DevBlog as well.

Check that out here: http://judgmentscall.tumblr.com/
DCoward
Level 0
**



View Profile WWW
« Reply #9 on: July 14, 2016, 04:30:59 PM »

I really hate bumping topics, but I'm at a complete loss here. I just don't get what I need to do to use Serialization appropriately for what I have.
Logged

Creating a JRPG Judgment's Call.
I have a Patreon going, and it acts as a DevBlog of sorts.

https://www.patreon.com/JudgmentsCall

I'm on Tumblr too.

Gonna use it as a partial DevBlog as well.

Check that out here: http://judgmentscall.tumblr.com/
oahda
Level 10
*****



View Profile
« Reply #10 on: July 14, 2016, 09:27:53 PM »

I don't get it either. Your structure is quite complex, tho. Do you really need to serialise this deep inheritance hierarchy? Can't you serialise simpler info packs of some sort instead? Little structs within the classes containing their stuff to be saved.
Logged

DCoward
Level 0
**



View Profile WWW
« Reply #11 on: July 14, 2016, 10:27:17 PM »

I don't get it either. Your structure is quite complex, tho. Do you really need to serialise this deep inheritance hierarchy? Can't you serialise simpler info packs of some sort instead? Little structs within the classes containing their stuff to be saved.

I mean, perhaps?

Ultimately, everything I have is intended to be inherited into a chain until it is a specific thing I can call into the character script that goes to a player.

For instance, I have Types and Classes/Jobs, there's a few different Types you can be, and then different subsections of that. Likewise, there are multiple jobs a character could have.

I thought I was doing it fairly logically, but I'm new, so I figure there's the likelihood that I didn't do it right.

I DO know, at least, that Serialization of some sort is what I theoretically need (unless I severely misunderstand it), and to that end, I have no idea how to properly make it work for my scripts.

Pretty much any help is more than appreciated. Be it an explanation of what my scripts should do differently, or be it how I messed up Serialization in my attempts.

Because until then, I can do very, very little before I'll need to be able to save stuff in order to test it.
Logged

Creating a JRPG Judgment's Call.
I have a Patreon going, and it acts as a DevBlog of sorts.

https://www.patreon.com/JudgmentsCall

I'm on Tumblr too.

Gonna use it as a partial DevBlog as well.

Check that out here: http://judgmentscall.tumblr.com/
DCoward
Level 0
**



View Profile WWW
« Reply #12 on: July 14, 2016, 11:59:11 PM »

Hmm, ok, since I suspect I might jsut be typing this out in a very obtuse way, I made pictures.

My gist doesn't TOTALLY apply now, because I changed the example in my pic to something easily understood -- Races and Jobs.

http://imgur.com/a/W9cuF

I think that explains my thought process very well on how I've tried to go about this.

**Slot is referring to the slot on the menu you'd see a character in. For instance, if you have 3 characters in your party, and you enter battle or open a menu, you'd see all 3, in some order. I'm referring to those as slots in the example pics, and I haven't coded anything for that yet (I included it because it demonstrates how I'm trying to pipeline the information.

Perhaps this is a very very bad way. I'd love to know if that's the case.

Or maybe that was clearer than anything I've said and now you lovely people can provide help Smiley
Logged

Creating a JRPG Judgment's Call.
I have a Patreon going, and it acts as a DevBlog of sorts.

https://www.patreon.com/JudgmentsCall

I'm on Tumblr too.

Gonna use it as a partial DevBlog as well.

Check that out here: http://judgmentscall.tumblr.com/
oahda
Level 10
*****



View Profile
« Reply #13 on: July 15, 2016, 12:32:59 AM »

No, your thinking is perfectly logical and fine, and that might be a good way to reduce duplicate code and stuff too. So this may indeed be the best layout for your purposes.

I don't actually know exactly how you're serialising or saving anything tho. I guess it's in the code you've posted, but I haven't had the time to look into it in detail because of work and stuff. But this is my last day before vacation so perhaps I can look more deeply into it soon.
Logged

DCoward
Level 0
**



View Profile WWW
« Reply #14 on: July 15, 2016, 12:42:36 AM »

No, your thinking is perfectly logical and fine, and that might be a good way to reduce duplicate code and stuff too. So this may indeed be the best layout for your purposes.

I don't actually know exactly how you're serialising or saving anything tho. I guess it's in the code you've posted, but I haven't had the time to look into it in detail because of work and stuff. But this is my last day before vacation so perhaps I can look more deeply into it soon.

Well, I certainly feel nice knowing that I didn't do something incredibly stupid.
And god I would LOVE if I could get some more hands-on help. I hate to sound so needy but I'm getting incredibly frustrated with myself and I don't think people have been understanding quite what I've wanted in other places I've asked and I'm too fixated on it to advance anything else :/
Logged

Creating a JRPG Judgment's Call.
I have a Patreon going, and it acts as a DevBlog of sorts.

https://www.patreon.com/JudgmentsCall

I'm on Tumblr too.

Gonna use it as a partial DevBlog as well.

Check that out here: http://judgmentscall.tumblr.com/
DCoward
Level 0
**



View Profile WWW
« Reply #15 on: July 17, 2016, 02:43:18 AM »

Well, good news and meh news.

On the good side, I was pointed to a very helpful udemy course that more helpfully covered Serialization.

I now understand some majorly important stuff.

On the less impressive side, I am still having some partial issues.

Firstly, I updated my gist: https://gist.github.com/alucard55/91e78c0488e84ccafa5aa183857d1686
Secondly, I am having a thing in Unity where, when I attack the script to a gameObject, it has some child profiles in it (like a nested menu that I don't want it to have).
I'm not quite sure how to combat that, and what's more it certainly doesn't seem like it's actually assigning the values I want it to have due to that (As in I want the GuardA script to have assigned stats from its "type" script I wrote, but I don't think it's working). I'm positive I just did something a bit excessive and somewhere in my code it made it call something extra, but I can't for the life of me figure out where that would've been.

So two questions now: What is going on with that?
Is this an effective use? Did I not quite implement this as intended?

Also, third question: This seems like an impeccable method for having duplicate enemies with minor variance in stats, but what exactly would I need to do to just save my standard player? Seems like it's still not quite hitting the mark for that, but I could be wrong and just not realize it.
Logged

Creating a JRPG Judgment's Call.
I have a Patreon going, and it acts as a DevBlog of sorts.

https://www.patreon.com/JudgmentsCall

I'm on Tumblr too.

Gonna use it as a partial DevBlog as well.

Check that out here: http://judgmentscall.tumblr.com/
DCoward
Level 0
**



View Profile WWW
« Reply #16 on: July 18, 2016, 12:03:13 AM »

Reluctantly bumping because I'm still unable to figure out what is going on.

Incredibly appreciative of anyone who can assist me.
Logged

Creating a JRPG Judgment's Call.
I have a Patreon going, and it acts as a DevBlog of sorts.

https://www.patreon.com/JudgmentsCall

I'm on Tumblr too.

Gonna use it as a partial DevBlog as well.

Check that out here: http://judgmentscall.tumblr.com/
oahda
Level 10
*****



View Profile
« Reply #17 on: July 18, 2016, 01:14:38 AM »

Your save function is confusing tho. It just creates a new guard and saves it. Shouldn't it be empty then? Or isn't it even reading the correct default values back when you call the load function?

1. What result are you expecting?
2. What result are you getting?
3. What's the error?
Logged

DCoward
Level 0
**



View Profile WWW
« Reply #18 on: July 18, 2016, 01:24:49 AM »

Your save function is confusing tho. It just creates a new guard and saves it. Shouldn't it be empty then? Or isn't it even reading the correct default values back when you call the load function?

1. What result are you expecting?
2. What result are you getting?
3. What's the error?

I suspect it is likely because I am so new to these concepts.

Thank you for the guidance thus far, great questions.

1. I am trying to have GuardA.cs declare values that GuardA should have in game, thus, I want it to have that information when being accessed, and any changes should be able to be saved through the format I have.

2.I am getting what you indicated. I am getting null values and a nested menu (I think I figured that out by making the Job and Type private in GuardA.cs, but I could be wrong -- that may not help me).

3. When trying to find a way to create information that is accessible, I have tried to return values, but I get this: "A return keyword must not be followed by any expression when method returns void", and past that, the only error I have gotten is a permission error stating that I'm not allowed to save in the path I have declared, but that's a separate (though important) issue that is less about persistence of data which I need to know first.
Logged

Creating a JRPG Judgment's Call.
I have a Patreon going, and it acts as a DevBlog of sorts.

https://www.patreon.com/JudgmentsCall

I'm on Tumblr too.

Gonna use it as a partial DevBlog as well.

Check that out here: http://judgmentscall.tumblr.com/
oahda
Level 10
*****



View Profile
« Reply #19 on: July 18, 2016, 02:57:59 AM »

Maybe you could try manually converting it to JSON before you write it to the file (and after reading it back): https://docs.unity3d.com/Manual/JSONSerialization.html

Possible you're using the binary thingy incorrectly too, not correctly converting to binary. Or you're not correctly converting back from binary when trying to reconstruct your object.
Logged

Pages: [1] 2
Print
Jump to:  

Theme orange-lt created by panic