SFSObject Copy to other in Serverside

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

Moderators: Lapo, Bax

Sreelu
Posts: 22
Joined: 03 Oct 2016, 08:02

SFSObject Copy to other in Serverside

Postby Sreelu » 16 Mar 2017, 19:13

I have an SFSObject which needs to be copied to 5 other different SFSObjects and only one property will be changed in each,
So, Please let me know how do we copy one SFSObject to other as the same reference is being returned everytime.

My Sample Code:

Code: Select all

SFSArray roomDataListTobeSend = new SFSArray();
      ISFSObject obj = new SFSObject();
      obj.putUtfString("roomname", "vinni");
                obj.putBool("exists", true);
                obj.putInt("count", 1);
      for(int i =1; i <= 5; i++){
         ISFSObject obj1 = new SFSObject();
                        obj1 = obj;
         String counterRoom = "vinni"+" "+i;
         obj1.putUtfString( "roomname", counterRoom );
         roomDataListTobeSend.addSFSObject(obj1);         
      }
      for(int i = 0; i< roomDataListTobeSend.size(); i++){
         SFSObject s = (SFSObject) roomDataListTobeSend.getSFSObject(i);
         System.out.println(s.getUtfString("roomname"));
      }   


Output at the end of loop is "5 5 5 5 5" all the time instead of 1, 2, 3, 4 ,5. I tried SFSObject.newFromObject(obj), which is throwing error like Class needs to be serialized..
Please help
frodo86
Posts: 14
Joined: 15 Jan 2016, 09:47

Re: SFSObject Copy to other in Serverside

Postby frodo86 » 17 Mar 2017, 07:56

Code: Select all

obj1.putUtfString( "roomname", counterRoom );

You are overwriting the variable

The object stores the last record, and it vinni 5
Sreelu
Posts: 22
Joined: 03 Oct 2016, 08:02

Re: SFSObject Copy to other in Serverside

Postby Sreelu » 17 Mar 2017, 08:27

True..that iam overwriting...But iam adding each object to sfsarray..so, sfsarray should have different 5 objects as I newly created each sfsobject..
Only Change in all 5 objects is only roomname..SO, I had overwritten that..
frodo86
Posts: 14
Joined: 15 Jan 2016, 09:47

Re: SFSObject Copy to other in Serverside

Postby frodo86 » 17 Mar 2017, 08:40

Code: Select all

         ISFSObject obj1 = new SFSObject();
          obj1 = obj;


You do not create a new object, you just copy obj
This object contains params roomname
obj1.putUtfString ("roomname", counterRoom);

What do you want to do?Tell us, we will help you
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SFSObject Copy to other in Serverside

Postby Lapo » 17 Mar 2017, 09:03

My recommendation is to create the 5 objects manually instead of "copying" them.
If they share common elements use a for loop to populate the common data and then add the remaining custom bits to each separate object.

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
Sreelu
Posts: 22
Joined: 03 Oct 2016, 08:02

Re: SFSObject Copy to other in Serverside

Postby Sreelu » 17 Mar 2017, 09:04

I have an SFSObject..which has 10 attributes like roomname, roomid and soon..
I want to copy all those values with key and values intact into other object except for roomname..
After copy the whole object, planning to overwrite that roomname..

As my original object has too many attributes, I dint feel like writing each and every attribute to be written in code like

ISFSObject obj1 = new SFSObject();
obj1.putUtfString("roomid", obj.getUtfString("roomid"));
.....
.....

Thats the reason, I would like to copy the object to new instance and then change the attribute I would like to.

Ofcourse I solved my problem this way:

ISFSObject newObj = new SFSObject();
newObj.putInt("playersregistered", obj.getInt("playersregistered")); // replacing already got registered players with fresh numbers
newObj.putUtfString("status", obj.getUtfString("status"));
newObj.putUtfString("roomname", obj.getUtfString("roomname"));
newObj.putLong("id", obj.getLong("id"));

Looking for a optimal code!! Plz help..

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 75 guests