InvalidCastException in user and buddy variables.

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

User avatar
faisal007
Posts: 37
Joined: 27 Sep 2016, 09:01

InvalidCastException in user and buddy variables.

Postby faisal007 » 27 Jan 2017, 17:18

Hi,

I am using Smartfox in Unity. On Buddy and User Variables I get an InvalidCastException: Cannot cast from source type to destination type.

buddy.GetVariable("$customvar").GetDoubleValue()

now when i check the type of the variable It shows as double

[BuddyVar: $customvar, type: DOUBLE, value: 12005234]

to further complicate things it seems to be working on some buddies and not on some even though a log of the type shows that it is double and data is there. when this happens error occurs. I can cast it to an int but then it gives me an error on the other buddies. I am really confused and any help with this would be greatly appreciated.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: InvalidCastException in user and buddy variables.

Postby Lapo » 28 Jan 2017, 08:39

Hi,
can you please give me a bit more context?
SFS2X version?
API version?
What platform are you building it to?
Are you testing in the Editor?

Finally can you show us an example of code that reproduces the issue?
It would also help if you could post the entire stack trace of the error, taken from the Editor's console.

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
faisal007
Posts: 37
Joined: 27 Sep 2016, 09:01

Re: InvalidCastException in user and buddy variables.

Postby faisal007 » 30 Jan 2017, 15:49

The sfs2x and the API version are the latest downloaded from the website about a week ago.
We are building the game to Unity WebGL.
And yes the testing was done in the editor when this error occoured.

I have shown the attached code in the first post. Quite literally just doing buddy.GetVariable("$customvar").GetDoubleValue(); is causing the error.


Detailed Code Below:


Code: Select all

List<Buddy> buddiesToShow = sfs.BuddyManager.BuddyList
            .OrderBy(p => p.Name)
            .Skip(4 * currentPage)
            .Take(4).ToList();

        string picURl;
        string Name;
        long biggestPotAmount;
        long currentChipAmount;
        int userID;

        foreach(Buddy buddy in buddiesToShow)
        {
         
            if (buddy.ContainsVariable("$userImageUrl") &&  buddy.ContainsVariable("$name") && 
                buddy.ContainsVariable("$highestPotWonAmount") &&  buddy.ContainsVariable("$totalChipCount"))
            {
                name = buddy.GetVariable("$name").GetStringValue();
                Debug.Log(name);
                picURl = buddy.GetVariable("$userImageUrl").GetStringValue();
                Debug.Log(picURl);
                biggestPotAmount = buddy.GetVariable("$highestPotWonAmount").GetDoubleValue();
                Debug.Log(biggestPotAmount);
                Debug.Log(buddy.GetVariable("$totalChipCount"));
                currentChipAmount = (long)buddy.GetVariable("$totalChipCount").GetDoubleValue();

                 //do some stuff with vars.

            }
        }


STACK TRACK:

Code: Select all

ProfileHandler.ShowPanelBuddies () (at Assets/Scripts/UIScripts/ProfileHandler.cs:72)
ProfileHandler.OnChange (UnityEngine.UI.Toggle newactive) (at Assets/Scripts/UIScripts/ProfileHandler.cs:47)
BetterToggleGroup.DoOnChange (UnityEngine.UI.Toggle newactive) (at Assets/Scripts/UIScripts/BetterToggleGroup.cs:50)
BetterToggleGroup.<Start>m__0 (Boolean isSelected) (at Assets/Scripts/UIScripts/BetterToggleGroup.cs:29)
UnityEngine.Events.InvokableCall`1[System.Boolean].Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:188)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:634)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:769)
UnityEngine.Events.UnityEvent`1[System.Boolean].Invoke (Boolean arg0) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_1.cs:53)
UnityEngine.UI.Toggle.Set (Boolean value, Boolean sendCallback) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Toggle.cs:188)
UnityEngine.UI.Toggle.Set (Boolean value) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Toggle.cs:163)
UnityEngine.UI.Toggle.set_isOn (Boolean value) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Toggle.cs:157)
UnityEngine.UI.Toggle.InternalToggle () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Toggle.cs:220)
UnityEngine.UI.Toggle.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Toggle.cs:231)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: InvalidCastException in user and buddy variables.

Postby Lapo » 30 Jan 2017, 15:56

In the stack trace there's no specific error, nor any references to any SmartFox API classes... so I am not sure what I am looking at.
Can you clarify?
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
faisal007
Posts: 37
Joined: 27 Sep 2016, 09:01

Re: InvalidCastException in user and buddy variables.

Postby faisal007 » 01 Feb 2017, 13:48

Here you go:

Code: Select all

