Unity Network: Convert int to ViewID

I want to get as fast as I can to the point, What I’m trying to to is a Semi-Authoritative Server for an online Life simulator.

I’ve been working on it for 3 days, And I managed to get good results. I can Spawn any player in any Map I have, and I can see other network players that spawns too.

The problem is: How to spawn players that logged before me? I can spawn players that logged after me, but they can’t still see me because they logged BEFORE me.

I actually found a semi-solution by using this system:

Spawn The player using ViewID-1

Maybe I’m not explaining myself too well, So I’m posting a part of the client-side script that SHOULD spawn correctly the players before me and then assign them a custom NetworkView.viewID:

@RPC

function SpawnPlayersBeforeMe () {

var HowManyPlayersBeforeMe = PlayerID -1 * Time.deltaTime;

while(HowManyPlayersBeforeMe > 1){

Clone = Instantiate(PlayerPrefab, transform.position, transform.rotation);

Clone.GetComponent(NetworkView).viewID = PlayerID-1 ;

HowManyPlayersBeforeMe -=1 * Time.deltaTime;

return;

	}
}

the result is:Assets/Scripts/NetworkScript.js(309,50): BCE0022: Cannot convert ‘int’ to ‘UnityEngine.NetworkViewID’.

You can generate an id using AllocateViewID
http://docs.unity3d.com/Documentation/ScriptReference/Network.AllocateViewID.html

It has to be specific, like the one up there, the:

Clone.GetComponent(NetworkView).viewID =PlayerID-1 ;

The type is of NetworkViewID and not int

I know that, that’s why I want to know if there’s a workaround for this.

someone? Anyone?

Your initial problem will not be solved by generating your own view id’s. Unity handles it behind the scenes, so no reason to change this.
Your problem is that you are not buffering the RPC calls for joining clients.

I searcher trough network, but still I have no clue what buffering is.

PS: For what I’m doing and for the way I setted up my server, it’s kind of the ONLY way, that’s why I know if there’s a way to do the PlayerID-1 Part.

At some point you will need to start reading the documentation:
http://docs.unity3d.com/Documentation/ScriptReference/30_search.html?q=buffered

No, it most certainly isn’t. It’s more likely a lack of knowledge on the matter.

At the end I managed to solve my problem using this simple System.

I added a RPC call to the client, that would tell the server to tell the client to spawn the players before, It’s a pretty dirty code, but it does work if you have your server set up like mine.

I’m also providing the code because I saw how much people had my same problem, and I truly want to help them.

Client-Side:

@RPC

function SpawnPlayerAfterMe(player: NetworkPlayer, View : NetworkViewID) {

if(Player.networkView.viewID != View) {

networkView.RPC("SendPlayerInfo", RPCMode.Server, player, Player.networkView.viewID, Player.position, Player.rotation);

Clone = Instantiate(PlayerPrefab, transform.position, transform.rotation);

Clone.networkView.viewID = View;

SpawnIt();

	}
}

@RPC

function SpawnPlayerBeforeMe(View : NetworkViewID, Pos : Vector3, Rot : Quaternion) {

Clone = Instantiate(PlayerPrefab, Pos, Rot);

Clone.networkView.viewID = View;

SpawnIt();

}

Server-Side:

@RPC

function SendPlayerInfo(Player : NetworkPlayer, View : NetworkViewID, Pos : Vector3, Rot : Quaternion) {

networkView.RPC("SpawnPlayerBeforeMe", Player, View, Pos, Rot);

}

This is not the full code, still it’s not that hard to fix for every other networking system.