Hello Guest

Author Topic: MultiPlayerGame  (Read 4105 times)

8BitGuy

  • Newbie
  • *
  • Posts: 9
    • View Profile
MultiPlayerGame
« on: February 21, 2014, 04:39:19 pm »
Hello Unikron, I've started a multiplayer mode in my game, and it all works fine but one thing, the sprites doesn't change.

I mean, if I use an animation with a sprite in the server the client see the same sprite, not the animation.

is there a way to refresh the sprite like networkview?

thanks a lot.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: MultiPlayerGame
« Reply #1 on: February 22, 2014, 12:40:49 pm »
You will need to propagate the changes somehow. Eg. RPC, etc. The sprite data isn't constantly monitored by the system, so just replicating data isn't sufficient. The mesh update is only triggered using the c# properties, so you will basically have to trigger these yourself when it changes.

8BitGuy

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: MultiPlayerGame
« Reply #2 on: February 22, 2014, 02:29:22 pm »
Resolved, I attached a simple script that pick the sprite id of the sprite script and if it changes it sends by RPC and set it.
I drop here the code if someone find it useful:


Code: [Select]
using UnityEngine;
using System.Collections;

public class SpriteRefresh : MonoBehaviour {
private tk2dSprite thisSprite;
private int idusingSprite;
// Use this for initialization
void Start () {
thisSprite=GetComponent<tk2dSprite>();
idusingSprite=thisSprite.spriteId;
}

// Update is called once per frame
void Update () {
if(thisSprite.spriteId != idusingSprite){
networkView.RPC("ChangeSprite",RPCMode.Others,idusingSprite);
idusingSprite=thisSprite.spriteId;
}
}

[RPC]
void ChangeSprite(int idSprite){
thisSprite.SetSprite(idSprite);
idusingSprite=thisSprite.spriteId;
}

}

Thanks unikron, I was new to networking but it was so easy I feel stupid asking these things -_-

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: MultiPlayerGame
« Reply #3 on: February 22, 2014, 02:58:11 pm »
Don't use update, that runs every frame.
Use sprite.SpriteChanged += ChangeSprite instead
This is a callback that gets triggered when the sprite changes, so you don't need to keep polling.

Check tk2dSpriteAttachPoint.cs for an example where its used.
In theory, the event handler doesn't even need to be in a mono behaviour if you want to optimise that far - you can simply have a static function to handle this, the only thing you need to do is hook this up properly, and remove it when the sprite is destroyed.