Page 1 of 1

Streaming Components

PostPosted: Fri Oct 28, 2016 10:56 am
by zhov
Hello.

Just started working with Isogenic, and I was wondering how one would go about streaming components over the network. In the multiplayer-isometric example, it shows adding a component without options. For instance if I have a 'CharacterComponent' that contains things like 'name' and 'gender', how do I get those values to the client when the entity is created after they connect?

Currently my ClientNetworkEvents.js looks like this:

Code: Select all
var ClientNetworkEvents = {
  _onPlayerEntity: function(data) {
    if (ige.$(data)) {
      ige.$(data).addComponent(PlayerComponent);
      ige.client.viewport.camera.trackTranslate(ige.$(data), 50);
    } else {
      var self = this;
      self._eventListener = ige.network.stream.on('entityCreated', function(entity) {
        if (entity.id() === data) {
          ige.$(data).addComponent(PlayerComponent);
          ige.client.viewport.camera.trackTranslate(ige.$(data), 50);
          ige.network.stream.off('entityCreated', self._eventListener, function(result) {
            if (!result) {
              this.log('Could not disable event listener!', 'warning');
            }
          });
        }
      });
    }
  }
};

if (typeof(module) !== 'undefined' && typeof(module.exports) !== 'undefined') { module.exports = ClientNetworkEvents; }


Adding the PlayerComponent is no problem because it has no options. The question is how to get the 'name', 'gender', and etc options to the client so I can instantiate the 'CharacterComponent' component which will need those values populated?

Would the best option just be streaming custom fields like characterComponentOptions and instantiating the component when they are received, or am I missing something much more obvious?

Thanks!

Re: Streaming Components

PostPosted: Fri Oct 28, 2016 10:17 pm
by zhov
So far the only thing I've come up with is something like:

Code: Select all
// After the entity is created (serverside)
        entity._customData = JSON.stringify({
          character: {
            name: 'test',
            gender: 'male'
          }
        });

// In the entity
   this.streamSections(['transform', 'custom']); // (client/server)

   streamSectionData: function(sectionId, data) {
     if (sectionId == 'custom') {
       if (data && ige.isClient) {
         this._customData = JSON.parse(data);
         if (!this.character) {
           this.addComponent(CharacterComponent, data.character);
           /* do other client-side setup stuff here */
         }
       } else {
         return this._customData;
       }
     } else {
       return IgeEntity.prototype.streamSectionData.call(this, sectionId, da     ta);
     }
   },


Unfortunately, it assigns the _customData every tick, so I have to add the check to make sure the character component doesn't already exist AND it parses the JSON every time the transform changes (on player move). Is there a better way to do this that doesn't involve sending unchanged data along with the changed transform?