- Home /
Running only one script across clients and server
I am making a trading card game. I have a Game Controller script that fills up lists with game objects, and instantiates cards based on those lists. Both the card backs to click on that act as decks, and the cards themselves.
When I went to make my game networked, I ran into some trouble. I want to assign heirlooms to each player with the ComputerPickHeirlooms function, and then show them the heirlooms that were picked with the ShowHeirloomsToPlayer function.
I want the both players to pull out of the same deck, so there should be no repeats. Unless you are very lucky in my script, there are repeats.
I think what is happening is each computer is running a different instance of the script. How do I make it only make one instance that each player can look at? Alternately, how do I make it so they pull out of the same deck?
Here is GameController.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameControllerNetwork : MonoBehaviour {
//These are public objects I have pulled into the GameController prefab's inspector.
public static bool startBool = false;
public GameObject cardback;
public GameObject fivegold;
public GameObject decoy;
public GameObject fireskeleton;
public GameObject flicker;
public GameObject gremlins;
public GameObject iceminotaur;
public GameObject jelly;
public GameObject venomviper;
public GameObject coldroom;
public GameObject cursedroom;
public GameObject fireroom;
public GameObject poisonroom;
public GameObject stoneroom;
public GameObject coldtrap;
public GameObject cursedtrap;
public GameObject firetrap;
public GameObject poisontrap;
public GameObject stonetrap;
public GameObject familyjewel;
public GameObject timestwo;
public GameObject swordofwar;
public GameObject hammeroflife;
public GameObject necklaceofregeneration;
public GameObject glovesofpilfering;
public GameObject staffofdeath;
public GameObject shieldofpeace;
public GameObject unlocked;
public GameObject fireknight;
public GameObject frontlineminer;
public GameObject frostmage;
public GameObject lichmage;
public GameObject ranger;
public GameObject thief;
public GameObject[] cardToDestroy;
//GetClicked.cs variables
//GetClicked.cs says
//OnMouseDown, if (clickCounter == howManyClicks && ranFunction == false)
//SendMessage whatFunction; ranFunction = true; else clickCounter++;
public static int clickCounter;
public static string whatFunction;
public static bool ranFunction = false;
public static int howManyClicks;
public static int cardsDealt;
public List<GameObject> heirloomDeck = new List<GameObject>();
public static List<GameObject> heirloomHandp1 = new List<GameObject>();
public static List<GameObject> heirloomHandp2 = new List<GameObject>();
public static List<GameObject> cardBackDeck = new List<GameObject>();
// Use this for initialization
void Start () {
}
void StartHeirlooms() {
Debug.Log ("Start Heirlooms!");
InitializeCardBackDeck ();
}
void InitializeCardBackDeck()
{
Debug.Log ("CardBackDeck Initialization");
cardBackDeck.Add (cardback);
cardBackDeck.Add (cardback);
cardBackDeck.Add (cardback);
cardBackDeck.Add (cardback);
cardBackDeck.Add (cardback);
cardBackDeck.Add (cardback);
whatFunction = "ShowHeirloomsToPlayer";
howManyClicks = 3;
ShowCards (cardBackDeck.Count, cardBackDeck, 1.5f, 1.5f, "Cardbacks");
InitializeHeirlooms();
}
void InitializeHeirlooms(){
Debug.Log ("HeirloomDeck Initialization");
heirloomDeck.Add(swordofwar);
heirloomDeck.Add(hammeroflife);
heirloomDeck.Add(necklaceofregeneration);
heirloomDeck.Add(glovesofpilfering);
heirloomDeck.Add(staffofdeath);
heirloomDeck.Add(shieldofpeace);
ComputerPickHeirlooms();
}
//Once deck Lists are filled up, pick three random Heirlooms and put them in a List for p1, then p2
void ComputerPickHeirlooms()
{
Debug.Log ("Computer picks Heirlooms for p1");
for (int i = 0; i < 3; i++) {
int randomNumber = Random.Range (0, heirloomDeck.Count - 1);
if (heirloomDeck[randomNumber] == swordofwar) { heirloomHandp1.Add(swordofwar); }
else if (heirloomDeck[randomNumber] == hammeroflife) { heirloomHandp1.Add(hammeroflife); }
else if (heirloomDeck[randomNumber] == necklaceofregeneration) { heirloomHandp1.Add(necklaceofregeneration); }
else if (heirloomDeck[randomNumber] == glovesofpilfering) { heirloomHandp1.Add(glovesofpilfering); }
else if (heirloomDeck[randomNumber] == staffofdeath) { heirloomHandp1.Add(staffofdeath); }
else if (heirloomDeck[randomNumber] == shieldofpeace) { heirloomHandp1.Add(shieldofpeace); }
else { Debug.Log ("Something has gone terribly wrong."); }
heirloomDeck.RemoveAt(randomNumber);
Debug.Log (heirloomHandp1[i] + " Heirloom card for p1");
}
Debug.Log ("Computer picks Heirlooms for p2.");
for (int i = 0; i < 3; i++) {
int randomNumber2 = Random.Range (0, heirloomDeck.Count - 1);
if (heirloomDeck[randomNumber2] == swordofwar) { heirloomHandp2.Add(swordofwar); }
else if (heirloomDeck[randomNumber2] == hammeroflife) { heirloomHandp2.Add(hammeroflife); }
else if (heirloomDeck[randomNumber2] == necklaceofregeneration) { heirloomHandp2.Add(necklaceofregeneration); }
else if (heirloomDeck[randomNumber2] == glovesofpilfering) { heirloomHandp2.Add(glovesofpilfering); }
else if (heirloomDeck[randomNumber2] == staffofdeath) { heirloomHandp2.Add(staffofdeath); }
else if (heirloomDeck[randomNumber2] == shieldofpeace) { heirloomHandp2.Add(shieldofpeace); }
else { Debug.Log ("Something has gone terribly wrong."); }
heirloomDeck.RemoveAt(randomNumber2);
Debug.Log (heirloomHandp2[i] + " Heirloom card for p2");
}
}
void ShowHeirloomsToPlayer()
{
//shows Heirlooms to player, quit changing
DestroyHand ("Cardbacks");
networkView.RPC("ShowHeirloomCardsRPC", RPCMode.All);
}
//Destroys cards with tagname
void DestroyHand(string tagname)
{
cardToDestroy = GameObject.FindGameObjectsWithTag(tagname);
foreach (GameObject move in cardToDestroy) {
move.AddComponent("Die");
}
}
// Shows heirloomHandp1 to p1 who is server, shows heirloomHandp2 to p2 who is client.p'
[RPC] void ShowHeirloomCardsRPC ()
{
if (networkView.isMine){
Debug.Log("Showing" + heirloomHandp1.Count + " Heirlooms to server.");
//ShowCardsToNetwork (heirloomHandp1.Count, heirloomHandp1, -6.5f, 1.5f, "ShownHeirloomCards");
ShowCards (heirloomHandp1.Count, heirloomHandp1, -6.5f, 1.5f, "ShownHeirloomCards");
}
if(!networkView.isMine){
Debug.Log("Showing" + heirloomHandp2.Count + " Heirlooms to others.");
//ShowCardsToNetwork (heirloomHandp2.Count, heirloomHandp2, -8.5f, 1.5f, "ShownHeirloomCards");
ShowCards (heirloomHandp2.Count, heirloomHandp2, -8.5f, 1.5f, "ShownHeirloomCards");
}
}
void ShowCardsToNetwork(int handCount, List<GameObject> deck, float targetx, float targety, string tagname)
{
cardsDealt = 0;
for (int i = 0; i < handCount; i++)
{
Debug.Log("Showing " + deck[cardsDealt]);
DealCardToNetwork (deck, cardsDealt, targetx, targety, tagname);
cardsDealt++;
}
}
public GameObject DealCardToNetwork (List<GameObject> deck, int cardsDealt, float targetx, float targety, string tagname)
{
if (deck[cardsDealt] != null)
{
Debug.Log("Showing " + deck[cardsDealt]);
Vector2 targetxy = new Vector2(targetx, targety);
GameObject go = Network.Instantiate (deck [cardsDealt], targetxy, Quaternion.identity, 0) as GameObject;
go.transform.position = new Vector2 ((float)targetx + cardsDealt / 1.5f, (float)targety + cardsDealt / 1.5f); // place card 1/4 up on all axis from last
if (howManyClicks != 0) {
Debug.Log("Putting getclicked and boxcollider on cards because howManyClicks = " + howManyClicks);
go.AddComponent ("GetClicked");
go.AddComponent ("BoxCollider");
go.collider.isTrigger = true;
go.tag = tagname;
}
else {
Debug.Log("Didn't put getclicked on card because howManyClicks = 0");
}
return go;
}
else { Debug.Log("Put cards in " + deck); }
return null;
}
void ShowCards(int handCount, List<GameObject> deck, float targetx, float targety, string tagname)
{
cardsDealt = 0;
for (int i = 0; i < handCount; i++)
{
Debug.Log("Showing " + deck[cardsDealt]);
DealCard (deck, cardsDealt, targetx, targety, tagname);
cardsDealt++;
}
}
public GameObject DealCard (List<GameObject> deck, int cardsDealt, float targetx, float targety, string tagname)
{
if (deck[cardsDealt] != null)
{
Debug.Log("Showing " + deck[cardsDealt]);
Vector2 targetxy = new Vector2(targetx, targety);
GameObject go = GameObject.Instantiate (deck [cardsDealt]) as GameObject;
go.transform.position = new Vector2 ((float)targetx + cardsDealt / 1.5f, (float)targety + cardsDealt / 1.5f); // place card 1/4 up on all axis from last
if (howManyClicks != 0) {
Debug.Log("Putting getclicked and boxcollider on cards because howManyClicks = " + howManyClicks);
go.AddComponent ("GetClicked");
go.AddComponent ("BoxCollider");
go.collider.isTrigger = true;
go.tag = tagname;
}
else {
Debug.Log("Didn't put getclicked on card because howManyClicks = 0");
}
return go;
}
else { Debug.Log("Put cards in " + deck); }
return null;
}
// Update is called once per frame
void Update () {
//if (GameControllerNetwork.startBool == false && NetworkManager.Connected) {
// StartHeirlooms();
// GameControllerNetwork.startBool = true; }
}
}
And here is the key function in my NetworkController.cs
void OnConnectedToServer()
{
Debug.Log("Server Joined");
connected = true;
Network.Instantiate(gameControllerPrefab, transform.position, Quaternion.identity, 0);
}