How to get UserVariable in OnUserVarsUpdate

Post here your questions about the C++ API for SFS2X

Moderators: Lapo, Bax, MBagnati

ganquan
Posts: 38
Joined: 17 Apr 2013, 01:42

How to get UserVariable in OnUserVarsUpdate

Postby ganquan » 19 Apr 2013, 06:11

with VC2010 , C++ API 0.9.3 , i change user's state at serve :

Code: Select all

UserVariable uv_state = new SFSUserVariable("state", "waiting");
List<UserVariable> vars = Arrays.asList(uv_state);
getApi().setUserVariables(user, vars);


and at client, i try to get the state:

Code: Select all

void ConnectionTest::OnUserVarsUpdate(void* ptrContext, BaseEvent* ptrEvent)
{
   vector<string>* changedVars = (vector<string>*)((*(ptrEvent->Params()))["changedVars"]);
   User * user = (User*)((*(ptrEvent->Params()))["user"]);

   string strBuffer = " user:" ;
   strBuffer += user->Name()->c_str();
   strBuffer += "change ";

   for ( int i = 0 ; i < changedVars->size() ;i ++ )
   {
      strBuffer += "  ";
      strBuffer += changedVars->at(i);
      strBuffer += ":";
      string * strKey = new string(changedVars->at(i));
      UserVariable * userVar = user->GetVariable(strKey);
      if ( userVar != NULL )
         strBuffer += userVar->GetStringValue()->c_str();
   }

   MessageBox(NULL,strBuffer.c_str(),"SmartFoxTest",MB_OK);
}


i can receive the callback, and "state" is in the changedVars, but i can not get the value in user, user->GetVariable return null.
MBagnati
Posts: 125
Joined: 12 Feb 2013, 10:57

Re: How to get UserVariable in OnUserVarsUpdate

Postby MBagnati » 21 Apr 2013, 09:46

Please use next API release 0.9.4 that contains some bug fixing and try follow statements to read vars.

Get the changed vars

vector<string*>* changedVars = (vector<string*>*)((*(ptrEvent->Params()))["changedVars"]);
User* user = (User*)((*(ptrEvent->Params()))["user"]);

Read a string var having name "MyStringVar"

UserVariable* variableString = user->GetVariable(new string("MyStringVar"));
string valueString = *(variableString->GetStringValue());

Read a boolean var having name "MyBoolVar"

UserVariable* variableBool = user->GetVariable(new string("MyBoolVar"));
bool valueBool = *(variableBool->GetBoolValue());

Read an integer var having name "MyIntVar"

UserVariable* variableInt = user->GetVariable(new string("MyIntVar"));
long int valueInt = *(variableInt->GetIntValue());

Read a double var having name "MyDoubleVar"

UserVariable* variableDouble = user->GetVariable(new string("MyDoubleVar"));
double valueDouble = *(variableDouble->GetDoubleValue());
ganquan
Posts: 38
Joined: 17 Apr 2013, 01:42

Re: How to get UserVariable in OnUserVarsUpdate

Postby ganquan » 22 Apr 2013, 02:01

it not work
1.
UserVariable* variableInt = user->GetVariable(new string("MyIntVar"));
long int valueInt = *(variableInt->GetIntValue());

get error when compile: error C2100: illegal indirection

2.break point after User* user = (User*)((*(ptrEvent->Params()))["user"]); and the size of the "variables" map in user is 0
User avatar
Bax
Site Admin
Posts: 4395
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: How to get UserVariable in OnUserVarsUpdate

Postby Bax » 22 Apr 2013, 08:21

This was fixed in API v0.9.4 we just published: viewtopic.php?f=34&t=15963
Thank you for reporting.
Paolo Bax
The SmartFoxServer Team
prince_hao
Posts: 71
Joined: 30 Oct 2010, 04:35

Re: How to get UserVariable in OnUserVarsUpdate

Postby prince_hao » 29 Dec 2014, 04:28

MBagnati wrote:Please use next API release 0.9.4 that contains some bug fixing and try follow statements to read vars.

Get the changed vars

vector<string*>* changedVars = (vector<string*>*)((*(ptrEvent->Params()))["changedVars"]);
User* user = (User*)((*(ptrEvent->Params()))["user"]);

