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

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Custom Object Creation functions (TNet 2)
« on: August 15, 2013, 12:36:08 PM »
Let's face it -- sometimes the generic TNManager.Create functions aren't enough. Sometimes it's useful to be able to pass custom data along. For example, creating a unit and wanting to assign its team. With Unity's objects it's easy -- Instantiate returns a game object that you can then modify, but with the creation having to go through proper synchronization, things get... tricky.

Well, TNet 1.8.0 aims to change that.

Adding a custom creation callback is now as simple as adding a new RFC call. In fact, the syntax is very similar as well.

The first thing you will want to do is to have a MonoBehaviour script attached to the same game object as your TNManager, or one of its children. This way TNManager will be able to find the function you are about to add. If you have a Game Manager, I'd suggest you add the following code there.

If you don't want to add the script under your TNManager, then you will need to register it with the TNManager by calling TNManager.AddRCCs<YourScriptType>();. TNet will check the script and add all of its RCCs automatically.

Next, add the actual RCC (remote creation call) function to your script. To do that, simply create a prototype for it. The function prototype for it is: [RCC(#)] GameObject FunctionName (GameObject prefab, ...); -- where "#" is the ID (10 to 255 range, inclusive), and "..." can be your custom parameters -- whatever you want them to be. For example, here's a function that expects a name to be passed along with position and rotation:
  1.         [RCC(10)]
  2.         static GameObject OnCreate (GameObject prefab, Vector3 pos, Quaternion rot, string unitName)
  3.         {
  4.                 GameObject go = Instantiate(prefab, pos, rot) as GameObject;
  5.                 go.name = unitName;
  6.                 return go;
  7.         }
Note that the function then returns the game object you've created so that TNet can do some other work with it (namely setting the TNObject IDs, if needed).

The last step is to call the new TNManager.CreateEx function somewhere. You can call it directly if you wish, but it's probably easier to create a wrapper function for it next to your RCC call and so that it's easy to find later:
  1.         static public void Create (GameObject prefab, Vector3 pos, Quaternion rot, string unitName, bool persistent = true)
  2.         {
  3.                 TNManager.CreateEx(10, persistent, prefab, pos, rot, unitName);
  4.         }
Note that the first 2 parameters passed to TNManager.CreateEx must be the ID of your RCC function and the persistent flag (whether the object will remain behind if the player leaves). The following parameters should match your RCC function's parameters.

As you probably guessed, calling your custom GameUnitManager.Create(...) function will indeed create your object as if you called TNManager.Create, except that your custom function will be doing the work -- including setting the name of the object.

Here is the complete script, for your convenience:
  1. using UnityEngine;
  2. using TNet;
  3.  
  4. public class GameUnitManager : MonoBehaviour
  5. {
  6.         /// <summary>
  7.         /// Step 1: Use this function instead of TNManager.Create when you want to create a new unit.
  8.         /// </summary>
  9.  
  10.         static public void Create (GameObject prefab, Vector3 pos, Quaternion rot, string unitName, bool persistent = true)
  11.         {
  12.                 TNManager.CreateEx(10, persistent, prefab, pos, rot, unitName);
  13.         }
  14.  
  15.         /// <summary>
  16.         /// Step 2: Create a function that will be called when the unit creation packet arrives.
  17.         /// Make sure that this script is attached to the same game object as TNManager or one of its children,
  18.         /// or that you registered this MonoBehaviour by calling TNManager.AddRCCs!
  19.         /// </summary>
  20.  
  21.         [RCC(10)]
  22.         static GameObject OnCreate (GameObject prefab, Vector3 pos, Quaternion rot, string unitName)
  23.         {
  24.                 GameObject go = Instantiate(prefab, pos, rot) as GameObject;
  25.                 go.name = unitName;
  26.                 return go;
  27.         }
  28. }
« Last Edit: February 15, 2016, 02:35:20 PM by ArenMook »

hippocoder

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #1 on: August 15, 2013, 07:07:08 PM »
Hiya, I meant to purchase over the weekend but got too busy. I saw you are mentioning a pro version now, do I buy that instead? is there a link to the new version?

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 #2 on: August 16, 2013, 12:10:19 AM »
Pro version is just $30 more right now ($95 total). Like NGUI Pro it comes with access to the git-based source code repository. Two ways of getting it right now -- just with a direct PayPal of $95 to support at tasharen.com (for a brand-new license), or upgrade of $30 to the same address, with an email mentioning your OR# on the Asset Store for TNet standard.

hippocoder

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #3 on: August 16, 2013, 07:05:40 AM »
May I know what the differences between standard and pro are? I was unable to find the pro version on unity asset store.

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 #4 on: August 16, 2013, 07:14:14 AM »
It's the same as NGUI -- Pro comes in the form of original source code repository access which gives you access to the latest changes sooner than you would otherwise, the ability to submit your own patches, and just overall much easier way to stay up to date if you plan on changing the library.

You won't find it on the Asset Store due to its delivery method (repo access).

pyscho2day

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 74
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #5 on: August 17, 2013, 10:03:18 PM »
Aren,

Will standard get the same functionality as pro at some point but with out the repo access?

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 #6 on: August 18, 2013, 06:35:31 AM »
Yes, of course. Just wait a bit and when I ensure that there are no issues with it, it will be pushed to the Asset Store.

pyscho2day

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 74
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #7 on: August 19, 2013, 10:52:39 AM »
OK, I figured as much but wanted to make sure.

jn5414

  • Guest
Re: Custom Object Creation functions (1.8.0+)
« Reply #8 on: September 12, 2013, 04:40:58 AM »
can rcc be used depart from 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 #9 on: September 12, 2013, 07:58:57 AM »
RCC functions must be on a script that's attached either to the same object as the TNManager or one of its children.

andrew

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 44
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #10 on: October 10, 2013, 09:59:49 PM »
Nice stuff...

Say I want to launch off a projectile with this code... and I want to pass in the GameObject owner of the projectile so that I can turn off collision between the owner and the projectile.  I can't serialize a GameObject for this, how would you go about doing that?

thanks

andrew

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 44
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #11 on: October 10, 2013, 11:04:25 PM »
Think I answered my own question.. keep a static list around and update it when creating/removing players using tno.ownerID for lookup.

  1. public class PlayerID {
  2.         public PlayerID( GameObject _go, int _id ) {
  3.                 go = _go;
  4.                 id = _id;
  5.         }
  6.         public GameObject       go;
  7.         public int                      id;
  8. }
  9. private static BetterList<PlayerID> players = new BetterList<PlayerID>();
  10.        
  11. public static GameObject Find( int objID ) {
  12.         for ( int i = 0; i < players.size; i++ ) {
  13.                 if ( players[i].id == objID ) {
  14.                         return players[i].go;
  15.                 }
  16.         }
  17.         return null;
  18. }
  19.  

pyscho2day

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 74
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #12 on: October 11, 2013, 07:55:59 AM »
This is how i do it

  1. [RCC(10)]      
  2. static GameObject myMethod(GameObject prefab, Vector3 pos, Quaternion rot, string someStringValue, int someIntValue)
  3. {
  4.         GameObject go = Instantiate(prefab, pos, rot) as GameObject;    //creates the object
  5.         go.name = someStringValue;                                     //Names the object
  6.         ScriptName sn = go.GetComponent<ScriptName>();    //Gets the script you want to add info to
  7.         sn.property    //Add info to the script such as name or team.
  8.         return go;
  9. }

case

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #13 on: February 05, 2014, 02:18:22 PM »
First, this is a great package, I've used a lot of Tasharen's sets from the space game, NGUI, and now TNET. However, I wasn't sure why TNManager requires a list of object prefabs set for creation. Most of the time this is handled by a scene/game manager, and I didn't want to have to redefine it in more than one place any time I make a change. Is it purely for the purpose of standalone servers?

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 #14 on: February 05, 2014, 05:01:19 PM »
It needs to know what the objects are. There are only two ways of doing this in Unity. You can either reference them directly, or you can Resources.Load them by name. TNet supports both -- so you don't need to add objects to TNManager's list if you want to create them by their prefab's name instead (assuming you placed them in the Resources folder, of course).