Code Base: GamerToGameDeveloper.com
Goal: Network Management of Instantiated Prefab with Player Name
Well Today was fun. I worked on expanding Looqmaan Ali's (aka GTGD) Code Base to allow me to track what player name created each construction block in the game. To do this, I had to pass the player name to the Server at the time of creating the blocks then pass the player name over to the actual blocks after they are created.
Since "ConstructionBlockAsksToRemoveItself.cs" is the only script attached to the construction block prefab, add your variable there:
Code:
//Variables Start_________________________________________________________ public bool iAmHit = false; // This is the variable I added: public string ObjectOwner;
Now, I will continue to work backwards and open "ServerCreatesConstructionBlock.cs", since this is where the prefab gets instantiated as a gameObject. Go to the bottom of the script where the "CreateConstructionBlock" method is located and modify it like this:
Code:
void CreateConstructionBlock (Vector3 pos, Quaternion rot, string blockNme, string blockOwnr) // add ", string blockOwnr" here { //Assign the unique name to the created block //across all game instances. block.name = blockNme; //Add these two lines below: ConstructionBlockAsksToRemoveItself script = block.transform.GetComponent<ConstructionBlockAsksToRemoveItself>(); script.ObjectOwner = blockOwnr;
Now look further up in the "ServerCreatesConstructionBlock.cs" script and in the update call change this:
Code:
networkView.RPC("CreateConstructionBlock", RPCMode.AllBuffered, dropPosition, dropRotation, blockName);
to this:
Code:
networkView.RPC("CreateConstructionBlock", RPCMode.AllBuffered, dropPosition, dropRotation, blockName, blockOwner);
Now further up to where the variables are declared add this:
Code:
public string blockOwner;
Now let's continue going backwards and open "PlayerAsksServerToPlaceConstructionBlock.cs" and near the bottom of the script modify the AskServerToDropConstBlock call to look like this:
Code:
void AskServerToDropConstBlock (Vector3 pos, Quaternion rot,string BlockOwnr) // added ", string BlockOwnr" { //Find the BlockManager gameObject and assign the position and rotation values //to its script ServerCreatesConstructionBlock. ServerCreatesConstructionBlock script = go.GetComponent<ServerCreatesConstructionBlock>(); script.dropPosition = pos; script.dropRotation = rot; script.dropSignal = true; // add this variable setting here: script.blockOwner = BlockOwnr;
Now continue back up in "PlayerAsksServerToPlaceConstructionBlock.cs" and change BOTH instances of this line of code:
Code:
to look like this:
Code:
Then near the top of "PlayerAsksServerToPlaceConstructionBlock.cs" in the Start() function add this:
Code:
{ // Add this here:
The at the top of the script in the variable declarations area add this:
Code:
public string BlockOwner;
No comments:
Post a Comment