Author Topic: "Parameters do not match signature" on the AutoSync example (with no changes :S)  (Read 4679 times)

Code

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile


I have a problem with autosync >.<

I've tried following the video tutorial and made a scene with scripts that sends RFC calls for updates etc.... THAT WORKS!

But I would like autosync as well, as it would work well for rapid prototyping - except I can't get it to work! Not even when I make a whole new project and then import tnet again.

Here's what I do:

  • I start the server.
  • I add the AutoJoin example first in the build settings and then autosync
  • It connects to the server when I play and goes to the autosync scene, but only the first instance works
  • When I try it inside the editor, it gives me the error above
I have been searching and searching and I simply cannot figure it out!


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Thanks, here's a fix. Replace TNAutoSync with this:
  1. //---------------------------------------------
  2. //            Tasharen Network
  3. // Copyright © 2012-2015 Tasharen Entertainment
  4. //---------------------------------------------
  5.  
  6. using UnityEngine;
  7. using TNet;
  8. using System.Collections;
  9. using System.Reflection;
  10. using System.IO;
  11.  
  12. /// <summary>
  13. /// This script makes it really easy to sync some value across all connected clients.
  14. /// Keep in mind that this script should ideally only be used for rapid prototyping.
  15. /// It's still better to create custom to-the-point sync scripts as they will yield
  16. /// better performance.
  17. /// </summary>
  18.  
  19. [ExecuteInEditMode]
  20. public class TNAutoSync : TNBehaviour
  21. {
  22.         [System.Serializable]
  23.         public class SavedEntry
  24.         {
  25.                 public Component target;
  26.                 public string propertyName;
  27.         }
  28.  
  29.         /// <summary>
  30.         /// Serialized synchronized entries.
  31.         /// </summary>
  32.  
  33.         public System.Collections.Generic.List<SavedEntry> entries = new System.Collections.Generic.List<SavedEntry>();
  34.  
  35.         /// <summary>
  36.         /// Maximum possible number of updates per second. If the values don't change, nothing will be sent.
  37.         /// If to set it to zero, the value will only be synchronized when new players join.
  38.         /// </summary>
  39.  
  40.         public float updatesPerSecond = 10f;
  41.  
  42.         /// <summary>
  43.         /// Whether the result will be saved on the server or not. In most cases it should remain as 'true'.
  44.         /// In any case the values will be sent to newly joined players automatically.
  45.         /// </summary>
  46.  
  47.         public bool isSavedOnServer = true;
  48.  
  49.         /// <summary>
  50.         /// Whether only the object's owner can send sync messages. In most cases it should remain as 'true'.
  51.         /// </summary>
  52.  
  53.         public bool onlyOwnerCanSync = true;
  54.  
  55.         /// <summary>
  56.         /// Whether to send through UDP or TCP. If it's important, TCP will be used. If not, UDP.
  57.         /// If you have a lot of frequent updates, leave it as not important.
  58.         /// </summary>
  59.  
  60.         public bool isImportant = false;
  61.  
  62.         class ExtendedEntry : SavedEntry
  63.         {
  64.                 public FieldInfo field;
  65.                 public PropertyInfo property;
  66.                 public object lastValue;
  67.         }
  68.  
  69.         class Par : IBinarySerializable
  70.         {
  71.                 public object[] vals;
  72.  
  73.                 public void Serialize (BinaryWriter writer)
  74.                 {
  75.                         if (vals != null)
  76.                         {
  77.                                 int len = vals.Length;
  78.                                 writer.Write((byte)len);
  79.                                 for (int i = 0; i < len; ++i) writer.WriteObject(vals[i]);
  80.                         }
  81.                         else writer.Write((byte)0);
  82.                 }
  83.  
  84.                 public void Deserialize (BinaryReader reader)
  85.                 {
  86.                         int len = reader.ReadByte();
  87.  
  88.                         if (len != 0)
  89.                         {
  90.                                 if (vals == null || vals.Length != len) vals = new object[len];
  91.                                 for (int i = 0; i < len; ++i) vals[i] = reader.ReadObject();
  92.                         }
  93.                         else vals = null;
  94.                 }
  95.         }
  96.  
  97.         [System.NonSerialized] List<ExtendedEntry> mList = new List<ExtendedEntry>();
  98.         [System.NonSerialized] Par mCached = null;
  99.  
  100.         /// <summary>
  101.         /// Locate the property that we should be synchronizing.
  102.         /// </summary>
  103.  
  104.         void Awake ()
  105.         {
  106. #if UNITY_EDITOR
  107.                 if (!Application.isPlaying)
  108.                 {
  109.                         TNAutoSync[] tns = GetComponents<TNAutoSync>();
  110.  
  111.                         if (tns.Length > 1 && tns[0] != this)
  112.                         {
  113.                                 Debug.LogError("Can't have more than one " + GetType() + " per game object", gameObject);
  114.                                 DestroyImmediate(this);
  115.                         }
  116.                 }
  117.                 else
  118. #endif
  119.                 {
  120.                         // Find all properties, converting the saved list into the usable list of reflected properties
  121.                         for (int i = 0, imax = entries.Count; i < imax; ++i)
  122.                         {
  123.                                 SavedEntry ent = entries[i];
  124.                                
  125.                                 if (ent.target != null && !string.IsNullOrEmpty(ent.propertyName))
  126.                                 {
  127.                                         FieldInfo field = ent.target.GetType().GetField(ent.propertyName, BindingFlags.Instance | BindingFlags.Public);
  128.  
  129.                                         if (field != null)
  130.                                         {
  131.                                                 ExtendedEntry ext = new ExtendedEntry();
  132.                                                 ext.target = ent.target;
  133.                                                 ext.field = field;
  134.                                                 ext.lastValue = field.GetValue(ent.target);
  135.                                                 mList.Add(ext);
  136.                                                 continue;
  137.                                         }
  138.                                         else
  139.                                         {
  140.                                                 PropertyInfo pro = ent.target.GetType().GetProperty(ent.propertyName, BindingFlags.Instance | BindingFlags.Public);
  141.  
  142.                                                 if (pro != null)
  143.                                                 {
  144.                                                         ExtendedEntry ext = new ExtendedEntry();
  145.                                                         ext.target = ent.target;
  146.                                                         ext.property = pro;
  147.                                                         ext.lastValue = pro.GetValue(ent.target, null);
  148.                                                         mList.Add(ext);
  149.                                                         continue;
  150.                                                 }
  151.                                                 else Debug.LogError("Unable to find property: '" + ent.propertyName + "' on " + ent.target.GetType());
  152.                                         }
  153.                                 }
  154.                         }
  155.  
  156.                         if (mList.size > 0f)
  157.                         {
  158.                                 if (updatesPerSecond > 0f)
  159.                                         StartCoroutine(PeriodicSync());
  160.                         }
  161.                         else
  162.                         {
  163.                                 Debug.LogWarning("Nothing to sync", this);
  164.                                 enabled = false;
  165.                         }
  166.                 }
  167.         }
  168.  
  169.         /// <summary>
  170.         /// Sync periodically.
  171.         /// </summary>
  172.  
  173.         IEnumerator PeriodicSync ()
  174.         {
  175.                 for (; ; )
  176.                 {
  177.                         if (TNManager.isInChannel && updatesPerSecond > 0f)
  178.                         {
  179.                                 if (mList.size != 0 && (!onlyOwnerCanSync || tno.isMine) && Cache()) Sync();
  180.                                 yield return new WaitForSeconds(1f / updatesPerSecond);
  181.                         }
  182.                         else yield return new WaitForSeconds(0.1f);
  183.                 }
  184.         }
  185.  
  186.         /// <summary>
  187.         /// If this values are not saved on the server, at least send them to the newly joined player.
  188.         /// </summary>
  189.  
  190.         void OnNetworkPlayerJoin (Player p)
  191.         {
  192.                 if (mList.size != 0 && !isSavedOnServer && TNManager.isHosting)
  193.                 {
  194.                         if (Cache()) Sync();
  195.                         else tno.Send(255, p, mCached);
  196.                 }
  197.         }
  198.  
  199.         /// <summary>
  200.         /// Immediately cache all synchronized values and return whether something actually changed.
  201.         /// </summary>
  202.  
  203.         bool Cache ()
  204.         {
  205.                 bool initial = false;
  206.                 bool changed = false;
  207.  
  208.                 if (mCached == null)
  209.                 {
  210.                         initial = true;
  211.                         mCached = new Par() { vals = new object[mList.size] };
  212.                 }
  213.  
  214.                 for (int i = 0; i < mList.size; ++i)
  215.                 {
  216.                         ExtendedEntry ext = mList[i];
  217.  
  218.                         object val = (ext.field != null) ?
  219.                                 val = ext.field.GetValue(ext.target) :
  220.                                 val = ext.property.GetValue(ext.target, null);
  221.  
  222.                         if (!val.Equals(ext.lastValue))
  223.                                 changed = true;
  224.  
  225.                         if (initial || changed)
  226.                         {
  227.                                 ext.lastValue = val;
  228.                                 mCached.vals[i] = val;
  229.                         }
  230.                 }
  231.                 return changed;
  232.         }
  233.  
  234.         /// <summary>
  235.         /// Immediately synchronize all data by sending current values to everyone else.
  236.         /// </summary>
  237.  
  238.         public void Sync ()
  239.         {
  240.                 if (TNManager.isInChannel && mList.size != 0)
  241.                 {
  242.                         if (isImportant) tno.Send(255, isSavedOnServer ? Target.OthersSaved : Target.Others, mCached);
  243.                         else tno.SendQuickly(255, isSavedOnServer ? Target.OthersSaved : Target.Others, mCached);
  244.                 }
  245.         }
  246.  
  247.         /// <summary>
  248.         /// The actual synchronization function function.
  249.         /// </summary>
  250.  
  251.         [RFC(255)]
  252.         void OnSync (Par par)
  253.         {
  254.                 if (enabled)
  255.                 {
  256.                         int len = (par.vals != null) ? par.vals.Length : 0;
  257.  
  258.                         if (mList.size == len)
  259.                         {
  260.                                 for (int i = 0; i < len; ++i)
  261.                                 {
  262.                                         ExtendedEntry ext = mList[i];
  263.                                         ext.lastValue = par.vals[i];
  264.                                         if (ext.field != null) ext.field.SetValue(ext.target, ext.lastValue);
  265.                                         else ext.property.SetValue(ext.target, ext.lastValue, null);
  266.                                 }
  267.                         }
  268.                         else Debug.LogError("Mismatched number of parameters sent via TNAutoSync!");
  269.                 }
  270.         }
  271. }
  272.  

Code

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Awesome, it works :)
It is something that will be in the next tnet update?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Yup. It's already in the Pro version.