Changes

Jump to navigation Jump to search
split into 'general concepts' and 'multiplayer concepts' (to make room for future subsections about shadow locations, split-screen state, etc)
Line 3: Line 3:  
This page explains some of the Stardew Valley fundamentals that are useful for modders. See also [[Modding:Common tasks]].
 
This page explains some of the Stardew Valley fundamentals that are useful for modders. See also [[Modding:Common tasks]].
   −
==Concepts==
+
==General concepts==
 
===Time format===
 
===Time format===
 
The in-game time of day is tracked using a version of [[wikipedia:24-hour clock|24-hour format]] informally called "26-hour time", measured in 10-minute intervals. This is the format used by <samp>Game1.timeOfDay</samp> in a [[Modding:Modder Guide/Get Started|C# mod]] or <samp><nowiki>{{Time}}</nowiki></samp> in a [[Modding:Content Patcher|Content Patcher pack]].
 
The in-game time of day is tracked using a version of [[wikipedia:24-hour clock|24-hour format]] informally called "26-hour time", measured in 10-minute intervals. This is the format used by <samp>Game1.timeOfDay</samp> in a [[Modding:Modder Guide/Get Started|C# mod]] or <samp><nowiki>{{Time}}</nowiki></samp> in a [[Modding:Content Patcher|Content Patcher pack]].
Line 79: Line 79:  
| tile || → || screen
 
| tile || → || screen
 
| <code>(x * Game1.tileSize) - Game1.viewport.X, (y * Game1.tileSize) - Game1.viewport.Y</code>
 
| <code>(x * Game1.tileSize) - Game1.viewport.X, (y * Game1.tileSize) - Game1.viewport.Y</code>
|}
  −
  −
===Net fields===
  −
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 <samp>NetBool</samp>, or complex values like <samp>NetFieldDictionary</samp>. The game will regularly collect all the net fields reachable from <samp>Game1.netWorldState</samp> 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). 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"
  −
|-
  −
! net type
  −
! description
  −
|-
  −
| <samp>NetBool</samp><br /><samp>NetColor</samp><br /><samp>NetFloat</samp><br /><samp>NetInt</samp><br /><samp>NetPoint</samp><br /><samp>NetString</samp>
  −
| A simple synchronised value. Access the value using <samp>field.Value</samp>.
  −
|-
  −
| <samp>NetCollection&lt;T&gt;</samp><br /><samp>NetList&lt;T&gt;</samp><br /><samp>NetObjectList&lt;T&gt;</samp>
  −
| A list of <samp>T</samp> values. This implements the standard interfaces like [https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1 <samp>IEnumerable&lt;T&gt;</samp>] and [https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ilist-1 <samp>IList&lt;T&gt;</samp>], so you can iterate it directly like <code>foreach (T value in field)</code>.
  −
|-
  −
| <samp>NetLongDictionary&lt;TValue, TNetValue&gt;</samp><br /><samp>NetPointDictionary&lt;TValue, TNetValue&gt;</samp><br /><samp>NetVector2Dictionary&lt;TValue, TNetValue&gt;</samp>
  −
| Maps <samp>Long</samp>, <samp>Point</samp>, or <samp>Vector2</samp> keys to instances of <samp>TValue</samp> (the underlying value type) and <samp>TNetValue</samp> (the synchronised net type). You can iterate key/value pairs like <code>foreach (KeyValuePair<Long, TValue> pair in field.Pairs)</code> (replacing <samp>Long</samp> with <samp>Point</samp> or <samp>Vector2</samp> if needed).
   
|}
 
|}
   Line 167: Line 146:     
: You can test whether your mod accounts for this correctly by setting the zoom to maximum and the UI scale to minimum (''i.e.,'' have them at opposite values) or vice versa; in particular check any logic which handles pixel positions, like menus clicking.
 
: You can test whether your mod accounts for this correctly by setting the zoom to maximum and the UI scale to minimum (''i.e.,'' have them at opposite values) or vice versa; in particular check any logic which handles pixel positions, like menus clicking.
 +
 +
==Multiplayer concepts==
 +
===Net fields===
 +
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 <samp>NetBool</samp>, or complex values like <samp>NetFieldDictionary</samp>. The game will regularly collect all the net fields reachable from <samp>Game1.netWorldState</samp> 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). 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"
 +
|-
 +
! net type
 +
! description
 +
|-
 +
| <samp>NetBool</samp><br /><samp>NetColor</samp><br /><samp>NetFloat</samp><br /><samp>NetInt</samp><br /><samp>NetPoint</samp><br /><samp>NetString</samp>
 +
| A simple synchronised value. Access the value using <samp>field.Value</samp>.
 +
|-
 +
| <samp>NetCollection&lt;T&gt;</samp><br /><samp>NetList&lt;T&gt;</samp><br /><samp>NetObjectList&lt;T&gt;</samp>
 +
| A list of <samp>T</samp> values. This implements the standard interfaces like [https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1 <samp>IEnumerable&lt;T&gt;</samp>] and [https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ilist-1 <samp>IList&lt;T&gt;</samp>], so you can iterate it directly like <code>foreach (T value in field)</code>.
 +
|-
 +
| <samp>NetLongDictionary&lt;TValue, TNetValue&gt;</samp><br /><samp>NetPointDictionary&lt;TValue, TNetValue&gt;</samp><br /><samp>NetVector2Dictionary&lt;TValue, TNetValue&gt;</samp>
 +
| Maps <samp>Long</samp>, <samp>Point</samp>, or <samp>Vector2</samp> keys to instances of <samp>TValue</samp> (the underlying value type) and <samp>TNetValue</samp> (the synchronised net type). You can iterate key/value pairs like <code>foreach (KeyValuePair<Long, TValue> pair in field.Pairs)</code> (replacing <samp>Long</samp> with <samp>Point</samp> or <samp>Vector2</samp> if needed).
 +
|}
    
==Assets==
 
==Assets==
translators
8,445

edits

Navigation menu