Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Overcast · Jun 06, 2017 at 12:08 PM · programmingserialization

How to serialize delegates in save game, or use another system to replace them.

Hi there.

So I have a turn based game, where characters can get modifiers on them like "Hungry, -10 mood".

Currently, I have a list of bool delegates which can be added to modifiers which act as checks to see if these modifiers to stay active, that is the modifier is removed if one of these delegates returns true. so it might look like:

 HungryModifier.AddRemoveCondition(() => hunger < 0.2f);

This is then assessed every turn, to see if it returns true, at which point the modifier fails and is removed. The problem I have is that my current serialisation method doesn't serialize delegates, and from what I've read it can only be done natively via the binary serilaizer. So..

  • have I missed another way to serialize them?

  • Is binary serialization the way to go (ideally I wanted a human readable format)

  • Should I change my approach? I use delegates in a few places like this, because they are so flexible, but do I need to create a more rigid, defined solution for achieving the same thing?

Thanks for your help

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image toddisarockstar · Jun 08, 2017 at 03:47 AM 0
Share

I dont think your code example shows enough for anyone to comment on. are you talking about saving progress in a file format? or how to collect it in the first place? the bigger picture on what you are trying to achieve might help.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jdean300 · Jun 08, 2017 at 05:58 AM

Do these delegates tend to be pretty simple, like the on you posted? If so, I'd just create an enum for each delegate:

 public enum HungerConditions{
     HungerIsLow,
     HungerIsHigh,
     // etc...
 }

Then write an extension method to apply each of these conditions to a HungryModifier:

 public static class HungerExtensions{
     public static void Apply(this HungerConditions condition, HungryModifier mod){
         switch (condition){
             case HungerConditions.HungerIsLow:
                 // idk the source of the hunger variable, so you'll need to have access to that somehow
                 mod.AddRemoveCondition(() => hunger < .2f);
                 return;
             // ...
         }
     }
 }

You could serialize the string version of the enum name, which you can get with this. When you deserialize it and get the enum value back, you can call the extension method directly on that enum value:

 HungerConditions condition = /*get from deserialization*/;
 condition.Apply(hungerModifier);

That extension method can be used throughout your code base rather than having the raw delegates used - IMO using the enums is more descriptive. Most of the time you wouldn't care about the actual values being compared in the delegate when you are scanning through the code, but a nice descriptive name for the enum is helpful. Also, the same delegate in multiple places can not easily be edited when you use the raw delegates all over the place - with this extension method scheme they are in one spot.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

72 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Instantiating Prefabs through Editor Script 1 Answer

Bringing Down the Buttons 0 Answers

Add lines to a script from another script 0 Answers

Problems with the XML Serializer 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges