Changes

Jump to navigation Jump to search
Line 15: Line 15:     
===Net fields===
 
===Net fields===
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, and a 'net field' is any field or property of those types. They're named for the <code>Net</code> prefix in their type names. Net types can represent simple values 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.
   −
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:
+
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). To avoid bugs, never implicitly cast net fields; access the underlying value directly instead. The build config NuGet package should detect most implicit conversions and show an appropriate build warning.
 +
 
 +
Here's how to access the data in some common net types:
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
Line 32: Line 34:  
| Maps <tt>Long</tt>, <tt>Point</tt>, or <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<Long, TValue> pair in field.Pairs)</code> (replacing <tt>Long</tt> with <tt>Point</tt> or <tt>Vector2</tt> if needed).
 
| Maps <tt>Long</tt>, <tt>Point</tt>, or <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<Long, TValue> pair in field.Pairs)</code> (replacing <tt>Long</tt> with <tt>Point</tt> or <tt>Vector2</tt> if needed).
 
|}
 
|}
  −
The build config NuGet package should detect most implicit conversions and show an appropriate build warning.
      
==Main classes==
 
==Main classes==
translators
8,446

edits

Navigation menu