x


When using Network.instantiate, how to set an additional setting for all clients (such as a script variable) ?

Hi,

When using Network.instantiate, how would one define an additional setting?

For instance, if I were to spawn a gun that has 4 bullets inside, I would call:

GameObject tempGun = ((Transform)Network.Instantiate(Gun, _targetSpawnLocation, Quaternion.identity, 0)).gameObject;

On the local code I write: (The gun has a script attached called "GunInfo")

tempGun.GetComponent<GunInfo>().Bullets = 4;

However, not all clients know how many bullets the gun has. How do I synchronize this with them?

An extra note: It would be possible using OnSerializeNetworkView(), but this runs every Update() and is completely unnecessary. It only needs to run once initially so all clients know how many bullets the gun has.

I suppose it's possible using an RPC, but how do I identify the object on all clients? How do I get the network-instantiated gun object on all clients code-wise?

Thanks, Nick

more ▼

asked Feb 08 '11 at 03:07 PM

Nikke gravatar image

Nikke
52 9 9 14

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I guess the object you instantiate have a NetworkView attached? If so it's quite easy. RPC calls are send to the objects with a NetworkView with the same ViewID.

public class GunInfo : MonoBehaviour
{
    private int m_Bullets = 0;
    public int Bullets
    {
        get { return m_Bullets; }
        set
        {
            m_Bullets = value;
            if (networkView.isMine)
                networkView.RPC("RPC_SetBullets",RPCMode.Others,m_Bullets);
        }
    }

    [RPC]
    void RPC_SetBullets(int aBullets)
    {
        m_Bullets = aBullets;
    }
}

Thanks to the property you don't have to worry about syncing.

If you don't have a NetworkView on your object you have to implement some kind of object management that keeps a list of all objects and using own IDs to find the corresponding objects.

Do you use an authoritative server setup? Or a "normal mixed" setup where clients can create objects? The only important thing regarding my solution is to keep in mind that only the player that created the object can sync the bullet count.

more ▼

answered Feb 08 '11 at 03:40 PM

Bunny83 gravatar image

Bunny83
45.5k 11 49 207

Bunny's got the right solution here. My question is, why do clients care how many bullets other clients have?

Feb 08 '11 at 03:46 PM PrimeDerektive

Thanks Bunny, I'll try it out!

Derek, the gun was just an simple example I thought about, so I wouldn't have to explain my own complex situation ;)

Feb 08 '11 at 03:58 PM Nikke

What is the networkView.isMine for? Isn't the networkView always a component of the current gameobject it is attached to?

Feb 08 '11 at 04:19 PM Nikke

isMine is just a easier way to check the owner (creator) of that NetworkViewID. It's the same as (networkView.viewID.isMine) or (networkView.viewID.owner == Network.player)

Feb 08 '11 at 04:45 PM Bunny83

What bunny said. The owner part is not referring to the object that owns it, but rather the network player that owns it (if you're using network.instantiate, the network player that called network.instantiate).

Feb 08 '11 at 04:50 PM PrimeDerektive
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1682
x711
x125
x9

asked: Feb 08 '11 at 03:07 PM

Seen: 2556 times

Last Updated: Feb 08 '11 at 03:17 PM