Custom Keywords - daviscook477/BaseMod GitHub Wiki

Keywords

In the base base game keywords are things like "Block", "Retain", "Weak", "Vulnerable", etc... that are highlighted and have tooltip information associated with them.

API

addKeyword(String[] names, String description) - only call this in the receiveEditKeywords callback that you get from EditKeywordsSubscriber or else your keywords might not be initialized properly

  • names - This should be an array of similar words to the word you want. (kinda like this {"block", "blocks"}). The name must be all lowercase or it will not work.
  • description - This should be the description for the keyword

Usage

Keywords should be stored in a JSON file (usually named keywords.json) and the receiveEditKeywords method should load that file register all the keywords found in it (using the addEditKeywords method). Most StS modding templates have a framework already set up for this (e.g., BasicMod's receiveEditKeywords). This code goes in your main mod file (which needs to implement EditKeywordsSubscriber).

If you are setting up your own project without using a template (not recommended for new modders!), generic code for this looks roughly as follows. This assumes you're following a mostly standard project setup, and to use this code you will need to define two static variables: (1) resourcesFolder, which is the path to where you're putting non-code resources like images and text; and (2) modID, the ID of your mod. If you're not sure what's going on here, consider following the BasicMod guide, which has this set up for you already, along with a ton of other helpful stuff.

    public static String localizationPath(String lang, String file) {
        return resourcesFolder + "/localization/" + lang + "/" + file;
    }

    @Override
    public void receiveEditKeywords() {
        Gson gson = new Gson();
        String json = Gdx.files.internal(localizationPath(Settings.language, "keywords.json")).readString(String.valueOf(StandardCharsets.UTF_8));
        Keyword[] keywords = gson.fromJson(json, Keyword[].class);

        if (keywords != null) {
            for (Keyword keyword : keywords) {
                //The modID here must be lowercase
                BaseMod.addKeyword(modID.toLowerCase(), keyword.PROPER_NAME, keyword.NAMES, keyword.DESCRIPTION);
            }
        }
    }

Manual keyword creation (deprecated, not best practice)

While it's not recommended, let's say you want to manually create a keyword called "Ice" without a JSON file for keywords (meaning your mod will not support localizing keywords in other languages). This can be done in your EditKeywordsSubscriber method.

BaseMod.addKeyword({"ice"},"Will apply a buff to the next attack with Ice in its description.");