This wiki is a read-only version of the Stardew Valley Wiki. The official editable wiki maintained by ConcernedApe can be found at stardewvalleywiki.com
User:BryghtShadow
From Stardew Valley Wiki
Hi!
Code samples
Variables
Although mw:Extension:Variables is probably what should be used, mw:Extension:Arrays can also be used to store and retrieve a variable.
- To store the variable, use
#arraydefine
with the value and specify the delimiter to something that isn't in the variable, e.g.<nowiki/>
. - To retrieve the variable, use either
#arrayprint
or#arrayindex
.#arrayindex
has the benefit of default values if the result is empty.
## Python code ### Wiki code # The array contains an item, so print and index both print the item. foo = ["42"] # {{#arraydefine:foo|42|<nowiki/>}} → print("".join(foo)) # {{#arrayprint:foo}} → 42 print(foo[0:] or "bar") # {{#arrayindex:foo|0|bar}} → 42 # The array is empty, so print returns empty, and index returns the default. foo = [] # {{#arraydefine:foo||<nowiki/>}} → print("".join(foo)) # {{#arrayprint:foo}} → print(foo[0:] or "bar") # {{#arrayindex:foo|0|bar}} → bar
Loops
Using mw:Extension:Arrays, it is possible to create an array of "keys", which is then used on a dictionary or array. While manually maintaining the array may be useful in some cases, a large array can be created with nested loops. One of the possible ways is outlined below.
- Define the array of "numbers" that will be used as keys/indices (such as 0-9). This will be iterated over for each "digit" (ones, tens, hundreds, so on).
- Using arrayprint, go over each item in the above array. Nest for each digit (ones, tens, hundreds, so on). If integers, using #expr to keep the output small is recommended.
- Assign the result into an array (can override previous arrays if they are no longer needed).
- Slice the array of indices if required. Here, you can specify the starting index and the length.
- Finally, iterate over this sliced array and use each key to do something, such as going through ObjectInformation.
# python digits = [0,1,2,3,4,5,6,7,8,9] ids_10 = [n for n in digits] ids_100 = [int('{}{}'.format(n,m)) for n in digits for m in digits] ids_55 = ids_100[0:55]
The following code...
{{#if: {{#arraydefine:digits|0,1,2,3,4,5,6,7,8,9|,}} {{#arraydefine:ids_10| {{#arrayprint:digits|,|@@@|{{#expr:@@@}}}} |,}} {{#arraydefine:ids_100| {{#arrayprint:digits|,|@@@| {{#arrayprint:digits|,|###| {{#expr:@@@###}} }} }} |,}} {{#arrayslice:ids_55|ids_100|0|55}} }} * digits = {{#arrayprint:digits}} * ids_10 = {{#arrayprint:ids_10}} * ids_100 = {{#arrayprint:ids_100}} * ids_55 = {{#arrayprint:ids_55}}
... results in...
- digits = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
- ids_10 = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
- ids_100 = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
- ids_55 = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54