查看“模组:常用方法”的源代码
←
模组:常用方法
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
← [[模组:目录|目录]] {{stub}} 此页面展示了制作SMAPI模组时常见的任务。在阅读时,请结合参考'''[[模组:创建 SMAPI 模组|模组制作入门]]'''和'''[[模组:制作指南/游戏基本架构|游戏基本架构]]'''。 ==基础技巧== ===追踪一个值的变化=== 在写模组时,你可能常常需要了解一个值的变化(什么时候变,变化前后的值分别是多少,等等)。如果该值没有包括在SMAPI内置的'''事件''' (event)中,那么你可以为该值创建一个私有变量,然后在SMAPI的update tick事件中刷新此变量,以达到追踪值变化的目的。 示例见:[模组:制作指南/APIs/Events#变化监控] ==物品 (Items)== 物品 代表那些能够放在背包里的东西,比如说工具、农作物等等。 ===创建一个物品的实例(Object)=== Object中所有的构造函数: <syntaxhighlight lang="c#"> public Object(Vector2 tileLocation, int parentSheetIndex, int initialStack); public Object(Vector2 tileLocation, int parentSheetIndex, bool isRecipe = false); public Object(int parentSheetIndex, int initialStack, bool isRecipe = false, int price = -1, int quality = 0); public Object(Vector2 tileLocation, int parentSheetIndex, string Givenname, bool canBeSetDown, bool canBeGrabbed, bool isHoedirt, bool isSpawnedObject); </syntaxhighlight> '''parentSheetIndex'''为该物品的ID(储存在 ObjectInformation.xnb 文件中)。 ===在地上生成物品=== <syntaxhighlight lang="c#"> public virtual bool dropObject(Object obj, Vector2 dropLocation, xTile.Dimensions.Rectangle viewport, bool initialPlacement, Farmer who = null); // 调用: Game1.getLocationFromName("Farm").dropObject(new StardewValley.Object(itemId, 1, false, -1, 0), new Vector2(x, y) * 64f, Game1.viewport, true, (Farmer)null); </syntaxhighlight> ===添加物品到背包=== <syntaxhighlight lang="c#"> // You can add items found in ObjectInformation using: Game1.player.addItemByMenuIfNecessary((Item)new StardewValley.Object(int parentSheetIndex, int initialStack, [bool isRecipe = false], [int price = -1], [int quality = 0])); </syntaxhighlight> 例2: <syntaxhighlight lang="c#"> // Add a weapon directly into player's inventory const int WEAP_ID = 19; // Shadow Dagger -- see Data/weapons Item weapon = new MeleeWeapon(WEAP_ID); // MeleeWeapon is a class in StardewValley.Tools Game1.player.addItemByMenuIfNecessary(weapon); // Note: This code WORKS. </syntaxhighlight> ===从背包移除物品=== 取决于你背包的具体情况。很少有情况需要你亲自来调用,因为相关的方法在Farmer类中已经有了。 在大多数情况下,仅需调用 .removeItemFromInventory(Item) 方法。 ==地点(Locations)== 见 [[模组:制作指南/游戏基本架构#地点|游戏基本架构#地点]]。 ==获取所有地点== <tt>Game1.locations</tt>属性中储存着主要的地点,但是不包括建筑的室内。以下这个方法提供了主玩家的所有地点。 <syntaxhighlight lang="c#"> /// <summary>Get all game locations.</summary> public static IEnumerable<GameLocation> GetLocations() { return Game1.locations .Concat( from location in Game1.locations.OfType<BuildableGameLocation>() from building in location.buildings where building.indoors.Value != null select building.indoors.Value ); } </syntaxhighlight> 遍历: <syntaxhighlight lang="c#"> foreach (GameLocation location in this.GetLocations()) { // ... } </syntaxhighlight> 注意:联机中客机是拿不到所有地点的。见[[模组:制作指南/APIs/Multiplayer#Get active locations|获取有效的地点]]。
该页面使用的模板:
Template:Key
(
查看源代码
)
Template:Stub
(
查看源代码
)
返回至
模组:常用方法
。
导航菜单
个人工具
创建账户
登录
名字空间
模组
讨论
不转换
不转换
简体
繁體
大陆简体
香港繁體
澳門繁體
大马简体
新加坡简体
台灣正體
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
未翻译的页面
帮助:编辑入门
随机页面
官方链接
官方网站
官方论坛
官方商品
Discord
Reddit
工具
链入页面
相关更改
上传文件
特殊页面
页面信息