Author Topic: TNManager.Create parenting  (Read 5405 times)

tenderpaw

  • Guest
TNManager.Create parenting
« on: April 29, 2013, 10:23:15 PM »
Hi

How can I add extra information on the created object via TNManager.Create like a parent?
Let's say this gameobject as the spawner that spawns monsters, I need to get all it's children so that when it dies and I need to destroy them also. Also for setting target for the spawned objects since the target is available on the parent.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TNManager.Create parenting
« Reply #1 on: April 29, 2013, 11:38:35 PM »
If you have a manager, then have that manager send RFC calls instead of using TNManager.Create.

If that's not desireable, add a new TNManager.Create function with all the parameters you need. Note this code:
  1. writer.Write((ushort)index);
  2. writer.Write(GetFlag(go, persistent));
  3. writer.Write((byte)2);
The (byte)2 you see at the end determines the type of creation this is. This value is read in TNManager.OnCreateObject. You will want to add a new identifier -- for example "10" -- in both your custom Create function, and OnCreateObject for parsing your custom data.

tenderpaw

  • Guest
Re: TNManager.Create parenting
« Reply #2 on: April 29, 2013, 11:57:01 PM »
Thanks for replying

For option 1 could you elaborate more on that, I mean I'm fine having a manager of some sort but as far as creating new object I only know TNManager.Create.

Option 2 as much as possible I want to avoid editing TNet itself because along the way it might pose some problems and also when TNet gets new update/feature it becomes messy.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TNManager.Create parenting
« Reply #3 on: April 30, 2013, 12:00:19 AM »
  1. tno.Send("CreateMyObject", Target.All, "Some Data");
  2.  
  3. [RFC]
  4. void CreateMyObject (string data)
  5. {
  6.     GameObject child = (GameObject)Object.Instantiate(somePrefab);
  7.     child.transform.parent = transform;
  8.     child.name = data;
  9.     <add it to the list of managed objects>
  10. }

tenderpaw

  • Guest
Re: TNManager.Create parenting
« Reply #4 on: April 30, 2013, 12:04:51 AM »
Thank you!, I'll look more into this.

tenderpaw

  • Guest
Re: TNManager.Create parenting
« Reply #5 on: April 30, 2013, 01:09:27 AM »
@    <add it to the list of managed objects>

Do you mean add it to TNManager.objects? I already did that.

As soon as the object is spawned I'm getting this error "Please make sure that a unique non-zero ID is given to all objects". Isn't that TNObject's Awake/Start handles the registration?
« Last Edit: April 30, 2013, 01:24:34 AM by tenderpaw »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TNManager.Create parenting
« Reply #6 on: April 30, 2013, 01:41:38 AM »
No, I mean a different list -- a list on your manager class.

You shouldn't have a TNObject script on the object you instantiate via Object.Instantiate. If you want to sync data related to this object, you have to do it via the manager.

tenderpaw

  • Guest
Re: TNManager.Create parenting
« Reply #7 on: April 30, 2013, 02:09:59 AM »
Thanks!

tenderpaw

  • Guest
Re: TNManager.Create parenting
« Reply #8 on: April 30, 2013, 10:56:59 AM »
Hey ArenMook I ran to another problem when trying to destroy the child object. My set-up is like this, Parent has TNObject component then spawns a child that has no TNObject as you've suggested then let's say at any point in time this child will hit other objects and when it does I need this object destroyed so on child's OnTriggerEnter method I call a function on it's parent like this

  1.         public void AddToDestroy(GameObject g)
  2.         {
  3.                
  4.                 objectToDestroy.Add(g);
  5.                 for(int i = (objectToDestroy.Count - 1); i >= 0; i--)
  6.                 {
  7.                         tnObject.Send(13,Target.AllSaved,i);
  8.                 }
  9.         }
  10.  
  11.         [RFC(13)]
  12.         void RFCDestroy(int i)
  13.         {
  14.                 Destroy(objectToDestroy[i]);
  15.         }

