Difference between revisions of "Modding:Common tasks"

From Stardew Valley Wiki
Jump to navigation Jump to search
Line 68: Line 68:
 
//todo
 
//todo
 
==UI==
 
==UI==
//todo describe section
+
 
 +
The UI is a collection of separate elements which make up the HUD and occasional popups.
 +
 
 +
//todo expand section.
 +
 
 +
 
 
===Banner Message===
 
===Banner Message===
Add a new HUDMessage to show [insert image] banner.
+
HUDMessage are those popups in the lower left hand screen. They have several constructors, which we will briefly go over here (a few non relevant ones have been snipped):
 +
 
 +
<source lang="c#">
 +
  public HUDMessage(string message);
 +
  public HUDMessage(string message, int whatType);
 +
  public HUDMessage(string type, int number, bool add, Color color, Item messageSubject = null);
 +
  public HUDMessage(string message, string leaveMeNull)
 +
  public HUDMessage(string message, Color color, float timeLeft, bool fadeIn)
 +
</source>
 +
 
 +
So before we go over when you'd use them, I'm going to briefly note how the class HUDMessage uses these. (I encourage people to read the class if they have further questions, but I doubt most of us will need to know more than this)
 +
 
 +
WhatType:
 +
1 - Achievement
 +
2 - New Quest
 +
3 - Error
 +
4 - Stamina
 +
5 - Health
 +
 
 +
Color: Fairly obvious. It should be noted that while the first two don't give an option (they default to ''Color:OrangeRed''), the fourth with the param 'leaveMeNull' displays as the same color as the game text.
 +
 
 +
For specifics:
 +
'' public HUDMessage(string type, int number, bool add, Color color, Item messageSubject = null);'' - This allows for expanded customization of the message. More often used for money.
 +
''  public HUDMessage(string message, string leaveMeNull)'' - Also displays no icon.
 +
''  public HUDMessage(string message, Color color, float timeLeft, bool fadeIn)'' - Displays a message that fades in for a set amount of time.
 +
 
 +
Note: For those of you who want a custom HUDMessage:
 +
- Almost all of these variables are public, excluding messageSubject, so feel free to customize!
 +
 
 +
 
 +
For example: add a new HUDMessage to show [insert image] banner.  
 
<source lang="c#">
 
<source lang="c#">
 
Game1.addHUDMessage(new HUDMessage("MESSAGE", 3));
 
Game1.addHUDMessage(new HUDMessage("MESSAGE", 3));
 
</source>
 
</source>
Optional parameters ''noIcon'' and ''timeLeft''
+
 
The 3 is because ...?
 
 
==Menus==
 
==Menus==
 
//todo describe section
 
//todo describe section

Revision as of 22:31, 21 May 2018

Index

Axe.png
Article Stub

This article is a stub and is missing information. You can help Stardew Valley Wiki by expanding it!

This page explains how mod creators can perform common tasks in SMAPI mods. (Note: this page is for interacting with the game itself, for SMAPI-provided tasks see SMAPI reference)

Things to note

Many things in Stardew Valley are accessed through Game1. It is highly recommended to decompile the game source and look at how the game does things and then attempt to do it yourself. This is a general guide to point you to the right places in the code to look, it is not meant to give you the code. Modding:Creating a SMAPI mod //todo link to decompiling section

Items

Items are objects which represent things which can be put in an inventory. Tools, Crops, etc.

Create an Item

//todo

Remove an Item

//todo

Add an item to an inventory

//todo

Remove an item from an inventory

This is dependent on the inventory - rarely will you be calling this directly, as the game has functions for this for the Player, located in Farmer (in the main namespace).

To do so, in most situations, just call .removeItemFromInventory(Item)

Locations

The GameLocation is a representation of any place in the game (e.g. Farm, Town)

The list of current locations is stored in Game1.currentLocations

Important Caveat: In 1.3 onwards, the Game1.currentLocations is not reliable for farmhands in MP.

Map Properties

You can edit many properties of the current map and the tiles in the map. This is already documented here Modding:Maps

Tiles

terrainFeatures contains information about the tiles. (e.g. dirt, grass, crops, etc.)

objects contains information about objects on top of tiles. (e.g. crops, stones)

Handling TerrainFeatures

As terrainFeatures is a NetField, always use .Pairs to access it for enumeration. The .Key value of this stores the location, and the .Value contains the terrainFeature itself. As a note, this includes items spawned off the map, which is usually cleared at end of day.

If you need to access just crops, this snippet will be of use:

            foreach (var tf in Game1.getFarm().terrainFeatures.Pairs)
            {
                if ((tf.Value is HoeDirt hd) && hd.crop != null)
                {
                    // do crop like things here.
                }
            }

If you need some other location, sub that out for Game1.getFarm(). Note this doesn't null check Farm! Make sure Farm is loaded.

Handling Objects

There are several fields that handle objects, but always use the .objects one. The same rules apply as in TerrainFeatures, except that you really can't place objects beyond the edge of the map.

Player

//todo describe section

Position

//todo

NPC

//todo

UI

The UI is a collection of separate elements which make up the HUD and occasional popups.

//todo expand section.


HUDMessage are those popups in the lower left hand screen. They have several constructors, which we will briefly go over here (a few non relevant ones have been snipped):

  public HUDMessage(string message);
  public HUDMessage(string message, int whatType);
  public HUDMessage(string type, int number, bool add, Color color, Item messageSubject = null);
  public HUDMessage(string message, string leaveMeNull)
  public HUDMessage(string message, Color color, float timeLeft, bool fadeIn)

So before we go over when you'd use them, I'm going to briefly note how the class HUDMessage uses these. (I encourage people to read the class if they have further questions, but I doubt most of us will need to know more than this)

WhatType: 1 - Achievement 2 - New Quest 3 - Error 4 - Stamina 5 - Health

Color: Fairly obvious. It should be noted that while the first two don't give an option (they default to Color:OrangeRed), the fourth with the param 'leaveMeNull' displays as the same color as the game text.

For specifics: public HUDMessage(string type, int number, bool add, Color color, Item messageSubject = null); - This allows for expanded customization of the message. More often used for money. public HUDMessage(string message, string leaveMeNull) - Also displays no icon. public HUDMessage(string message, Color color, float timeLeft, bool fadeIn) - Displays a message that fades in for a set amount of time.

Note: For those of you who want a custom HUDMessage: - Almost all of these variables are public, excluding messageSubject, so feel free to customize!


For example: add a new HUDMessage to show [insert image] banner.

Game1.addHUDMessage(new HUDMessage("MESSAGE", 3));

Menus

//todo describe section

Set the active Menu

Game1.activeClickableMenu = <Menu>

Simple Menu

Copy the menu you want from the game and make it your own

Harmony

Here be dragons. Thou art forewarned. - Pathoschild

Ask @Cat or @Sakorona on Discord about this

Other

Add a small animation

location.temporarySprites.Add(new TemporaryAnimatedSprite(...))

See TemporaryAnimatedSprite for more details

Play a sound

location.playSound("SOUND");

(e.g. "junimoMeep1")

Open Source

When all else fails, when you've looked at the decompiled source too long and it makes no sense, take a look at some open source mod code! There's a lot out there, including (in no particular order):