Read a string var having name "MyStringVar"

UserVariable* variableString = user->GetVariable(new string("MyStringVar"));
string valueString = *(variableString->GetStringValue());

Read a boolean var having name "MyBoolVar"

UserVariable* variableBool = user->GetVariable(new string("MyBoolVar"));
bool valueBool = *(variableBool->GetBoolValue());

Read an integer var having name "MyIntVar"

UserVariable* variableInt = user->GetVariable(new string("MyIntVar"));
long int valueInt = *(variableInt->GetIntValue());

Read a double var having name "MyDoubleVar"

UserVariable* variableDouble = user->GetVariable(new string("MyDoubleVar"));
double valueDouble = *(variableDouble->GetDoubleValue());

hi MBagnati,
I want to know how can I like as3 API traversal the changedVars! thanks!
MBagnati
Posts: 125
Joined: 12 Feb 2013, 10:57

Re: How to get UserVariable in OnUserVarsUpdate

Postby MBagnati » 07 Jan 2015, 01:44

Hi prince_hao
To enumerate changed vars you can follow these steps:
  • Register a listener for USER_VARIABLES_UPDATE event
    For instance this statement declares the method OnSmartFoxUserVariablesUpdate of CMyClass as event listener

    Code: Select all

       m_ptrSmartFox->AddEventListener(SFSEvent::USER_VARIABLES_UPDATE, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(CMyClass::OnSmartFoxUserVariablesUpdate, (unsigned long long)0)));

  • Implement the event listener to enumerate changed vars and get their values according on variable type

    Code: Select all

    void CMyClass::OnSmartFoxUserVariablesUpdate(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent)
    {
       // Get event params
       boost::shared_ptr<map<string, boost::shared_ptr<void>>> ptrEventParams = ptrEvent->Params();

       // Get list of changed vars names from event params
       boost::shared_ptr<vector<string>> ptrChangedVarNames = ((boost::static_pointer_cast<vector <string>>))((*ptrEventParams)["changedVars"]);

       // Get user that owns changed vars from event params
       boost::shared_ptr<User> ptrVarOwner = ((boost::static_pointer_cast<User>))((*ptrEventParams)["user"]);

       // Enumerate changed vars
       vector<string>::iterator iteratorVars;
       for (iteratorVars = ptrChangedVarNames->begin(); iteratorVars != ptrChangedVarNames->end(); iteratorVars++)
       {
          // Get the name of a changed var
          string name = *iteratorVars;
          CStringA csVarName(name.c_str());

          // Get changed var and read its value according on variable type
          boost::shared_ptr<UserVariable> ptrUserVariable = ptrVarOwner->GetVariable(name);
          switch(ptrUserVariable->Type())
          {
          case VARIABLETYPE_UNKNOWN:
             break;

          case VARIABLETYPE_NULL:
             break;

          case VARIABLETYPE_BOOL:
             {
                CString csDump;
                csDump.Format(_T("Var %s = %d\n"), CMainFrame::UTF8ToUTF16(csVarName), *(ptrUserVariable->GetBoolValue()));
                OutputDebugString(csDump);
                break;
             }

          case VARIABLETYPE_INT:
             {
                CString csDump;
                csDump.Format(_T("Var %s = %d\n"), CMainFrame::UTF8ToUTF16(csVarName), *(ptrUserVariable->GetIntValue()));
                OutputDebugString(csDump);
                break;
             }

          case VARIABLETYPE_DOUBLE:
                            // To be implemented
             break;

          case VARIABLETYPE_STRING:
             {
                CStringA csVariableValueUtf8(ptrUserVariable->GetStringValue()->c_str());

                CString csDump;
                csDump.Format(_T("Var %s = %s\n"), CMainFrame::UTF8ToUTF16(csVarName), CMainFrame::UTF8ToUTF16(csVariableValueUtf8));
                OutputDebugString(csDump);
                break;
             }

          case VARIABLETYPE_OBJECT:
                            // To be implemented
             break;

          case VARIABLETYPE_ARRAY:
                            // To be implemented
             break;
          }
       }
    }

Return to “SFS2X C++ API”

Who is online

Users browsing this forum: No registered users and 1 guest