On the host the child gets destroyed but on the client does not.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TNManager.Create parenting
« Reply #9 on: April 30, 2013, 10:25:38 PM »
objectToDestroy.Add(g) only happens on the sender... and what's the point of objectToDestroy anyway? transform.childCount tells you the number of children, transform.GetChild(i) gets you the child. Careful using indices though. It's better to identify by some ID instead. When creating objects, pass some ID. When destroying, find the child by the same ID.

tenderpaw

  • Guest
Re: TNManager.Create parenting
« Reply #10 on: April 30, 2013, 11:06:03 PM »
At some point I would make periodic call on destroying objects that's why I have the objectToDestroy array so as not to send updates often when destroying objects. As for sending updates to all clients when destroying objects how do would you do it in my case? I was under the impression that with my RFCDestroy it will send to all client that the objectToDestroy(i) will get destroyed or do I need a globalManager for bullets to handle this?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TNManager.Create parenting
« Reply #11 on: April 30, 2013, 11:45:05 PM »
If you make it so that only the object's owner (tno.isMine) sends out updates / destroy calls, then you don't have to worry about marking the object as destroyed in some array. What I do in such cases is like so:

  1. void OnTriggerEnter ()
  2. {
  3.     if (tno.isMine)
  4.     {
  5.         tno.Send("DestroyNow", Target.Others);
  6.         DestroyNow();
  7.     }
  8. }
  9.  
  10. [RFC] void DestroyNow () { Destroy(gameObject); }
  11.  
The only difference in your case would be that your object destruction would be going through the manager, and you'd want to find the object to destroy by some ID first.

tenderpaw

  • Guest
Re: TNManager.Create parenting
« Reply #12 on: May 01, 2013, 02:04:59 AM »
Hey thanks for replying.

This is where I'm currently at but it still doesn't work  :'(.

On my Child
   
  1. void OnTriggerEnter(Collider collider)
  2.         {
  3.                 if(TNManager.isHosting)
  4.                 {
  5.                         parent.AddToDestroy(gameObject);
  6.                 }
  7.         }

Then on the parent
   
  1. public void AddToDestroy(GameObject g)
  2.     {
  3.         objectToDestroy.Add(g);
  4.         for(int i = (objectToDestroy.Count - 1); i >= 0; i--)
  5.         {
  6.             tnObject.Send(13,Target.AllSaved,i);
  7.         }
  8.     }
  9.  
  10.     [RFC(13)]
  11.     void RFCDestroy(int i)
  12.     {
  13.         Destroy(objectToDestroy[i]);
  14.     }

With the objectToDestroy I don't need to set-up an ID of some sort when destroying the object
« Last Edit: May 01, 2013, 03:02:41 AM by tenderpaw »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TNManager.Create parenting
« Reply #13 on: May 01, 2013, 03:27:29 AM »
You are using TNManager.isHosting rather than tno.isMine, and you are using a list that I suggested not using, so I stand by my previous post.

tenderpaw

  • Guest
Re: TNManager.Create parenting
« Reply #14 on: May 01, 2013, 03:52:09 AM »
Isn't you suggested to remove the TNObject on the bullet(child)? so tno.isMine won't work then I tried putting the TNObject.isMine on the parent but I still get the same result. I'm fine not using a list so instead I replace it with a "Public GameObject objectToDestroy" just to reference which object to destroy. Here's the updated code for the parent

  1. public void AddToDestroy(GameObject g)
  2.     {
  3.         if(tnObject.isMine)
  4.         {
  5.             objectToDestroy = g;
  6.             tnObject.Send(13,Target.Others);
  7.             RFCDestroy();
  8.         }
  9.     }
  10.  
  11.     [RFC(13)]
  12.     void RFCDestroy()
  13.     {
  14.         Destroy(objectToDestroy);
  15.     }