Author Topic: How get GameObject on TNManager.Instantiate  (Read 3307 times)

Elmo loves cookies

  • Jr. Member
  • **
  • Thank You
  • -Given: 60
  • -Receive: 1
  • Posts: 62
    • View Profile
How get GameObject on TNManager.Instantiate
« on: March 08, 2017, 01:21:31 PM »
when I create Player - I need write him in variable "private PlayerController localPlayer;"

  1. GameObject pers = TNManager.Instantiate("CreatePlayer", "Player", false, spawn.position, spawn.rotation);
  2.        
  3.         localPlayer = pers.GetComponent<PlayerController>();

but it is impossible because: void != GameObject

so, How I can put creation object in some variable?:)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How get GameObject on TNManager.Instantiate
« Reply #1 on: March 10, 2017, 11:56:09 AM »

Elmo loves cookies

  • Jr. Member
  • **
  • Thank You
  • -Given: 60
  • -Receive: 1
  • Posts: 62
    • View Profile
Re: How get GameObject on TNManager.Instantiate
« Reply #2 on: March 11, 2017, 04:33:08 AM »
I need write GameObject(after create) in variable, how can I do this?

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: How get GameObject on TNManager.Instantiate
« Reply #3 on: March 11, 2017, 07:44:26 AM »
The link ArenMook provided details object creation in TNet and the concept of RCC's. I'll regurgitate it here since you didn't seem to click it.

When you call TNManager.Instantiate your object isn't created instantly; instead, a "CreateObject" packet is constructed. This packet is sent across the network and arrives in each client's packet Queue. Packets are processed one by one from this Queue. When the "CreateObject" packet is processed, TNet first looks if there is an RCC function associated with the object: if there is an RCC function then it is called. In this RCC function you call Unity's GameObject.Instantiate method to create the GameObject and perform whatever processing you need to do with it, then you return this newly instantiated GameObject back to TNet (TNet needs to do additional work with the GameObject).

So, in this RCC is where you would store the GameObject in your variable. If this doesn't suit your needs then you should rethink your design. Could the code I gave you in your other thread be repurposed?

Elmo loves cookies

  • Jr. Member
  • **
  • Thank You
  • -Given: 60
  • -Receive: 1
  • Posts: 62
    • View Profile
Re: How get GameObject on TNManager.Instantiate
« Reply #4 on: March 11, 2017, 08:43:04 AM »
Maybe I cant translate to English my question, and you answer is not about this

For example,
On photon I can do that like this:

  1. GameObject LPObject = PhotonNetwork.Instantiate("Player" + model.ToString(), spawn.position, spawn.rotation, 0); //Spawning our Player
  2.         localPlayer = pers.GetComponent<Player>(); //this our localPlayer

How I can do this on TNet?

Elmo loves cookies

  • Jr. Member
  • **
  • Thank You
  • -Given: 60
  • -Receive: 1
  • Posts: 62
    • View Profile
Re: How get GameObject on TNManager.Instantiate
« Reply #5 on: March 11, 2017, 08:48:01 AM »
  1. GameObject LPObject = PhotonNetwork.Instantiate("Player" + model.ToString(), spawn.position, spawn.rotation, 0); //Spawning our Player
  2.         localPlayer = LPObject.GetComponent<Player>(); //this our localPlayer

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: How get GameObject on TNManager.Instantiate
« Reply #6 on: March 12, 2017, 09:52:38 AM »
I understood your question. My answer was accurate.
  1. [RCC(1)]
  2. static GameObject OnCreateObject(GameObject prefab)
  3. {
  4.         GameObject newObject = GameObject.Instantiate(prefab, Vector3.zero, Quaternion.identity);
  5.         myVariable = newObject;
  6.         return newObject;
  7. }
  8.  
  9. TNManager.Instantiate(1, "PathToPrefab", persistent);
  10.  

Alternatively:
  1. [RCC]
  2. static GameObject OnCreateObject(GameObject prefab, Vector3 pos, Quaternion rot)
  3. {
  4.         GameObject newObject = GameObject.Instantiate(prefab, pos, rot);
  5.         myVariable = newObject;
  6.         return newObject;
  7. }
  8.  
  9. TNManager.Instantiate("OnCreateObject", "PathToPrefab", persistent, Vector3.zero, Quaternion.identity);
  10.