Line 17: |
Line 17: |
| A 'net type' is any of several classes which Stardew Valley uses to sync data between players, named for the <code>Net</code> prefix in their name. A net type can represent a simple value like <tt>NetBool</tt>, or complex values like <tt>NetFieldDictionary</tt>. The game will regularly collect all the net fields reachable from <tt>Game1.netWorldState</tt> and sync them with other players. That means that many mod changes will be synchronised automatically in multiplayer. | | A 'net type' is any of several classes which Stardew Valley uses to sync data between players, named for the <code>Net</code> prefix in their name. A net type can represent a simple value like <tt>NetBool</tt>, or complex values like <tt>NetFieldDictionary</tt>. The game will regularly collect all the net fields reachable from <tt>Game1.netWorldState</tt> and sync them with other players. That means that many mod changes will be synchronised automatically in multiplayer. |
| | | |
− | Net fields can implicitly convert to their underlying value type (like <code>bool x = new NetBool(true)</code>), but their conversion rules can be counterintuitive and error-prone. For example, <code>item?.category == null && item?.category != null</code> can both be true at once. '''Always avoid implicit casts to minimise bugs.''' Instead, access the underlying value using <tt>.Value</tt> (or <tt>.Pairs</tt> on a net dictionary):
| + | Although net fields can be implicitly converted to an equivalent value type (like <code>bool x = new NetBool(true)</code>), their conversion rules are counterintuitive and error-prone (e.g. <code>item?.category == null && item?.category != null</code> can both be true at once). Never implicitly case net fields to minimise bugs; instead, access the underlying value directly. Here's how to use some common net types: |
− | : <source lang="C#">
| + | {| class="wikitable" |
− | NetString str = new NetString("bar");
| + | |- |
− | if (str.Value == "bar") // true
| + | ! net type |
− | </source> | + | ! description |
| + | |- |
| + | | <tt>NetBool</tt><br /><tt>NetColor</tt><br /><tt>NetFloat</tt><br /><tt>NetInt</tt><br /><tt>NetPoint</tt><br /><tt>NetString</tt> |
| + | | A simple synchronised value. Access the value using <tt>field.Value</tt>. |
| + | |- |
| + | | <tt>NetCollection<T></tt><br /><tt>NetList<T></tt><br /><tt>NetObjectList<T></tt> |
| + | | A list of <tt>T</tt> values. This implements the standard interfaces like [https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1 <tt>IEnumerable<T></tt>] and [https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ilist-1 <tt>IList<T></tt>], so you can iterate it directly like <code>foreach (T value in field)</code>. |
| + | |- |
| + | | <tt>NetPointDictionary<TValue, TNetValue></tt> |
| + | | Maps <tt>Point</tt> keys to instances of <tt>TValue</tt> (the underlying value type) and <tt>TNetValue</tt> (the synchronised net type). You can iterate key/value pairs like <code>foreach (KeyValuePair<Point, TValue> pair in field.Pairs)</code>. |
| + | |- |
| + | | <tt>NetVector2Dictionary<TValue, TNetValue></tt> |
| + | | Maps <tt>Vector2</tt> keys to instances of <tt>TValue</tt> (the underlying value type) and <tt>TNetValue</tt> (the synchronised net type). You can iterate key/value pairs like <code>foreach (KeyValuePair<Vector2, TValue> pair in field.Pairs)</code>. |
| + | |} |
| | | |
| The build config NuGet package should detect most implicit conversions and show an appropriate build warning. | | The build config NuGet package should detect most implicit conversions and show an appropriate build warning. |