Changes

Jump to navigation Jump to search
3,070 bytes added ,  20:55, 28 September 2019
→‎Inject dynamic content: Added some source code as an example.
Line 379: Line 379:  
If you want to send a letter that contains data that needs to change based on the situation, such as the number of purple mushrooms eaten today, then you have to create that letter content each time you plan on sending it, especially if you want an up-to-date value.  That is what I am referring to by "dynamic" letters.  
 
If you want to send a letter that contains data that needs to change based on the situation, such as the number of purple mushrooms eaten today, then you have to create that letter content each time you plan on sending it, especially if you want an up-to-date value.  That is what I am referring to by "dynamic" letters.  
   −
//TODO: Explain caveat about player quitting with dynamic letter in the mailbox.
+
Consider the following source code, which is basically an enhanced version of the static mail class shown above, that will also support "dynamic" content.  You could certainly always use the enhanced version of this code and just not make use of the dynamic content unless needed.  The code was separated for the purpose of illustrating the differences.
   −
//TODO: This will be expanded soon...  please check back!
+
<source lang="c#">
 +
using StardewModdingAPI;
 +
using System.Collections.Generic;
 +
 
 +
namespace MyMail
 +
{
 +
    public class MailData : IAssetEditor
 +
    {
 +
        // This collection holds any letters loaded after the initial load or last cache refresh
 +
        private Dictionary<string, string> dynamicMail = new Dictionary<string, string>();
 +
 
 +
        public MailData()
 +
        {
 +
        }
 +
 
 +
        public bool CanEdit<T>(IAssetInfo asset)
 +
        {
 +
            return asset.AssetNameEquals("Data\\mail");
 +
        }
 +
 
 +
        public void Edit<T>(IAssetData asset)
 +
        {
 +
            var data = asset.AsDictionary<string, string>().Data;
 +
 
 +
            // This is just an example
 +
            data["StaticMail"] = "If there were any letters with static content they could be placed here.";
 +
 
 +
            // Inject any mail that was added after the initial load.
 +
            foreach (var item in dynamicMail)
 +
            {
 +
                data.Add(item);
 +
            }
 +
 
 +
            dynamicMail.Clear();    // For the usage of this MOD the letters are cleared
 +
        }
 +
 
 +
        /// <summary>
 +
        /// Add a new mail asset into the collection so it can be injected by the next cache refresh.  The letter will
 +
        /// not be available to send until the cache is invalidated in the code.
 +
        /// </summary>
 +
        /// <param name="mailId">The mail key</param>
 +
        /// <param name="mailText">The mail text</param>
 +
        public void Add(string mailId, string mailText)
 +
        {
 +
            if (!string.IsNullOrEmpty(mailId))
 +
            {
 +
                if (dynamicMail.ContainsKey(mailId))
 +
                    dynamicMail[mailId] = mailText;
 +
                else
 +
                    dynamicMail.Add(mailId, mailText);
 +
            }
 +
        }
 +
    }
 +
}
 +
 
 +
</source>
 +
 
 +
You will notice that there is really very little difference in the code used for static mail and dynamic mail.  The class that supports dynamic mail has a private dictionary collection for holding on to any mail content waiting to be injected.  It could have been made public to allow mail to be added directly into the collection, but that is not good practice.  Instead a public Add method was provided so that mail could be sent, so to speak, to the collection.  This code is for a specific MOD, not a robust framework, so it isn't overly concerned with error handling. You can improve that based on your needs.
 +
 
 +
Notice the additional code in the Edit method, where any mail in the dynamicMail collection is injected into Stardew Valley's content.  There will be no mail in the dynamicMail collection when the MOD is loaded (in this case) the first time. If you add mail after the original load, then the content will have to be reloaded by invalidating the cacheRefer to [https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Content#Advanced Invalidating Cache] for more details.
    
===Send a letter===
 
===Send a letter===
49

edits

Navigation menu