Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

Add custom object to SharedCollection

Avatar

Level 2

Hi,

in my code I have a SharedCollection called 'playerDataCollection'. I want to add a custom Object called PlayerData which I use for storing Player-info like the color, name, cards etc... to it, something like this.

playerDataCollection.addItem({userId:connectSession.userManager.myUserID, playerData:playerData});

If I now see in the ChangeHandler of the playersDataCollection, I can see that its updated, but the playerData is added as a plain object, causing me to loose all the data I could otherwise access with the setters defined in the custom Object.

Is the only solution to pass all separate elements of the playerData object seperately, something like

playerDataCollection.addItem({userId:connectSession.userManager.myUserID, playerName:playerData.name, playerColor:playerData.color, ...});

and reconstruct locally in the ChangeHandler, which would be like this:

private function onPlayerDataCollectionChange(event:CollectionEvent):void{

     playerData = new PlayerData(playerDataCollection[0].playerName, playerDataCollection[0].playerColor);    

}

Is there maybe a more easy solution?

Kind regards,

Hans

1 Accepted Solution

Avatar

Correct answer by
Employee

Hi Hans,

All you need to do is, register your custom PlayerData object using MessageItem.registerBodyClass(PlayerData). Ensure that PlayerData's constructor accept null as default values for the constructor parametres.

For more infomation please refer to CompleObjectTransfer example in the SDK's sampleApps.

Thanks

arun

View solution in original post

3 Replies

Avatar

Correct answer by
Employee

Hi Hans,

All you need to do is, register your custom PlayerData object using MessageItem.registerBodyClass(PlayerData). Ensure that PlayerData's constructor accept null as default values for the constructor parametres.

For more infomation please refer to CompleObjectTransfer example in the SDK's sampleApps.

Thanks

arun

Avatar

Former Community Member

Hi Hans,

In order to successfully round-trip MessageItems which are strongly-typed

classes, you need to call MessageItem.registerBodyClass(PlayerData) - call

this just once as the session synchronizes, and it will remember that this

class should be preserved. If PlayerData contains any other classes (other

than the plain player classes like String, etc), then register those as

well.

However, this really only works when the class you've registered is at the

top level of what you're sending - we don't introspect your objects to see

if one of the registered classes is contained inside what you're sending.

So, to make this work, I'd recommend adding "userId" as a property inside

PlayerData, and sending a PlayerData object :

PlayerDataCollection.addItem(playerData); // playerData has userID inside of

it.

On a semi-related note, I wonder somewhat if a customUserField might not be

a better place to store player data? In this way, your custom playerData can

be attached right to the UserDescriptor of each user - it will be removed

when the player leaves, etc. Might be less management work for you. See

UserManager.registerCustomUserField() and UserManager.setCustomUserField()

in the API reference for more. The same registerBodyClass rules apply there.

hope that helps.

nigel

Avatar

Level 2

Arun, Nigel,

thanks a lot for the help!

I did not know about the registerBodyClass() method, but it might indeed be interesting for me to check out the CustomUserFields: I make sure I'll check that out.

Best regards,

Hans