Difference between revisions of "Modding:Modder Guide/Game Fundamentals"

From Stardew Valley Wiki
Jump to navigation Jump to search
(+ net fields from Modding:Migrate to Stardew Valley 1.3 (only author is Pathoschild))
(→‎Net fields: rewrite to fit new page)
Line 5: Line 5:
 
==Data structures==
 
==Data structures==
 
===Net fields===
 
===Net fields===
A 'net type' is any of several new classes which Stardew Valley 1.3 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>. Many existing fields have been converted to net types (called 'net fields'), each wrapping the underlying value:
+
{{upcoming|1.3}}
 +
 
 +
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):
 
: <source lang="C#">
 
: <source lang="C#">
 
NetString str = new NetString("bar");
 
NetString str = new NetString("bar");
Line 11: Line 15:
 
</source>
 
</source>
  
Impact on mods:
+
The build config NuGet package should detect most implicit conversions and show an appropriate build warning.
* 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 equivalent normal values (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.'''
 
 
 
Suggested fix:
 
* With the latest mod build package installed, rebuild your project. The package will detect net field references you need to update, and show an appropriate warning. See [[#Fix common build warnings|''fix common build warnings'']] below.
 
 
 
  
 
[[Category:Modding]]
 
[[Category:Modding]]

Revision as of 23:35, 1 June 2018

Index

This page explains some of the Stardew Valley fundamentals that are useful for modders. See also Modding:Common tasks.

Data structures

Net fields

The following describes the upcoming Stardew Valley 1.3, and may change before release.

A 'net type' is any of several classes which Stardew Valley uses to sync data between players, named for the Net prefix in their name. A net type can represent a simple value like NetBool, or complex values like NetFieldDictionary. The game will regularly collect all the net fields reachable from Game1.netWorldState 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 bool x = new NetBool(true)), but their conversion rules can be counterintuitive and error-prone. For example, item?.category == null && item?.category != null can both be true at once. Always avoid implicit casts to minimise bugs. Instead, access the underlying value using .Value (or .Pairs on a net dictionary):

NetString str = new NetString("bar");
if (str.Value == "bar") // true

The build config NuGet package should detect most implicit conversions and show an appropriate build warning.