InvalidCastException: Cannot cast from source type to destination type.
Sfs2X.Entities.Variables.BaseVariable.GetDoubleValue ()
ProfileBuddyListController.ShowCurrentPageBuddies (Int32 currentPage) (at Assets/Scripts/UIScripts/ProfileBuddyListController.cs:97)
ProfileBuddyListController.OpenBuddyList () (at Assets/Scripts/UIScripts/ProfileBuddyListController.cs:58)
ProfileHandler.ShowPanelBuddies () (at


Nothing else in unity editor console.
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: InvalidCastException in user and buddy variables.

Postby Bax » 01 Feb 2017, 16:23

Can you please show how you set the variable to a double value?
I'd like to test this under the same conditions.
Paolo Bax
The SmartFoxServer Team
User avatar
faisal007
Posts: 37
Joined: 27 Sep 2016, 09:01

Re: InvalidCastException in user and buddy variables.

Postby faisal007 » 02 Feb 2017, 05:47

Here is the full server side code.

Code: Select all

ArrayList<UserVariable> userVars = new ArrayList<UserVariable>();
         List<BuddyVariable> buddyVars = new ArrayList<BuddyVariable>();
         
         ISFSArray res = dbManager.executeQuery(sql,  new Object[]{});
         trace("Result size " + res.size());
         
         if(res.size() == 0)
         {
            getApi().disconnectUser(user, ClientDisconnectionReason.KICK);
            
            return;
         }
         
         for (int i = 0; i < res.size(); i++)
         {
            trace("Check handsWon", res.getSFSObject(i).getInt("win_count"));
            trace("Check sending_chip_limit", res.getSFSObject(i).getInt("sending_chip_limit"));
            userVars.add(new SFSUserVariable("sendingLimit", res.getSFSObject(i).getInt("sending_chip_limit")));
            userVars.add(new SFSUserVariable("handsWon", res.getSFSObject(i).getInt("win_count")));
            userVars.add(new SFSUserVariable("userID", res.getSFSObject(i).getLong("user_id").doubleValue(), false));
            userVars.add(new SFSUserVariable("totalChipCount", res.getSFSObject(i).getLong("chips").doubleValue(), false));
            userVars.add(new SFSUserVariable("highestPotWonAmount", res.getSFSObject(i).getLong("biggest_pot").doubleValue(), false));
            userVars.add(new SFSUserVariable("level", res.getSFSObject(i).getInt("level"), false));
            userVars.add(new SFSUserVariable("title", res.getSFSObject(i).getUtfString("title"), false));
            userVars.add(new SFSUserVariable("userImageUrl", "https://graph.facebook.com/" + res.getSFSObject(i).getUtfString("platform_id") + "/picture?type=large"));
            userVars.add(new SFSUserVariable("name", res.getSFSObject(i).getUtfString("name")));
            userVars.add(new SFSUserVariable("isSeated", "false"));
            
            getApi().setUserVariables(user, userVars, true, false);
            
            buddyVars.add(new SFSBuddyVariable("$currentRoom", "Lobby"));
            buddyVars.add(new SFSBuddyVariable("$name", res.getSFSObject(i).getUtfString("name")));
            buddyVars.add(new SFSBuddyVariable("$userID", res.getSFSObject(i).getLong("user_id").doubleValue()));
            buddyVars.add(new SFSBuddyVariable("$totalChipCount", res.getSFSObject(i).getLong("chips").doubleValue()));
            buddyVars.add(new SFSBuddyVariable("$highestPotWonAmount", res.getSFSObject(i).getLong("biggest_pot").doubleValue()));
            buddyVars.add(new SFSBuddyVariable("$userImageUrl", "https://graph.facebook.com/" + res.getSFSObject(i).getUtfString("platform_id") + "/picture?type=large"));
            buddyVars.add(new SFSBuddyVariable("$receivingLimit", res.getSFSObject(i).getInt("receiving_chip_limit")));
            
            /*
             * BuddyListManager bManager = getParentZone().getBuddyListManager();
   
               if (bManager.getBuddyList("SomeUserName") != null)
               {
                   // Inited
               }
               else
               {
                  // Not inited
               }
            
             */
            
            buddyApi.initBuddyList(user, false);
            
            buddyApi.setBuddyVariables(user, buddyVars, true, false);



Even on client side (Unity c#) we are getting type double when checking with getType function and also I can see double type of these variables (Only those are set to double) when monitoring a particular player in admin panel.

Thanks.
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: InvalidCastException in user and buddy variables.

Postby Bax » 02 Feb 2017, 11:14

We can't reproduce the issue in two different tests based on our BuddyMessenger example for Unity.
The tests involved two clients: the Unity Editor itself (in WebGL mode) and a browser (Firefox) running the WebGL build.
SFS C# API version is 1.7.2, server version is 2.12.1.

TEST 1
Make the client set a variable of type double together with the other vars set in the example (nickname, age, etc).
All his buddies receive the update event and can access the value without issues.

TEST 2
In a server side Extension, as soon as a user inits his Buddy List, a listener for the SFSEventType.BUDDY_LIST_INIT event creates a variable of type double. All his buddies receive the update event and can access the value without issues.

This is the server side listener for test 2:

Code: Select all

public class BuddyListInitEventHandler extends BaseServerEventHandler
{
   @Override
   public void handleServerEvent(ISFSEvent event) throws SFSException
   {
      User user = (User) event.getParameter(SFSEventParam.USER);
      
      Random r = new Random();
      double val = r.nextDouble();
      
      System.out.println("\n\nSetting $test buddy var for user " + user.getName() + " to value " + val + "\n\n");
      
      List<BuddyVariable> buddyVars = new ArrayList<BuddyVariable>();
      buddyVars.add(new SFSBuddyVariable("$test", val));
      
      ISFSBuddyApi buddyApi = SmartFoxServer.getInstance().getAPIManager().getBuddyApi();
           buddyApi.setBuddyVariables(user, buddyVars, true, false);
   }
}


Can you reproduce the error using our example and a simple test extension?
Thank you.
Paolo Bax
The SmartFoxServer Team
User avatar
faisal007
Posts: 37
Joined: 27 Sep 2016, 09:01

Re: InvalidCastException in user and buddy variables.

Postby faisal007 » 03 Feb 2017, 06:35

Alright i will try test 2 and will post results.

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 30 guests