The DRY Guide
DRY is a software development acronym that stands for: Don't Repeat Yourself.
When code (or markup) starts to accumulate a lot of copy-paste code or boilerplate, it becomes error-prone and hard to maintain, both for the original author and for future maintainers—which, in the case of a game mod, can also include other mod authors who want to base their mod on yours.
This page is designed to be a guide and quick reference for mod authors using the UI Framework on how to DRY, whether it's reduce boilerplate, refactor complex views, or just improve the overall consistency of a mod's look and feel.
Reusable Components
StardewUI provides a few different strategies for creating reusable "components", each with its own tradeoffs.
Feature | Scope(1) | Usage | Target | Expansion(2) | Parameterization |
---|---|---|---|---|---|
Templates | Local | Tag | Document (Node Transformer) |
Static | Template attributes |
Included Views | Global | Asset | Binding (View Node) |
Dynamic | Context only |
Custom Views | Global | Tag | Compiled (View Factory) |
Static | Normal attributes |
- Local means available for use in the same view (
.sml
) file only; Global means available for use in any view. - "Dynamic" means the ability to change which template is used, allowing a UI to display content that is not be known ahead of time and may even be from another mod.
The following flowchart may help decide on a strategy:
flowchart TD
A["`Does it require non-standard **layout**?`"]
B["`Does it draw something other than **text** or **sprites**?`"]
C["`Will it create **overlays** or **custom animations**?`"]
D["`Will it be used from **multiple views**?`"]
E["`Do you need to insert **arbitrary content**?`"]
F["`Should customization be done with **attributes** instead of **data**?`"]
Custom{{Custom View}}:::outcome
Included{{Included View}}:::outcome
Template{{Template}}:::outcome
A -- Yes --> Custom
A -- No --> B
B -- Yes --> Custom
B -- No --> C
C -- Yes --> Custom
C -- No --> D
D -- Yes --> Included
D -- No --> E
E -- Yes --> Template
E -- No --> F
F -- Yes --> Template
F -- No --> Included
classDef outcome fill:#6f6,stroke:#009,font-size:18pt
These are simply guidelines to help you choose—in practice, most UIs can be built using any or none of the above methods, and a well-designed project with a complex UI will most likely use several or all of them.