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

case

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #15 on: February 05, 2014, 10:06:50 PM »
Okay, I think I understand. Can the TNManager.objects list can be populated at runtime somehow? Thanks for the answers. :)
« Last Edit: February 06, 2014, 12:25:45 AM by case »

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 #16 on: February 06, 2014, 10:50:59 PM »
It shouldn't be populated at run-time, as it expects the list to match on all clients before the object instantiation begins.

If your list is dynamic, then just instantiate from the Resources folder by name.

mohydine

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #17 on: May 14, 2014, 01:13:42 PM »
Hello ArenMook,

Does the OnCreate Method require to be static?


Thanks

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 #18 on: May 15, 2014, 12:19:47 AM »
No, it doesn't. But in most cases you will want it to be.

danfoo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 93
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #19 on: August 29, 2014, 03:39:51 AM »
Hi,

I have a problem with custom RCCs. I call TNManager without error, but the RCC never fires.
The only difference compared to your examples is that the RCC function is not static, does it have to be?
  1. TNManager.CreateEx(10, true, patchPrefab, tid, coordSpan, TNManager.playerID);
  1. [TNet.RCC(10)]
  2.         public GameObject OnCreate (GameObject prefab, int mastertile, int[] coordspan, int reqPlayer) {....}

Thanks in advance!

Update:
CreateEx does fire, but since the passed gameobject was null (my mistake) it returns without an error (maybe it should at least log something).
Anyway, looking at the TNManager code it seems that if you pass a GO to the CreateEx function it assumes it is one of TNManagers "known" objects, and will not accept just any prefab. Correct?
I am assuming that passing a string path instead leads to it being loaded from Resources regardless of whether it is in the TNManager list or not?

TIA!
« Last Edit: August 29, 2014, 07:13:28 AM by danfoo »

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 #20 on: August 30, 2014, 09:53:41 AM »
Yup. It's generally best to pass a string in its place that you would normally pass to Resources.Load.

TNet handles prefab instantiation using its Resources.Load string instead of the prefab game object.

P.S. Also... the RCC should ideally be a static function.

viri

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #21 on: February 22, 2015, 09:02:56 PM »
It would be kinda nice to have immediate access to the gameobjects. Kindof like
  1. class SpawnerButton :MonoBehavior
  2. {
  3.          void OnMouseDown()
  4.          {
  5.                 GameObject go=TNManager.Create ("Token",Vector3.zero,Quaternion.identity,false);
  6.                 Random r=new Random();
  7.                 //yield new WaitForSeconds(1);
  8.                 go.GetComponent<Token>().SetName("Random name "+r.Next(100).ToString());
  9.          }
  10. }
  11.  

  1. public class Token:TNBehavior
  2. {
  3.         public void SetName(string newname)
  4.         {
  5.                 tno.Send ("SetNameRFC",Target.AllSaved,newname);
  6.         }
  7.         [RFC]
  8.         public void SetNameRFC(string newname)
  9.         {
  10.                 name=newname;
  11.         }
  12. }
  13.  

« Last Edit: February 22, 2015, 09:50:30 PM by viri »

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 #22 on: February 24, 2015, 12:03:01 PM »
That's impossible. Game objects are created by sending a request to the server, the server sending it back to everyone at the same time. It's a delayed call. Sending is done by specific object IDs which are generated by the server. They are not available immediately, so sending of immediate RFCs wouldn't be possible.

As this thread explains if you need to pass arguments along with your create call, create an RCC function and use it.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #23 on: July 02, 2015, 05:30:28 AM »
What's the syntax for passing a string reference to the RCC?

