Serialization, OnExtensionResponse Not firing

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

Sanity1993
Posts: 26
Joined: 30 Jan 2018, 21:14

Re: Serialization, OnExtensionResponse Not firing

Postby Sanity1993 » 08 Feb 2018, 11:31

Okay that makes me sense i was expecting to see them listed as they are with the manual way below it.

But there still is a problem as

Code: Select all

        [SerializeField]public ArrayList _inventory;
        [SerializeField]public Hashtable _properties;
        [SerializeField]public Hashtable _spells;


Is returning null.
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Serialization, OnExtensionResponse Not firing

Postby Lapo » 08 Feb 2018, 14:29

I am not sure where those values get tested in your code, but seeing that they are marked as [SerializeField] I suspect they are also part of a class that extends MonoBehavior?

I'd recommend re-checking those data classes and removing the inheritance where necessary.
Lapo
--
gotoAndPlay()
...addicted to flash games
Sanity1993
Posts: 26
Joined: 30 Jan 2018, 21:14

Re: Serialization, OnExtensionResponse Not firing

Postby Sanity1993 » 08 Feb 2018, 16:47

At the bottom of RpgCharacter this is:

Code: Select all

 public string toString()
        {
            Debug.Log("RPG Character: " + this._name);
            Debug.Log("Type: " + this._type);
          if (_inventory != null)
           {
                Debug.Log("Inventory is not null");
            Debug.Log(this._inventory);
            foreach (Item Item in this._inventory)
                {
                    Debug.Log("Item: " + Item);
                }

            }
            if (_spells != null)
            {
                Debug.Log("_spells is not null");
            Debug.Log(_spells);
            foreach (Spell spell in _spells)
                {
                    Debug.Log("Spells: " + spell);
                }

            }
            if (_properties != null)
            {
                Debug.Log("properties is not null");
            Debug.Log(_properties);
            foreach (CharacterProperty prop in _properties)
                {
                    Debug.Log("Properties: " + prop);
                }
            return "";
        }
           
        }
    }


I attempted this code and everything is null.
Removing the test of null, i get:
NullReferenceException: Object reference not set to an instance of an object
altarianonline.model.RpgCharacter.toString () (at Assets/model/RpgCharacter.cs:124)
GameManager.dumpModel (ISFSObject data) (at Assets/Scripts/GameManager.cs:512)
GameManager.OnExtensionResponse (Sfs2X.Core.BaseEvent e) (at Assets/Scripts/GameManager.cs:492)
Sfs2X.Core.EventDispatcher.DispatchEvent (Sfs2X.Core.BaseEvent evt)
Rethrow as Exception: Error dispatching event extensionResponse: Object reference not set to an instance of an object at altarianonline.model.RpgCharacter.toString () (at Assets/model/RpgCharacter.cs:124)
GameManager.dumpModel (ISFSObject data) (at Assets/Scripts/GameManager.cs:512)
GameManager.OnExtensionResponse (Sfs2X.Core.BaseEvent e) (at Assets/Scripts/GameManager.cs:492)
Sfs2X.Core.EventDispatcher.DispatchEvent (Sfs2X.Core.BaseEvent evt)
Sfs2X.Core.EventDispatcher.DispatchEvent (Sfs2X.Core.BaseEvent evt)
Sfs2X.SmartFox.ProcessEvents ()
GameManager.FixedUpdate () (at Assets/Scripts/GameManager.cs:195)
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Serialization, OnExtensionResponse Not firing

Postby Lapo » 09 Feb 2018, 09:53

I must apologize for not having thought of this before, haven't used class serialization in a long while.

In our documentation we require to setup the current assembly before using serialization, so that the reflection code can find the classes needed for rebuilding objects.
It is reminded in our docs here:
http://docs2x.smartfoxserver.com/api-do ... faf3df.htm

Essentially you need to use this line early in your code:

Code: Select all

DefaultSFSDataSerializer.RunningAssembly = Assembly.GetExecutingAssembly();

for example before the connection.

Also, are you sure about the getter/setter syntax you've been using?

Code: Select all

public int hitpoints
{
    get
    {
        return _hitPoints;
    }

    set
    {
        this._hitPoints = hitpoints;
    }
}

Unless something was changed recently in the language this is not the correct way of creating setters. You should refer to the passed value with the value keyword otherwise you're pointing to the getter value, which in turn will always be null.

In other words:

Code: Select all

