Changes

m
Replace deprecated <source> tags with <syntaxhighlight> tags
Line 7: Line 7:  
===Get new ID===
 
===Get new ID===
 
In some cases the game expects a 'multiplayer ID' value, which is a unique 64-bit number. This is mainly intended for cases where the game expects a multiplayer ID for data sync, such as when creating farm animals:
 
In some cases the game expects a 'multiplayer ID' value, which is a unique 64-bit number. This is mainly intended for cases where the game expects a multiplayer ID for data sync, such as when creating farm animals:
<source lang="c#">
+
<syntaxhighlight lang="c#">
 
int animalID = this.Helper.Multiplayer.GetNewID();
 
int animalID = this.Helper.Multiplayer.GetNewID();
 
var animal = new FarmAnimal("Cow", animalID, ownerID);
 
var animal = new FarmAnimal("Cow", animalID, ownerID);
</source>
+
</syntaxhighlight>
    
===Get active locations===
 
===Get active locations===
 
In multiplayer mode, the game doesn't sync all locations to other players. Each farmhand will receive data for their current location, the farm, farmhouse, and farm buildings. You can get a list of the locations being sync'd:
 
In multiplayer mode, the game doesn't sync all locations to other players. Each farmhand will receive data for their current location, the farm, farmhouse, and farm buildings. You can get a list of the locations being sync'd:
<source lang="C#">
+
<syntaxhighlight lang="C#">
 
foreach (GameLocation location in this.Helper.Multiplayer.GetActiveLocations())
 
foreach (GameLocation location in this.Helper.Multiplayer.GetActiveLocations())
 
{
 
{
 
   // do stuff
 
   // do stuff
 
}
 
}
</source>
+
</syntaxhighlight>
    
===Get connected player info===
 
===Get connected player info===
Line 71: Line 71:     
For example, this will log information about the currently connected players:
 
For example, this will log information about the currently connected players:
<source lang="C#">
+
<syntaxhighlight lang="C#">
 
foreach (IMultiplayerPeer peer in this.Helper.Multiplayer.GetConnectedPlayers())
 
foreach (IMultiplayerPeer peer in this.Helper.Multiplayer.GetConnectedPlayers())
 
{
 
{
Line 85: Line 85:  
     }
 
     }
 
}
 
}
</source>
+
</syntaxhighlight>
    
'''Note:''' players whose connections haven't been approved by the game yet are returned too. Although you can send/receive messages through SMAPI's APIs, the game itself may not know about them yet. If you only want players who are fully connected, you can do something like this instead:
 
'''Note:''' players whose connections haven't been approved by the game yet are returned too. Although you can send/receive messages through SMAPI's APIs, the game itself may not know about them yet. If you only want players who are fully connected, you can do something like this instead:
<source lang="C#">
+
<syntaxhighlight lang="C#">
 
foreach (Farmer farmer in Game1.getOnlineFarmers())
 
foreach (Farmer farmer in Game1.getOnlineFarmers())
 
{
 
{
 
   IMultiplayerPeer peer = this.Helper.Multiplayer.GetConnectedPlayer(farmer.UniqueMultiplayerID);
 
   IMultiplayerPeer peer = this.Helper.Multiplayer.GetConnectedPlayer(farmer.UniqueMultiplayerID);
 
}
 
}
</source>
+
</syntaxhighlight>
    
===Send messages===
 
===Send messages===
Line 104: Line 104:     
For example:
 
For example:
<source lang="C#">
+
<syntaxhighlight lang="C#">
 
// broadcast a message to all mods on all computers
 
// broadcast a message to all mods on all computers
 
MyMessage message = new MyMessage(...); // create your own class with the data to send
 
MyMessage message = new MyMessage(...); // create your own class with the data to send
 
this.Helper.Multiplayer.SendMessage(message, "MyMessageType");
 
this.Helper.Multiplayer.SendMessage(message, "MyMessageType");
</source>
+
</syntaxhighlight>
<source lang="C#">
+
<syntaxhighlight lang="C#">
 
// send a message to a specific mod on all computers
 
// send a message to a specific mod on all computers
 
MyMessage message = new MyMessage(...);
 
MyMessage message = new MyMessage(...);
 
this.Helper.Multiplayer.SendMessage(message, "MyMessageType", modIDs: new[] { this.ModManifest.UniqueID });
 
this.Helper.Multiplayer.SendMessage(message, "MyMessageType", modIDs: new[] { this.ModManifest.UniqueID });
</source>
+
</syntaxhighlight>
    
===Receive messages===
 
===Receive messages===
Line 120: Line 120:  
For example:
 
For example:
   −
<source lang="C#">
+
<syntaxhighlight lang="C#">
 
public override void Entry(IModHelper helper)
 
public override void Entry(IModHelper helper)
 
{
 
{
Line 134: Line 134:  
   }
 
   }
 
}
 
}
</source>
+
</syntaxhighlight>
    
==Per-screen data==
 
==Per-screen data==
Line 140: Line 140:     
SMAPI provides <code>PerScreen&lt;T&gt;</code> to make that easy. You use it by creating '''readonly''' fields for your values (which can be any value from <tt>int</tt> to entire class instances):
 
SMAPI provides <code>PerScreen&lt;T&gt;</code> to make that easy. You use it by creating '''readonly''' fields for your values (which can be any value from <tt>int</tt> to entire class instances):
<source lang="C#">
+
<syntaxhighlight lang="C#">
 
private readonly PerScreen<int> LastPlayerId = new PerScreen<int>(); // defaults to 0
 
private readonly PerScreen<int> LastPlayerId = new PerScreen<int>(); // defaults to 0
 
private readonly PerScreen<SButton> LastButtonPressed = new PerScreen<SButton>(createNewState: () => SButton.None); // defaults to the given value
 
private readonly PerScreen<SButton> LastButtonPressed = new PerScreen<SButton>(createNewState: () => SButton.None); // defaults to the given value
</source>
+
</syntaxhighlight>
    
Then you can just use its properties and methods:
 
Then you can just use its properties and methods:
Line 168: Line 168:  
For example, this mod plays a simple game: the main player presses a key, and farmhands need to guess which key was pressed.
 
For example, this mod plays a simple game: the main player presses a key, and farmhands need to guess which key was pressed.
   −
<source lang="C#">
+
<syntaxhighlight lang="C#">
 
internal class ModEntry : Mod
 
internal class ModEntry : Mod
 
{
 
{
Line 206: Line 206:  
     }
 
     }
 
}
 
}
</source>
+
</syntaxhighlight>
    
'''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.
 
'''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.
114

edits