I can't get this or variations of it to work:
  1.     [RCC(10)]
  2.     static GameObject OnCreate (string prefabName, Vector3 pos, Quaternion rot, string unitName)
  3.     {
  4.         GameObject go = Instantiate(Resources.Load (prefabName) as GameObject);
  5.         go.transform.position = pos;
  6.         go.transform.rotation = rot;
  7.         go.name = unitName;
  8.         return go;
  9.     }
  10.  

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 #24 on: July 02, 2015, 09:27:43 PM »
I see nothing wrong with that. What's the actual error you're running into? Did you actually register your RCC(10)? Note TNManager.AddRCCs.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #25 on: July 03, 2015, 04:01:54 AM »
My script, GameControl:
  1.         void Awake(){
  2.                 Instance = this;
  3.                 TNManager.AddRCCs<GameControl>();
  4.                 ...
  5.         }
  6.  
  7.         TNManager.CreateEx ((byte)GLOBAL.RFCs.CREATE_ACTIONABLE, true, prefabName, (Vector3)pos, Quaternion.Euler (0,0,0));
  8.  
  9.         [RCC((byte)GLOBAL.RFCs.CREATE_ACTIONABLE)]
  10.         static GameObject CreateActionable (string prefabName, Vector3 pos, Quaternion rot){
  11.                 GameObject go = Instantiate(Resources.Load (prefabName) as GameObject);
  12.                 go.transform.position = pos;
  13.                 go.transform.rotation = rot;
  14.                 return go;
  15.         }
  16.  

Error:
  1. [TNet] Failed to call RFC #10 on <null>: failed to convert parameters
  2.   Expected args: System.String, UnityEngine.Vector3, UnityEngine.Quaternion
  3.   Received args: UnityEngine.GameObject, UnityEngine.Vector3, UnityEngine.Quaternion
  4.  
  5.   at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00063] in /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:192
  6.   at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115
  7.   at TNet.UnityTools.ExecuteFirst (TNet.List`1 rfcs, Byte funcID, System.Object& retVal, System.Object[] parameters) [0x0003e] in D:\_Programming Stuff\Unity\AiaVG\Assets\TNet\Client\TNUnityTools.cs:103
  8.  
  9. UnityEngine.Debug:LogError(Object, Object)
  10. TNet.UnityTools:PrintException(Exception, CachedFunc, Int32, String, Object[]) (at Assets/TNet/Client/TNUnityTools.cs:81)
  11. TNet.UnityTools:ExecuteFirst(List`1, Byte, Object&, Object[]) (at Assets/TNet/Client/TNUnityTools.cs:111)
  12. TNManager:CreateEx(Int32, Boolean, String, Object[]) (at Assets/TNet/Client/TNManager.cs:884)
  13. GameControl:DEV_AddObjective(String, TIME_TYPE, Int32, OBJECTIVE_TYPE, Int32, Vector2, Vector2, Vector2) (at Assets/_Scripts/GameControl.cs:430)
  14. GameControl:HOST_CreateLevel() (at Assets/_Scripts/GameControl.cs:412)
  15. <Start>c__Iterator33:MoveNext() (at Assets/_Scripts/GameControl.cs:163)

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 #26 on: July 03, 2015, 11:49:51 AM »
It's telling you what's wrong. You have "string prefabName", but it expects a Game Object. TNet finds the game object for you and passes a reference to the prefab for you. You don't need to Resources.Load. TNet does it for you.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #27 on: July 03, 2015, 12:08:25 PM »
If I pass the prefab as a GameObject, TNManager complains that the object isn't in its list.

I currently use Create("String") to instantiate from Resources. How do I do the same with CreateEx()?

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 #28 on: July 03, 2015, 12:13:06 PM »
TNManager.CreateEx needs the string. The RCC needs a GameObject.

TNet takes the string, does Resources.Load, then passes the game object to your RCC.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Custom Object Creation functions (1.8.0+)
« Reply #29 on: July 03, 2015, 02:12:10 PM »
Okay, thanks. To clarify, the syntax is exactly the same as the first post, only the call uses a string instead of a GameObject. The GameObject parameter of the RCC is created and passed via TNet. It's also only a reference - that object itself doesn't exist in the game and we need the Instantiation of it to create an in-game version.

It's a very simple, effective solution.