ArgumentException error

Discussion and help for Easy Save 3
hthought
Posts: 15
Joined: Sat Jun 16, 2018 10:41 pm

ArgumentException error

Post by hthought »

Hi there ! Thanks for all the help so far :)

I am having a new issue which I am not sure how to go about resolving. I am getting this error :

Code: Select all

ArgumentException: Only types of UnityEngine.Component can be written with this method, but argument given is type of GameData
I have a GameData class like this (I've added it as a custom type on ES3):

Code: Select all

public class GameData: MonoBehaviour {
	public string pd = "GameData.es3"; 
	public Player player;
}
I need to have this be a monobehaviour(for ui data bind reasons) when saved with ES3 because when I load it to the scene, it has to be the GameData component of an already existing gameObject in the scene ( basically substituting the GameData component ).

The scene already contains this gameObject which already has a GameData component attached to it and it looks like the fact that this is the case may cause the error because when I remove the GameData component from that gameObject I am not getting the above error. But I don't have the slightest clue about why that's the case :shock: The thing is, the GameData component has to be there all the time because it holds references to the ui data bind variables, so I really need to have it present at all times and just substitute all its values on load.

My save method(which gives the original exception error) is this :

Code: Select all

		if(!ES3.FileExists("GameData.es3")) {
			GameData gameData = new GameData();
			/* First Time Initialization of Game Data */

			ES3.Save<GameData>("GameData", gameData, "GameData.es3");
			Debug.Log("First time initialization of the GameData.es3 file");
		}
Any ideas on what could be causing this ? Thanks !
User avatar
Joel
Moodkie Staff
Posts: 4852
Joined: Wed Nov 07, 2012 10:32 pm

Re: ArgumentException error

Post by Joel »

Hi there,

Firstly, please could you check that you're using the latest version of Easy Save?

Secondly, incase there's previous data which does not match with the current type, please could you try deleting your save data and see if this fixes the issue? You can open the default save folder by going to Window > Easy Save 3 > Tools > Open Persistent Data Path.

If updating doesn't fix the problem, please could you PM me a basic project which replicates it and instructions?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
hthought
Posts: 15
Joined: Sat Jun 16, 2018 10:41 pm

Re: ArgumentException error

Post by hthought »

Joel wrote:Hi there,

Firstly, please could you check that you're using the latest version of Easy Save?

Secondly, incase there's previous data which does not match with the current type, please could you try deleting your save data and see if this fixes the issue? You can open the default save folder by going to Window > Easy Save 3 > Tools > Open Persistent Data Path.

If updating doesn't fix the problem, please could you PM me a basic project which replicates it and instructions?

All the best,
Joel
Yeap, I have actually checked whether I was using the latest version and I was indeed. There's actually no save data at that file, it's a completely new one. The code is basically what I pasted on the post, nothing else on that saved file. I'll probably try a few things and will create a small project for you if i can't get it to work. Thanks !
hthought
Posts: 15
Joined: Sat Jun 16, 2018 10:41 pm

Re: ArgumentException error

Post by hthought »

By the way I am seeing a couple of posts here :

https://moodkie.com/forum/viewtopic.php ... thod#p4338
https://moodkie.com/forum/viewtopic.php ... thod#p4524

It seems that this was acknowledged as a bug at some point ? Maybe it hasn't still been pushed to Easy Save ?

Maybe you can send me the same fix ?

Thanks !

(Btw, I have already PMed you my invoice number)
User avatar
Joel
Moodkie Staff
Posts: 4852
Joined: Wed Nov 07, 2012 10:32 pm

Re: ArgumentException error

Post by Joel »

Hi there,

This issue was fixed quite a long time ago, so it would appear that your issue is different, despite having the same error message.

If you could PM me a basic project and instructions to replicate it I'll be happy to look into it further.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
hthought
Posts: 15
Joined: Sat Jun 16, 2018 10:41 pm

Re: ArgumentException error

Post by hthought »

Joel wrote:Hi there,

This issue was fixed quite a long time ago, so it would appear that your issue is different, despite having the same error message.

If you could PM me a basic project and instructions to replicate it I'll be happy to look into it further.

All the best,
Joel
Thanks Joel ! I have an idea on what could be causing it, but I will need to make some changes in order to test it. I am looking at this code where the error is produced:

Code: Select all

		protected override void WriteObject(object obj, ES3Writer writer)
		{
			Debug.Log(obj);
			var instance = obj as Component;
			if(instance == null)
				throw new ArgumentException("Only types of UnityEngine.Component can be written with this method, but argument given is type of "+obj.GetType());
In my save method, I basically create the object on the fly like:

Code: Select all

      if(!ES3.FileExists("GameData.es3")) {
         GameData gameData = new GameData();
         /* First Time Initialization of Game Data */
It looks like var instance retrieves that object as a Component, which I think should mean that it's actually a component of a gameobject instance, therefore coming back null in my example cause it's not attached to anything. I think it should probably work if I save it from a GameObject that currently resides on the hierarchy, or maybe if i create one dynamically. Will need to try that out though. I want to be able to contruct the monobehaviour on the fly, without having to actually create a gameObject on the scene, but I am actually not sure if that's even possible at the moment.
hthought
Posts: 15
Joined: Sat Jun 16, 2018 10:41 pm

Re: ArgumentException error

Post by hthought »

So yeah, I was actually able to make it work like this. I created a gameobject with preinitialized GameData(null values) and then via script :

Code: Select all

		if(!ES3.FileExists("GameData.es3")) {
			
			GameData gameData = gameDataObjOnScene.GetComponent<GameData>();
			gameData.player = new Player();
			gameData.player.level = 1;
			
			/* First Time Initialization of Game Data with ES3 */
			savePlayerData("GameData.es3", gameData);
		}
Seems like now that the monobehavior is actually stored in an actual gameobject, the component is not null and the whole thing works :) I would still be interested in knowing if it's possible to do without precreating or attaching to an existing gameobject.

Thanks for the help !
User avatar
Joel
Moodkie Staff
Posts: 4852
Joined: Wed Nov 07, 2012 10:32 pm

Re: ArgumentException error

Post by Joel »

Hi there,

I understand what is happening now. In Unity a Component cannot exist without a GameObject. So you cannot use new to create your Component, and should use AddComponent to add a new instance to a GameObject instead. This is a limitation of Unity rather than of Easy Save.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
hthought
Posts: 15
Joined: Sat Jun 16, 2018 10:41 pm

Re: ArgumentException error

Post by hthought »

Joel wrote:Hi there,

I understand what is happening now. In Unity a Component cannot exist without a GameObject. So you cannot use new to create your Component, and should use AddComponent to add a new instance to a GameObject instead. This is a limitation of Unity rather than of Easy Save.

All the best,
Joel
Yeap, that makes sense, I really should have thought of that, my bad. I am bumping into another issue now, which has to do with ezBind(a UI-data bind asset at the store) and Easy Save. I am setting a gameobject with the GameData Component in the hierarchy, which pretty much always exists on the scene and contains data for the game. Looks like this:

https://imgur.com/a/KNWroIk

I am using these methods to save and load data into that object:

Code: Select all

	
public void loadPlayerDataIntoGameObject(string saveFilename, GameObject gameDataObj)
	{
		GameData gameData = gameDataObj.GetComponent<GameData>();
		
		if (ES3.FileExists(saveFilename)) {
			ES3.LoadInto<GameData>("GameData", saveFilename, gameData);
		} else {
			print("cannot locate save file: " + saveFilename);
		}
	}

	public void savePlayerData(string saveFilename, GameData data) {
		ES3.Save<GameData>("GameData", data, saveFilename);
	}
When i replace the LoadInto method with the code below, the binds work fine :

Code: Select all

			gameData.player.level = 2000;
			gameData.player.xp = 10000;
This makes me think that something changes with the original gameobject GameData when loaded with Easy Save, maybe an instance id or something, which causes ezBind to mess up its bindings. Is there something like that happening on save/load that I could potentially bypass somehow ? I will next try to save/load the entire gameobject but I am a bit baffled as to why this is happening :|

Thanks a lot for all the help !
User avatar
Joel
Moodkie Staff
Posts: 4852
Joined: Wed Nov 07, 2012 10:32 pm

Re: ArgumentException error

Post by Joel »

Hi there,

I'm afraid I have no experience with ezBind, or how it works underneath. However, Easy Save does not touch Unity instance IDs.

All the LoadInto method will do is load the fields/properties you selected in the Types window for that type (or the fields defined here if you have not set them in the Types window: https://docs.moodkie.com/easy-save-3/es ... ted-types/).

You may want to ask the creator of the bind asset, as this seems to be an issue at their end rather than ours.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply