Author Topic: Custom Object Creation functions (TNet 2)  (Read 26020 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #30 on: July 03, 2015, 02:13:17 PM »
That's correct. TNet gives you a reference to the loaded prefab that you will then instantiate.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #31 on: July 11, 2015, 10:57:55 AM »
I'm finding calls after CreateEx are executing before the CreateEx is complete. Is there a callback or something to get when a CreateEx is complete? Or should I implement a handshake via RFCs?

Edit: Sorry, complete nonsense on my part. Can't remember the fault now but my interpretation of the situation was wrong. It was some dumb implementation error on my end!
« Last Edit: July 24, 2015, 08:21:51 AM by Shifty Geezer »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #32 on: July 12, 2015, 08:11:23 PM »
What calls? When you instantiate an object, its Awake() and OnEnable() will trigger before the creation completes. Is this what you're seeing? If so, that's just how Unity works.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #33 on: July 15, 2015, 12:51:18 AM »
You shouldn't be doing any RFCs in Start() of a created object. You can't send RFCs while the channel is being joined, and joining operation can stretch across several frames, meaning your Start() will most likely execute before you finish joining the channel.

This is why I yield in all my Start() calls like so:
  1. IEnumerator Start()
  2. {
  3.     while (TNManager.isJoiningChannel) yield return null;
  4.     // .. do stuff
  5. }

blueagardq15

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #34 on: July 24, 2015, 10:29:58 AM »
Does the script have to be attached to TNManager?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #35 on: July 24, 2015, 11:00:25 AM »
No, just be sure to register it with TNManager in your script's Awake(), and make sure that this script exists as a part of your scene.
  1. TNManager.AddRCCs<YourScript>();

Doomlazy

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 1
  • Posts: 28
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #36 on: October 20, 2015, 08:29:28 AM »
Is there any easier way of Instantiating a player.
I tried using TNManager.Create today and got the error:
"[TNet] Failed to call RCC #1. Did you forget to register it in Awake() via TNManager.AddRCCs?"
Do I seriously have to set all this stuff up just to instantiate a player?
In Unity all I would need to do is Network.Instantiate and that worked fine for me.
Is there an easier way of instantiating gameobjects Unity-style?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #37 on: October 24, 2015, 05:17:07 AM »
RCC #1 is a part of TNet. It's bound to OnCreate1 function in TNManager. It doesn't need to be registered because it gets registered automatically on line 951 of TNManager.cs:
  1.                                 // Add the built-in remote creation calls
  2.                                 AddRCCs<TNManager>();
TNet also automatically registers any RCCs it finds on scripts underneath its instance object (assuming you have a TNManager script present in the scene rather than having it instantiate itself dynamically). The rest you need to register in Awake() when first starting up your application.

This is only for custom RCCs, mind you -- that is RCCs that involve passing custom data during the creation.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #38 on: January 28, 2016, 03:06:42 PM »
As of TNet 3, it's no longer necessary to call TNManager.AddRCCs<T>(), and the syntax of RCCs changed. They can be called by name, just like RFCs (the ID is no longer needed). Instantiation is done via TNManager.Instantiate, or TNBehaviour's Instantiate() functions (there for convenience).

Examining the ExampleCreate.cs script shows both steps needed -- creating the RFC and instantiating an object. As such, this topic is now obsolete.
  1. using UnityEngine;
  2. using TNet;
  3.  
  4. public class ExampleCreate : MonoBehaviour
  5. {
  6.         /// <summary>
  7.         /// Create a new object above the clicked position
  8.         /// </summary>
  9.  
  10.         void OnClick ()
  11.         {
  12.                 // Object's position will be up in the air so that it can fall down
  13.                 Vector3 pos = TouchHandler.worldPos + Vector3.up * 3f;
  14.  
  15.                 // Object's rotation is completely random
  16.                 Quaternion rot = Quaternion.Euler(Random.value * 180f, Random.value * 180f, Random.value * 180f);
  17.  
  18.                 // Object's color is completely random
  19.                 Color color = new Color(Random.value, Random.value, Random.value, 1f);
  20.  
  21.                 // Create the object using a custom creation function defined below
  22.                 TNManager.Instantiate("ColoredObject", "Created Cube", true, pos, rot, color);
  23.         }
  24.  
  25.         /// <summary>
  26.         /// RCCs (Remote Creation Calls) allow you to pass arbitrary amount of parameters to the object you are creating.
  27.         /// TNManager will call this function, passing a prefab to it that you should then instantiate.
  28.         /// </summary>
  29.  
  30.         [RCC]
  31.         static GameObject ColoredObject (GameObject prefab, Vector3 pos, Quaternion rot, Color c)
  32.         {
  33.                 // Instantiate the prefab
  34.                 GameObject go = Object.Instantiate(prefab) as GameObject;
  35.  
  36.                 // Set the position and rotation based on the passed values
  37.                 Transform t = go.transform;
  38.                 t.position = pos;
  39.                 t.rotation = rot;
  40.  
  41.                 // Set the renderer's color as well
  42.                 go.GetComponentInChildren<Renderer>().material.color = c;
  43.                 return go;
  44.         }
  45. }
« Last Edit: January 31, 2016, 06:55:43 AM by ArenMook »