Click here to Skip to main content
15,849,494 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been bashing my head against this RPC call for a few hours now, and despite extensive googling I cannot figure out what is going wrong.

At the moment, I am doing all testing within unity editor, (player set to run in background if that matters).

For some reason, my RPC call isn't going through.

There is a lot of stuff going on in the code, so the only important bit (I think) is:

C#
public void GenerateDungeon()
 {
     Debug.Log ("GENERATING DUNGEON");
     tiles = dungeon.SetDungeonData();

     //Begin looking through every single tile
     for(int i = 0; i < tiles.Length; i++)
     {
         //If our tile is something useful, aka not a void area in the dungeon
         if(tiles[i] != 0){

             //Bogus location
             int x = 0;
             int z = 0;

             //Real World Location
             convert.ConvertIndexToXY(i,ref x,ref z);

             //Wouldn't need if I understood the math, but since I don't being extra careful
             int leftTile = tiles[convert.ConvertXYToIndex(x-1,z)];
             int rightTile = tiles[convert.ConvertXYToIndex(x+1,z)];
             int topTile = tiles[convert.ConvertXYToIndex(x,z-1)];
             int bottomTile = tiles[convert.ConvertXYToIndex(x,z+1)];

             //Create a base layer for compairing dungeon
             CreateDebugDungeon(x,z);

             //Determine what we are trying to spawn, and spawn it
             if(tiles[i] == (short)Enums.TileType.Corner)
                 CheckCorners(leftTile, rightTile, topTile, bottomTile, x, z, dungeonWidth, dungeonHeight);

             if(tiles[i] == (short)Enums.TileType.OneWall)
                 CheckOneWalls(leftTile, rightTile, topTile, bottomTile, x, z, dungeonWidth, dungeonHeight);

             if(tiles[i] == (short)Enums.TileType.FloorAndRoof)
                 CreateTile((int)Enums.TileType.FloorAndRoof, x,z,0,dungeonWidth,dungeonHeight);

             if(tiles[i] == (short)Enums.TileType.Corridor)
                 CheckCorridors(leftTile, rightTile, topTile, bottomTile, x, z, dungeonWidth, dungeonHeight);
         }//end if tiles != 0
     }//end for

     Debug.Log ("Done pushing tiles");
     //networkView.RPC("SpawnCharacter",RPCMode.AllBuffered, team1Spawn);
     networkView.RPC ("SpawnCharacter",RPCMode.All);
     //networkView.RPC ("Merp",RPCMode.All);
     //networkView.RPC ("PushTiles",RPCMode.All);
     //Debug.Log ("Merr?");

 }//end generate dungeon

 [RPC]
 void SpawnCharacter()
 {
     Debug.Log ("Spawning character at : " );
     //Instantiate(character,position, Quaternion.identity);
 }

At the moment, this should only be called server side.

Here is the full code for the script:


