Author Topic: TNManager.Create and PoolManager  (Read 3871 times)

meta87

  • Guest
TNManager.Create and PoolManager
« on: April 09, 2013, 12:09:54 PM »
Firstly let me say that TNet is looking great. I'm just starting to try and incorporate it into a platformer game I'm working on. I'm currently using PoolManager to spawn pretty much everything that would need TNManager.Create. Here is the code I'm assuming needs to be changed in poolmanager:

  1. public Transform SpawnNew(Vector3 pos, Quaternion rot)
  2. {
  3. ...
  4. var inst = (Transform)Object.Instantiate(this.prefab, pos, rot);

TNManager.create wants a gameObject as the first variable while poolmanager is using a Transform. Can this be changed to tnmanager.create easily you think? Thanks in advance!

Cripple

  • TNP Alpha
  • Full Member
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 117
    • View Profile
Re: TNManager.Create and PoolManager
« Reply #1 on: April 09, 2013, 03:02:01 PM »
A transform as a link to its GameObject and a GameObject as a link to a transform, I see no problem here.
Graphicstream Dev.

meta87

  • Guest
Re: TNManager.Create and PoolManager
« Reply #2 on: April 09, 2013, 07:57:13 PM »
Thanks for the reply Cripple. My c# skills aren't great, I've been developing in Unity script...

So that being said maybe I'm missing something easy, but I get a slew of errors when I just change the line to:

  1. var inst = (Transform)TNManager.Create(this.prefab, pos, rot);

or this:

  1. var inst = TNManager.Create(this.prefab, pos, rot);

The "this.prefab" is a cached transform for the prefab that will spawn and it doesn't seem like it would be trivial
to change the way it works. Maybe I could change TNManager.Create to expect a transform? I'm going to look into that.
Let me know if I'm missing something :D

Cripple

  • TNP Alpha
  • Full Member
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 117
    • View Profile
Re: TNManager.Create and PoolManager
« Reply #3 on: April 10, 2013, 02:41:41 AM »
Try this.

  1. GameObject inst = TNManager.Create(this.prefab.gameObject, pos, rot);
  2. Transform instTranform = inst.transform;

You can access to the transform of any GameObject using 'go.transform', and access to the gameObject of a transform using 'tr.gameObject'. This is true because a GameObject has always a transform and a transform is always assigned to a GameObject.

New tip for c#, please avoid the use of the keyword var, it is a keyword 'script like' where you don't have to specify the type of a object when it is explicit such as
  1. var myObject = new GameObject(); // We know that var is equivalent to GameObject here
But when you use it like this :
  1. var inst = TNManager.Create(this.prefab, pos, rot); // We don't know the return type of the TNManager.Create(...) function, I can't guess the type for inst.
  2. GameObject inst = TNManager.Create(this.prefab, pos, rot); // This is better, we know the type.

I prefer to avoid this keyword and never use it because it is easier to read a code where every variable is typed.
Graphicstream Dev.

broknecho

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 33
    • View Profile
Re: TNManager.Create and PoolManager
« Reply #4 on: April 10, 2013, 11:21:26 AM »
This pattern won't work for you anymore due to TNManager.Create not actually give you a GameObject reference back.  TNManager.Create has a void return type and defers the creation to the network code. 

The new Pattern is to do something with the Object in it's Start Method.  So when your poolmanager creates an object, it's done at that point.  You then have to go into the Objects Start method and do something there.  In my game, I am using the start method of my game object to register itself with whatever it needs to.

So your poolmanager should be:

  1. public void SpawnNew(Vector3 pos, Quaternion rot)
  2. {
  3.     ...
  4.     TNManager.Create(this.prefab.gameObject, pos, rot); //assuming this.prefab is a Transform.
  5. }
  6.  
  7.  

Then in each Start Method of your objects:
  1. public class MyObject: TNBehaviour
  2. {
  3.  
  4.     void Start()
  5.     {
  6.         //This if could probably be removed if you need each client to register their own version of the object.
  7.         if(tno.isMine)
  8.         {
  9.             //Register myself with something if needed.  Like:
  10.             PoolManager.RegisterNewObject(this);
  11.         }
  12.     }
  13. }
  14.  

meta87

  • Guest
Re: TNManager.Create and PoolManager
« Reply #5 on: April 10, 2013, 09:52:49 PM »
Appreciate the help from you both. Going to try and get it working like you propose broknecho.