public int hitpoints
{
    get
    {
        return _hitPoints;
    }

    set
    {
        this._hitPoints = value;
    }
}


Or more succinctly:

Code: Select all

public int hitpoints { get; set; }


I've fixed your code and re-tested with the assembly thing and it works without problems.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
Sanity1993
Posts: 26
Joined: 30 Jan 2018, 21:14

Re: Serialization, OnExtensionResponse Not firing

Postby Sanity1993 » 11 Mar 2018, 22:28

Resolved the serialization changing the interfaces to:

Code: Select all

using Sfs2X.Protocol.Serialization;

public interface Item : SerializableSFSType
{

    string getId();
    void useItem();
    void sell();
    void buy();
}

So now a class is:

Code: Select all

using UnityEngine;
using Sfs2X.Protocol.Serialization;
namespace altarianonline.model
{

    public class WeaponItem : Item, SerializableSFSType
    {

        public WeaponItem()
        {
            //Debug.Log("WeaponItem Dynamically Called.");
        }
        public WeaponItem(string id, int price, int patk, int matk, int satk, int fire, int earth, int water, int wind, int thunder, string quality, int location)
        {
            this.id = id;
            this.price = price;
            this.active = true;
            this.Patk = patk;
            this.Matk = matk;
            this.Satk = satk;
            this.wind = wind;
            this.thunder = thunder;
            this.earth = earth;
            this.fire = fire;
            this.quality = quality;
            this.location = location;

        }
        public string id;
        public bool active;
        public int price;
        public int Patk;
        public int Matk;
        public int Satk;
        public int wind;
        public int thunder;
        public int earth;
        public int water;
        public int fire;
        public string quality;
        public int location;

        public string getId()
        {
            return id;
        }
        public void useItem()
        {
            Debug.Log("Used Item: " + id);
        }

        public void sell()
        {
            Debug.Log("Sold Item: " + id);
        }

        public void buy()
        {
            Debug.Log("Bought Item: " + id);
        }
    }
}


So from within the RpcCharacter.Class im trying to grab the Map<string, Item> = new Hashmap(); (sent by Java).
//Create Hashmaps to hold charater information
Map<String, Item> inventory = new HashMap<>();
Map<String, CharacterProperty> props1 = new HashMap<>();
Map<String, Skill> skills = new HashMap<>();
Map<String, Pet> pets = new HashMap<>();
Map<String, Buffs> buffs = new HashMap<>();
RpgCharacter knightOfTheRoundTable1 = new RpgCharacter();

From website it converted into Hashmap.http://docs2x.smartfoxserver.com/Advanc ... ialization
So my RpgCharacter(c#) if it is an Hashmap i cannot do the same in java of :

Code: Select all

if (skills != null)
         for (Map.Entry<String, Skill> entry : skills.entrySet())
            sb.append(TAB).append(TAB).append(entry.getValue().getId()).append(" -- Modifies: ").append(entry.getValue().getModifies()).append(", Level: ").append(entry.getValue().getLevel()).append(NEW_LINE);


If the data type is Hashmap none of the .getLevel() etc. only the

Code: Select all

if (inventory != null)
            {
                foreach (var Item in inventory.Values)
                {
                    dump(Item);
                }
            }

Which i cannot seem to do much with.
And trying to expand on Item, you can only do the following: Equals, GetHashCode, GetType, ToString.
Even attempting to change the return type as Item, generates an error (in compiler).
if (inventory != null)
{
foreach (Item Item in inventory.Values)
{
dump(Item.getId.ToString());
}
}

Error:
Severity Code Description Project File Line Suppression State
Error CS0119 'Item.getId()' is a method, which is not valid in the given context Assembly-CSharp D:\Documents\Smart Fox Server\Training\07 MMORoomDemo\source\client\MMORoomDemo\MMORoomDemo\Assets\model\RpgCharacter.cs 102 Active
Sanity1993
Posts: 26
Joined: 30 Jan 2018, 21:14

Re: Serialization, OnExtensionResponse Not firing

Postby Sanity1993 » 11 Mar 2018, 22:32

scratch that fixed it!!1

Code: Select all

foreach (Item item in inventory.Values)
                {

                    Debug.Log(item.getId().ToString());
                }

Used the above instead of:

Code: Select all

foreach (Item Item in inventory.Values)
                {

                    Debug.Log(Item.getId().ToString());
                }

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Google [Bot] and 43 guests