C#
    using UnityEngine;
    using System.Collections;
    /// 
    /// BSP Spawning Script is responsible creating a dungeon from scratch to finished.
    /// 
    public class BSPSpawningScript : MonoBehaviour{

    public int dungeonWidth = 80;
    public int dungeonHeight = 80;
    public int numSplits = 4;
    public GameObject character;
    public float charYOffset;

    short[] tiles;

    BSPDungeon dungeon;
    CoConvert convert;
    bool team1  = false;
    bool team2 = false;
    GameObject lastTile;
    Vector3 team1Spawn;

    void Start()
    {
        if(Network.isClient)
        {
            GameObject.Destroy(this.gameObject);
            Debug.Log ("Destroying");	
        }

        convert = new CoConvert(dungeonWidth);
        dungeon = new BSPDungeon(dungeonWidth, dungeonHeight);
        dungeon.treeData.SplitNodes(numSplits);
        dungeon.GenerateRooms ();
        dungeon.GenerateCorridors();
        GenerateDungeon();
    }

    public void Reset()
    {
        dungeon.Reset();
        dungeon.treeData.SplitNodes(numSplits);
        dungeon.GenerateRooms ();
        dungeon.GenerateCorridors();
        GenerateDungeon();
    }

    public void GenerateDungeon()
    {
        Debug.Log ("GENERATING DUNGEON");
        tiles = dungeon.SetDungeonData();
    
        //Begin looking through every single tile
        for(int i = 0; i < tiles.Length; i++)
        {	
            //If our tile is something useful, aka not a void area in the dungeon
            if(tiles[i] != 0){
    
            //Bogus location
            int x = 0;
            int z = 0;

            //Real World Location
            convert.ConvertIndexToXY(i,ref x,ref z);

            //Wouldn't need if I understood the math, but since I don't being extra careful
            int leftTile = tiles[convert.ConvertXYToIndex(x-1,z)];
            int rightTile = tiles[convert.ConvertXYToIndex(x+1,z)];
            int topTile = tiles[convert.ConvertXYToIndex(x,z-1)];
            int bottomTile = tiles[convert.ConvertXYToIndex(x,z+1)];

            //Create a base layer for compairing dungeon
            CreateDebugDungeon(x,z);

            //Determine what we are trying to spawn, and spawn it
            if(tiles[i] == (short)Enums.TileType.Corner)
                CheckCorners(leftTile, rightTile, topTile, bottomTile, x, z, dungeonWidth, dungeonHeight);

            if(tiles[i] == (short)Enums.TileType.OneWall)
                CheckOneWalls(leftTile, rightTile, topTile, bottomTile, x, z, dungeonWidth, dungeonHeight);

            if(tiles[i] == (short)Enums.TileType.FloorAndRoof)
                CreateTile((int)Enums.TileType.FloorAndRoof, x,z,0,dungeonWidth,dungeonHeight);

            if(tiles[i] == (short)Enums.TileType.Corridor)
                CheckCorridors(leftTile, rightTile, topTile, bottomTile, x, z, dungeonWidth, dungeonHeight);	
            }//end if tiles != 0
		}//end for

        Debug.Log ("Done pushing tiles");
        //networkView.RPC("SpawnCharacter",RPCMode.AllBuffered, team1Spawn);
        networkView.RPC ("SpawnCharacter",RPCMode.All);
        //networkView.RPC ("Merp",RPCMode.All);
        //networkView.RPC ("PushTiles",RPCMode.All);
        //Debug.Log ("Merr?");

    }//end generate dungeon

    [RPC]
    void SpawnCharacter()
    {
        Debug.Log ("Spawning character at : " );
        //Instantiate(character,position, Quaternion.identity);
    }

    /// 
    /// Create Tile is responsible for instantiating tiles.  
    /// 
    /// What Tile type to spawn.
    /// The x coordinate to spawn tile at.
    /// The z coordinate to spawn tile at.
    /// What rotation to spawn the tile at.
    /// Dungeon width.
    /// Dungeon height.
    public void CreateTile(int tileType, int x, int z, float rotation, int dungeonWidth, int dungeonHeight)
    {
        //So it displays right-side up in the center of the scene
        x -= dungeonWidth/2;
        z -= dungeonHeight/2;

        //Offsetting for the tile size
        x *= 8;
        z *= -8;

        //Set up temporary object
        GameObject tempObject;

        //Get it's position
        Vector3 tempPos = new Vector3(x,0,z);

        bool sendSpawn = false;
    
        switch(tileType)
        {
            case -1:
                Debug.Log ("NONE FOUND " + tempPos);
                tempObject = (GameObject)GameObject.Instantiate(Resources.Load("Error"));
                break;
            case (int)Enums.TileType.Corner:
                tempObject = (GameObject)GameObject.Instantiate(Resources.Load("Corner"));
                break;
            case (int)Enums.TileType.OneWall:
                tempObject = (GameObject)GameObject.Instantiate(Resources.Load("OneWall"));
                break;
            case (int)Enums.TileType.Doorway:
                tempObject = (GameObject)GameObject.Instantiate(Resources.Load("Doorway"));
                break;
            case (int)Enums.TileType.FloorAndRoof:
                tempObject =(GameObject)GameObject.Instantiate(Resources.Load("FloorAndRoof"));
            if(team1 == false)
                {
                    //character.transform.position = new Vector3(x, charYOffset,z);
                    //GameObject.Find"		
                    team1Spawn = new Vector3(x, charYOffset, z);		
                    team1 = true;
                    sendSpawn = true;
                    Debug.Log ("SETTING SPAWN");
                }
                break;
        case (int)Enums.TileType.Corridor:
            tempObject =(GameObject)GameObject.Instantiate(Resources.Load("Corridor"));
            break;
        default:
            tempObject = (GameObject)GameObject.Instantiate(Resources.Load ("Error"));
            Debug.Log ("DEFAULT " + tempPos);
            break;
        }

        tempObject.transform.Rotate (new Vector3(0f, rotation, 0f));
        tempObject.transform.position = tempPos;


        //TESTING
        networkView.RPC ("PushTile", RPCMode.AllBuffered, tempObject, tempPos, rotation);

        //GameObject.Destroy (tempObject);
    }

    [RPC]
    void PushTile(GameObject tileObject, Vector3 tilePosition, float rotation)
    {
        Debug.Log ("Rawr");
        GameObject objectCopy = tileObject;
        Quaternion newRotation = Quaternion.Euler(0f, rotation, 0f);
        Network.Instantiate(objectCopy, tilePosition, newRotation, 0);
    }

    /// 
    /// Determins which direction a tile should face, as a corner.
    /// 
    /// The tile to the left of the target tile.
    /// The tile to the right of the target tile.
    /// The tile ontop of the target tile
    /// The tile below the target tile
    /// The x coordinate of the target tile.
    /// The z coordinate of the target tile.
    /// Dungeon width.
    /// Dungeon height.
    public void CheckCorners (int leftTile, int rightTile, int topTile, int bottomTile, int x, int z, int dungeonWidth, int dungeonHeight)
    {
        //Left And Top Empty = NW
        //Left and Bottom Empty = SW
        //Right and Top Empty = NE
        //Right and Bottom Empty = NW
    
        if(leftTile == (int)Enums.TileType.Empty)
        {
            if(topTile == (int)Enums.TileType.Empty)
                CreateTile((int)Enums.TileType.Corner,x,z,(float)Enums.CornerDirections.NorthWest, dungeonWidth, dungeonHeight);
    
            else if(bottomTile == (int)Enums.TileType.Empty)
                CreateTile((int)Enums.TileType.Corner,x,z,(float)Enums.CornerDirections.SouthWest, dungeonWidth, dungeonHeight);
    
            else
                CreateTile(-1,x,z,0, dungeonWidth, dungeonHeight);
        }
        else if(rightTile == (int)Enums.TileType.Empty)
        {
            if(topTile == (int)Enums.TileType.Empty)
                CreateTile((int)Enums.TileType.Corner,x,z,(float)Enums.CornerDirections.NorthEast, dungeonWidth, dungeonHeight);

            else if(bottomTile == (int)Enums.TileType.Empty)
                CreateTile((int)Enums.TileType.Corner,x,z,(float)Enums.CornerDirections.SouthEast, dungeonWidth, dungeonHeight);

            else
                CreateTile(-1,x,z,0, dungeonWidth, dungeonHeight);				
        }
    else
        CreateTile(-1,x,z,0, dungeonWidth, dungeonHeight);
    }

        /// 
        /// Determins which direction a tile should face, as a single wall.
        /// 
        /// The tile to the left of the target tile.
        /// The tile to the right of the target tile.
        /// The tile ontop of the target tile
        /// The tile below the target tile
        /// The x coordinate of the target tile.
        /// The z coordinate of the target tile.
        /// Dungeon width.
        /// Dungeon height.
        public void CheckOneWalls(int leftTile, int rightTile, int topTile, int bottomTile, int x, int z, int dungeonWidth, int dungeonHeight)
        {		
            if(bottomTile == (int)Enums.TileType.FloorAndRoof && topTile == (int)Enums.TileType.Empty)
                CreateTile((int)Enums.TileType.OneWall, x,z,(float)Enums.OneWallDirections.North, dungeonWidth, dungeonHeight);

            else if(leftTile == (int)Enums.TileType.FloorAndRoof && rightTile == (int)Enums.TileType.Empty)
                CreateTile((int)Enums.TileType.OneWall, x,z,(float)Enums.OneWallDirections.East, dungeonWidth, dungeonHeight);

            else if(rightTile == (int)Enums.TileType.FloorAndRoof && leftTile == (int)Enums.TileType.Empty)
                CreateTile((int)Enums.TileType.OneWall, x,z,(float)Enums.OneWallDirections.West, dungeonWidth, dungeonHeight);

            else if(topTile == (int)Enums.TileType.FloorAndRoof && bottomTile == (int)Enums.TileType.Empty)
                CreateTile((int)Enums.TileType.OneWall, x,z,(float)Enums.OneWallDirections.South, dungeonWidth, dungeonHeight);

			else if(bottomTile == (int)Enums.TileType.Corridor)
                CreateTile((int)Enums.TileType.Doorway, x,z,(float)Enums.DoorwayDirections.South, dungeonWidth, dungeonHeight);

            else if(topTile == (int)Enums.TileType.Corridor)
                CreateTile((int)Enums.TileType.Doorway, x,z,(float)Enums.DoorwayDirections.North, dungeonWidth, dungeonHeight);

            else if(leftTile == (int)Enums.TileType.Corridor)
                CreateTile((int)Enums.TileType.Doorway, x,z,(float)Enums.DoorwayDirections.West, dungeonWidth, dungeonHeight);

            else if(rightTile == (int)Enums.TileType.Corridor)
                CreateTile((int)Enums.TileType.Doorway, x,z,(float)Enums.DoorwayDirections.East, dungeonWidth, dungeonHeight);

            else
                CreateTile(-1, x,z,0, dungeonWidth, dungeonHeight);
    }

    /// 
    /// Determins which direction a tile should face, as a corridor.
    /// 
    /// The tile to the left of the target tile.
    /// The tile to the right of the target tile.
    /// The tile ontop of the target tile
    /// The tile below the target tile
        /// The x coordinate of the target tile.
    /// The z coordinate of the target tile.
    /// Dungeon width.
    /// Dungeon height.
    public void CheckCorridors(int leftTile, int rightTile, int topTile, int bottomTile, int x, int z, int dungeonWidth, int dungeonHeight)
    {
        //We are a N/S corridor IF our bottom tile is a...
        //Doorway, corridor, or a single wall (because sometimes we are checked before walls have become doorways)
        if(bottomTile == (int)Enums.TileType.Doorway || bottomTile == (int)Enums.TileType.Corridor || bottomTile == (int)Enums.TileType.OneWall)
            CreateTile((int)Enums.TileType.Corridor, x,z,(float)Enums.CorridorDirections.NorthSouth, dungeonWidth, dungeonHeight);

        //We are a W/E corridor IF our bottom tile is a...
        //Doorway, corridor, or a single wall (because sometimes we are checked before walls have become doorways)
        else if(leftTile == (int)Enums.TileType.Doorway || leftTile == (int)Enums.TileType.Corridor || leftTile == (int)Enums.TileType.OneWall)
            CreateTile((int)Enums.TileType.Corridor, x,z,(float)Enums.CorridorDirections.WestEast, dungeonWidth, dungeonHeight);

		else
	        CreateTile(-1, x,z,0, dungeonWidth, dungeonHeight);
    }

    /// 
    /// Create Debug Dungeon is responible for creating a flat dungeon underneathe (-2 y) the real dungeon.
    /// This is for testing only and should not be included in the final product.
    /// 
    /// The x coordinate of the tile to create.
    /// The z coordinate of the tile to create.
    public void CreateDebugDungeon(int x, int z)
    {
        //So it displays right-side up in the center of the scene
        x -= dungeonWidth/2;
        z -= dungeonHeight/2;

        //Offsetting for the tile size
        x *= 8;
        z *= -8;

        GameObject tempObjectDebug = (GameObject)GameObject.Instantiate(Resources.Load("TestSubject"));
        tempObjectDebug.transform.position = new Vector3(x, -2, z);
    }
}



After all is said and done, the console will print:
GENERATING DUNGEON
SETTING DUNGEON DATA
SETTING SPAWN
Done pushing tiles


The character reference is set up correctly, the tiles are being instantiated correctly, everything seems to be working as intended, with the exception of the RPC call being ignored? There are no errors, no warnings.

I appreciate any help anyone can give,
Tiffany
Posted
Updated 5-Dec-14 11:00am
v2

1 solution

You must be sure to add a nertworkview on the gameobject of your script. You can add
[RequireComponent (typeof (NetworkView))]
at the start of your class to ensure unity add it by itself.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900