Forum rules - please read before posting.

Saving in iOS generates an InvalidOperationException

edited August 2023 in Technical Q&A

I have been developing my game to run on desktop and WebGL and I have created an 'AutoSaver' script to auto save the state of the game on certain events: when an inventory item is added or deleted, when a variable is changed and when a scene is about to change (OnBeforeChangeScene) and when it starts (OnStartScene).

Everything seems to be working fine on the desktop and WebGL targets (where a binary save file is saved), but when I switch the platform to iOS (where XML is used) and try to run the game either via the Editor or Xcode then I get the following exception upon trying to save the scene (after the OnStartScene event fires):

InvalidOperationException: AC.SceneItemSpawnData cannot be serialized because it does not have a parameterless constructor.
Rethrow as InvalidOperationException: Cannot serialize member 'AC.SingleLevelData.allSceneItemSpawnData' of type 'System.Collections.Generic.List`1[[AC.SceneItemSpawnData, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]', see inner exception for more details.
System.Xml.Serialization.StructModel.CheckSupportedMember (System.Xml.Serialization.TypeDesc typeDesc, System.Reflection.MemberInfo member, System.Type type) (at <7fcddeed965246b69b2218bb4b12c6d7>:0)
System.Xml.Serialization.StructModel.CheckSupportedMember (System.Xml.Serialization.TypeDesc typeDesc, System.Reflection.MemberInfo member, System.Type type) (at <7fcddeed965246b69b2218bb4b12c6d7>:0)
System.Xml.Serialization.StructModel.GetFieldModel (System.Reflection.FieldInfo fieldInfo) (at <7fcddeed965246b69b2218bb4b12c6d7>:0)
System.Xml.Serialization.StructModel.GetFieldModel (System.Reflection.MemberInfo memberInfo) (at <7fcddeed965246b69b2218bb4b12c6d7>:0)
...

The full call-stack can be found here.

When I add this missing parametereless constructor on the AC.SceneItemSpawnData class, the error goes away:

diff --git a/Assets/AdventureCreator/Scripts/Save system/RememberSceneItem.cs b/Assets/AdventureCreator/Scripts/Save system/RememberSceneItem.cs
index 1cbd0ac..1d107be 100644
--- a/Assets/AdventureCreator/Scripts/Save system/RememberSceneItem.cs
+++ b/Assets/AdventureCreator/Scripts/Save system/RememberSceneItem.cs
@@ -247,6 +247,11 @@ namespace AC
                public int itemID;
                public int objectID;

+               /**
+                * The default Constructor.
+                */
+               public SceneItemSpawnData () { }
+
                public SceneItemSpawnData (SceneItem sceneItem, int _objectID)
                {
                        if (sceneItem == null || !InvInstance.IsValid (sceneItem.LinkedInvInstance))

But I don't want to change the source code of the asset, so maybe there's another way of fixing the issue here.

I am running the latest version of Adventure Creator (1.78.2) and Unity 2022.3.5f1.

Comments

  • Thanks for the details. It's not an issue specific to iOS, but XML serialization that AC defaults to on this platform.

    A fix for this will be included in the upcoming release - along similar lines to yours.

  • Thanks Chris! I am gonna keep my small fix until the next release then!

  • Issue confirmed as fixed in version 1.78.3! :smiley:

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.