Hello I´m following this tutorial:
http://skaredcreations.com/api/combu/v3/page_faq.html
How could I add a variable of List<int> or "CustomClass"??
Thanks and best regards!!!
using System.Collections;using Combu;public class CombuDemoUser : User{string _myProperty1 = "";int _myProperty2 = 0;int _coins = 0;public string myProperty1{get { return _myProperty1; }set { _myProperty1 = value; customData["myProperty1"] = _myProperty1; }}public int myProperty2{get { return _myProperty2; }set { _myProperty2 = value; customData["myProperty2"] = _myProperty2; }}public int coins{get { return _coins; }set { _coins = value; customData["Coins"] = _coins; }}public CombuDemoUser(){myProperty1 = "";myProperty2 = 0;coins = 0;}public override void FromHashtable (Hashtable hash){// Set User class propertiesbase.FromHashtable (hash);// Set our own custom properties that we store in customDataif (customData.ContainsKey("myProperty1"))_myProperty1 = customData["myProperty1"].ToString();if (customData.ContainsKey("myProperty2"))_myProperty2 = int.Parse(customData["myProperty2"].ToString());if (customData.ContainsKey("Coins"))_coins = int.Parse(customData["Coins"].ToString());}}
You could store the List as JSON string, though since List involves also Garbage Collection in .NET I'd suggest to update this particular customData only in Update:
List<int> _list = new List<int>(); public List<int> list { get { return _list; } set { _list = value; } } public override void FromHashtable (Hashtable hash) { // .... _list = new List<int>(); if (customData.ContainsKey ("List")) { var tempList = customData ["List"].ToString ().arrayListFromJson (); if (tempList != null) { foreach (var v in tempList) { int v1; if (int.TryParse (v + "", out v1)) _list.Add (v1); } } } } public override void Update (System.Action<bool, string> callback) { var tempList = new ArrayList (_list); customData ["List"] = tempList.toJson (); base.Update (callback); }
The script MiniJSON adds toJson() method to ArrayList and Hashtable so you can easily get a representative JSON string from those classes, and adds arrayListFromJson() and hashtableFromJson() to the string class to convert back.
FRANCESCO CROCETTI @ SKARED CREATIONS
thanks very much for your reply.
So to a custom class wich inside has some variables and list I have to use:
arrayListFromJson() and hashtableFromJson() to the string class to convert back.
???
Or there any option to serialize the class and deseriealize to read it from server?
Thanks!
Look at the sample code in that post, I already used the functions there to deserialize (inside FromHashtable) and serialize (inside Update) a List<int> object.
FRANCESCO CROCETTI @ SKARED CREATIONS
I think, I´m missing something...
How can I fix this?? :
{
string _myProperty1 = "";
int _myProperty2 = 0;
int _gems = 0;
List<CombuUserInfoBuyPacks> _listBoughtGeo = new List<CombuUserInfoBuyPacks>();
public List<CombuUserInfoBuyPacks> listBoughtGeo
{
get { return _listBoughtGeo; }
set { _listBoughtGeo = value; }
}
public override void Update(System.Action<bool, string> callback)
{
var tempList = new ArrayList(_listBoughtGeo);
customData["ListGeos"] = tempList.toJson();
base.Update(callback);
}
public string myProperty1
{
get { return _myProperty1; }
set { _myProperty1 = value; customData["myProperty1"] = _myProperty1; }
}
public int myProperty2
{
get { return _myProperty2; }
set { _myProperty2 = value; customData["myProperty2"] = _myProperty2; }
}
public int gems
{
get { return _gems; }
set { _gems = value; customData["Gems"] = _gems; }
}
public CombuUserInfo()
{
myProperty1 = "";
myProperty2 = 0;
gems = 0;
}
public override void FromHashtable(Hashtable hash)
{
// Set User class properties
base.FromHashtable(hash);
// Set our own custom properties that we store in customData
if (customData.ContainsKey("myProperty1"))
_myProperty1 = customData["myProperty1"].ToString();
if (customData.ContainsKey("myProperty2"))
_myProperty2 = int.Parse(customData["myProperty2"].ToString());
if (customData.ContainsKey("Gems"))
_gems = int.Parse(customData["Gems"].ToString());
_listBoughtGeo = new List<CombuUserInfoBuyPacks>();
if (customData.ContainsKey("List"))
{
var tempList = customData["List"].ToString().arrayListFromJson();
if (tempList != null)
{
foreach (var v in tempList)
{
int v1;
// if (int.TryParse(v + "", out v1))
// _listBoughtGeo.Add(v1);
}
}
}
}
}
And why I get this error when I try to casting localUser??
CombuUserInfo user = (CombuUserInfo)CombuManager.localUser;
InvalidCastException: Cannot cast from source type to destination type. IcePopGamesVIP.CustomDataPost () (at Assets/IcePopGamesVIP.cs:101)
What is CombuUserInfoBuyPacks? It seems a class, that's why it cannot work like you're supposing. You should create two methods in the class CombuUserInfoBuyPacks: FromHashtable (to cast from JSON) and toJson (to cast to JSON). You would use a Hashtable to store in the database, just like the core classes work.
Here is a sample code for CombuUserInfoBuyPacks:
public class CombuUserInfoBuyPacks { public string prop1 { get; set; } public int prop2 { get; set; } public CombuUserInfoBuyPacks (Hashtable data) { if (data != null) { if (data.ContainsKey ("prop1")) prop1 = data ["prop1"].ToString (); if (data.ContainsKey ("prop2")) prop2 = int.Parse (data ["prop2"].ToString ()); } } public Hashtable ToHashtable () { var data = new Hashtable (); data.Add ("prop1", prop1); data.Add ("prop2", prop2); return data; } }
And here is for CombuUserInfo:
public class CombuUserInfo : User { public List<CombuUserInfoBuyPacks> listBoughtGeo { get; set; } public string myProperty1 { get; set; } public int myProperty2 { get; set; } public int gems { get; set; } public CombuUserInfo() { myProperty1 = ""; myProperty2 = 0; gems = 0; listBoughtGeo = new List<CombuUserInfoBuyPacks> (); } public override void FromHashtable(Hashtable hash) { // Set User class properties base.FromHashtable(hash); // Set our own custom properties that we store in customData if (customData.ContainsKey("myProperty1")) myProperty1 = customData["myProperty1"].ToString(); if (customData.ContainsKey("myProperty2")) myProperty2 = int.Parse(customData["myProperty2"].ToString()); if (customData.ContainsKey("Gems")) gems = int.Parse(customData["Gems"].ToString()); listBoughtGeo = new List<CombuUserInfoBuyPacks> (); if (customData.ContainsKey("List")) { var tempList = customData["List"].ToString().arrayListFromJson(); if (tempList != null) { foreach (Hashtable data in tempList) { listBoughtGeo.Add (new CombuUserInfoBuyPacks (data)); } } } } public override void Update(System.Action<bool, string> callback) { var tempList = new ArrayList(); foreach (var pack in listBoughtGeo) { tempList.Add (pack.ToHashtable ()); } customData["List"] = tempList.toJson(); base.Update(callback); } }
Anyway you need to use Hashtables and ArrayLists to easily store class objects as values.
FRANCESCO CROCETTI @ SKARED CREATIONS
Thanks! Now it has more sense!!
About CombuUserInfo user = (CombuUserInfo)CombuManager.localUser; I found the problem, localuser was "CombuDemo" it has to be "User"
Thanks for your help! 🙂
PabloAM said
And why I get this error when I try to casting localUser??
InvalidCastException: Cannot cast from source type to destination type. IcePopGamesVIP.CustomDataPost () (at Assets/IcePopGamesVIP.cs:101)
Are you using CombuManager.platform.Authenticate<CombuUserInfo>() ?
FRANCESCO CROCETTI @ SKARED CREATIONS