I made a gameManager for my game and it’s job is to feed my programs with various static variables like give me all the players on the scene, or tell Me which blocks are empty on my map!
I need to use this to respawn my Characters after they die, and currently I’m using these two functions to calculate the respawn position.
This is the code in the player that tells it where to spawn;
List<Vector3> GetEmptyTiles() { //Code that Calculates Respawn Position
List<Vector3> l = new List<Vector3> ();
for (int i = 0; i <= GameManager.getBlocks().GetLength(0)-1;i++) {
for (int j = 0; j <= GameManager.getBlocks ().GetLength (1)-1; j++) {
if (GameManager.getBlocks () [i, j].GetComponent<Block> ().blockID == tileID.FLOOR) {
l.Add (GameManager.getBlocks () [i, j].transform.position);
}
}
}
print ("ADDED");
return l;
}
Vector3 CalcSpawnPosition() { //Change Network Transform to this to respawn!
int arc4random = Random.Range (0, GetEmptyTiles ().Count);
Vector3 spawnVec3 = GetEmptyTiles()[arc4random];
print("CALCULATED");
return spawnVec3;
}
and this is the code that gets all the Block positions and whatnot:
public class GameManager: NetworkBehaviour {
public static Stage currentStage = null; //After Generating Map the Other Class Initializes this Variable.
...
public static GameObject[,] getBlocks() { //This basically returns the 2D array containing all of my blocks on the map
return currentStage.blocks;
}
}
The problem with this is while it works on the local, main Client it doesn’t work on the connecting Client.
Can someone help me with this?
you will have to use some kind of messaging or syncing to push the data across the wire to the clients. The static var ‘currentStage’ isn’t magically updated on the clients - you have to send a message to let them know what it is. Look into the Command, ClientRPC and SyncVar properties/functions. Good luck!
Command, ClientRPC and SyncVar Don’t work…
Command and ClientRPC say that it has to return a void and SyncVar gives an error because my custom State class isn’t a primitive datatype…
Is there another way update and reference the state variable?
Well, maybe you have to tweak your architecture to work within the confines of the UNET API, or learn the lower level api and use the messaging it makes available for more customization. Just because you can’t send the actual state var across the wire doesn’t mean you can’t send some other message which maps to those states or values. Good luck!
Sorry about asking you so many questions, but if I were to alter the way my game works, what would be a efficient way to get the stage and all of it’s contents?
Thanks for replying, again I’m sorry asking all the noonish questions about uNet and Networking.
Well, i don’t know what the Stage object/class is so hard for me to say. Also, if Command/SyncVar/RPC are not working, then you have implemented them in the right way. They do work, if you use them correctly. I would suggest going back and reading the sections in the unity manual and api reference about networking. Good Luck!