Support => TNet 3 Support => Topic started by: meta87 on April 09, 2013, 12:09:54 PM
Title: TNManager.Create and PoolManager
Post by: meta87 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:
public Transform SpawnNew(Vector3 pos, Quaternion rot)
{
...
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!
Title: Re: TNManager.Create and PoolManager
Post by: Cripple 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.
Title: Re: TNManager.Create and PoolManager
Post by: meta87 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:
var inst =(Transform)TNManager.Create(this.prefab, pos, rot);
or this:
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
Title: Re: TNManager.Create and PoolManager
Post by: Cripple on April 10, 2013, 02:41:41 AM
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
var myObject =new GameObject();// We know that var is equivalent to GameObject here
But when you use it like this :
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.
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.
Title: Re: TNManager.Create and PoolManager
Post by: broknecho 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:
publicvoid SpawnNew(Vector3 pos, Quaternion rot)
{
...
TNManager.Create(this.prefab.gameObject, pos, rot);//assuming this.prefab is a Transform.
}
Then in each Start Method of your objects:
publicclass MyObject: TNBehaviour
{
void Start()
{
//This if could probably be removed if you need each client to register their own version of the object.
if(tno.isMine)
{
//Register myself with something if needed. Like:
PoolManager.RegisterNewObject(this);
}
}
}
Title: Re: TNManager.Create and PoolManager
Post by: meta87 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.