Changes

Jump to navigation Jump to search
→‎Per-screen data: move content to multiplayer API page
Line 183: Line 183:     
===Per-screen data===
 
===Per-screen data===
[[Multiplayer|Split-screen multiplayer]] swaps the entire game state between each player, so each mod runs across every player. For example, with four local players the [[Modding:Modder Guide/APIs/Events#GameLoop.UpdateTicked|<tt>UpdateTicked</tt>]] event would be raised four times per tick. The game manages its own state automatically (e.g. <tt>Game1.activeClickableMenu</tt>), but if your mod stores info in its own fields you may need to handle those yourself.
+
SMAPI's <code>PerScreen&lt;T&gt;</code> utility manages a separate value for each local screen in split-screen mode. See [[Modding:Modder Guide/APIs/Multiplayer#Per-screen data|<tt>PerScreen&lt;T&gt;</tt> in the multiplayer API]] for details.
 
  −
That can be tricky, so SMAPI provides <code>PerScreen&lt;T&gt;</code> to take care of it. A <code>PerScreen&lt;T&gt;</code> field manages a separate value for each local screen.
  −
 
  −
For example, this mod would keep track of the last button pressed by each player:
  −
 
  −
<source lang="C#">
  −
internal class ModEntry : Mod
  −
{
  −
    private readonly PerScreen<SButton> LastButtonPressed = new PerScreen<SButton>();
  −
 
  −
 
  −
    public override void Entry(IModHelper helper)
  −
    {
  −
        helper.Events.Input.ButtonPressed += this.OnButtonPressed;
  −
    }
  −
 
  −
    private void OnButtonPressed(object sender, ButtonPressedEventArgs e)
  −
    {
  −
        this.LastButtonPressed.Value = e.Button;
  −
    }
  −
}
  −
</source>
  −
 
  −
If you haven't set a value for the current player, <code>PerScreen&lt;T&gt;</code> will return the default value for the type (e.g. 0 for <tt>int</tt>). You can change that by specifying your own default-value logic:
  −
 
  −
<source lang="C#">
  −
private readonly PerScreen<SButton> LastButtonPressed = new PerScreen<SButton>(createNewState: () => SButton.None);
  −
</source>
  −
 
  −
'''Tip:''' you should almost always mark a per-screen field <code>readonly</code>. Overwriting the entire field (instead of setting the <tt>Value</tt> property) will clear the data for all players, instead of setting it for the current one.
      
===Semantic versions===
 
===Semantic versions===
translators
8,411

edits

Navigation menu