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 jcv8000 · Mar 11, 2017 at 09:36 PM · serializationserializescope

Serializing a System.Action won't work with variables outside of its scope

So basically I'm serializing a bunch of System.Action's (for saving, like saving the quests the player has going). The Actions are being defined by lambda expressions.

It works completely fine if I serialize the Action in this:

 GameEventManager.SubscribeTo(GameEventManager.GotItemGameEvent, true, (object o) =>
 {
     if (o is blabla)
         Debug.Log("test");
 });

But if I include a variable from outside the lambda expression, I get an error about the parent function not being serializable.

 void Foo() {
     int x = 33;
     GameEventManager.SubscribeTo(GameEventManager.GotItemGameEvent, true, (object o) =>
     {
         if (o is blabla)
             Debug.Log("x is" + x);
     });
 }

Let's say this code is being ran in a function called "Foo()" and is inside of a class called "classX" This is the error:

 System.Runtime.Serialization.SerializationException: Type classX+<run>c__AnonStorey1 is 
 not marked as Serializable.

Is there any way around this? I would imagine needing to use variables inside these quests later in the game (for example whenever you pick up an item, every action that is added as a subscriber to that event would run)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Mar 12, 2017 at 06:20 AM

No, there's not really a way around that. The auto generated closure classes (those with "c__AnonStorey1" in their name) are not marked as serializable for a reason. The same closure instance might be used by multiple scopes as the main point of a closure object is to provide a shared environment. Serializing (and especially deserializing) would break this unless everything would be serialized at once. Since this can't be ensured from the compiler point of view, internal closure classes won't serialize.

Of course there is a way to somehow restore a closure instance by using reflection and manually iterating over the members of the closure class and store / restore them. However as i said above the result might not be what you would have expected. In your example case it would work as you only capture a local variable which isn't used elsewhere (for example by another closure). Though implementing this can be tricky. Referencing variables inside a closure will give you more problems. You can't deserialize the delegate if it should reference some other object that already exist somewhere. Deserialization can only deserialize a complete object tree.

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

67 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

Related Questions

how to serialize unity variables? (Sprite, Image ...) 1 Answer

Serialize an array of custom objects 3 Answers

Properly serialize array of derived classes. 4 Answers

usage of FieldInfo.SetValue with SerializeField 2 Answers

Save and Load all spawned objects in their position using array 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