Skip to content

diogo-webber/API-inventory-images

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 

Repository files navigation

[API] Inventory Images

An API for mod development, written in Lua, for the Klei Entertainment's game, Don't Starve.




Setup:

The API can be imported in 2 ways:

1. Having the API in your mod files:

  1. Download the API file.

  2. Put the downloaded file in your mod files.

  3. Import it with the modimport function.

  modimport("path/to/inv_images_API.lua")

2. Using the workshop mod as a dependency (recommended):

  1. Add the mod as a dependency on your mod's workshop page.
  1. Import the API with the modimport function.
  modimport("../workshop-2840451757/inv_images_API.lua")

Handling possible errors: (recommended)

local _G = GLOBAL

local API_path = "../workshop-2840451757/inv_images_API.lua"
local API_file_exist = _G.softresolvefilepath(MODROOT..API_path)

if API_file_exist then
    modimport(API_path)
    AddInventoryItemAtlas("path/to/atlas.xml", Assets)
else
    --> Show a simple warning popup.
    local PopupDialogScreen = _G.require "screens/popupdialog"
    
    local API_mod_url = "steam://openurl/https://steamcommunity.com"..
                        "/sharedfiles/filedetails/?id=2840451757"
                        
    local API_warning_showed = false

    AddGlobalClassPostConstruct("screens/mainscreen", "MainScreen", function(self)
        local _OnBecomeActive = self.OnBecomeActive
        function self:OnBecomeActive()
            _OnBecomeActive(self)

            if not API_warning_showed then
                API_warning_showed = true

                local popup = PopupDialogScreen(
                _G.KnownModIndex:GetModFancyName(modname).." Warning", 
                "The mod needs the \"[API] Inventory Images\" mod downloaded!",
                    {
                        {text="Ok", cb = function() _G.TheFrontEnd:PopScreen() end},
                        {text="Download It", cb = function() _G.VisitURL(API_mod_url) end}
                    }
                )

                popup.title:SetPosition(0, 60, 0)
                popup.text:SetPosition(0, -50, 0)

                _G.TheFrontEnd:PushScreen(popup)
            end
        end
    end)
end

Obs: This method is good for keeping the API up to date.


Usage:

Simply call this function in modmain.lua:

   AddInventoryItemAtlas(atlas_path, assets_table)

Function documentation:

Adds a global inventory items atlas, compatible with mini signs, crafts, ingredients and shelves.
It can be called indefinitely.

Parameters:

  • atlas_path (string) - The xml file path.
  • assets_table (table) - The "Assets" table, to load the atlas assets. Not required.

   Example: AddInventoryItemAtlas("inventoryimages.xml", Assets)

Usage Exemples:

  • These are just examples! Your mod doesn't need to be organized this way.

1. Having the API in your mod files:

It also demonstrates the use of the AddInventoryItemAtlas load assets feature.

📁 mod_folder/
  📁 images/
      📄 itemicons.xml
      🌆 itemicons.tex
      
  📁 scripts/
      📄 inv_images_API.lua
      
  📄 modmain.lua
      >> modimport("scripts/inv_images_API.lua")
      >> Assets = {...}
      >> AddInventoryItemAtlas("images/itemicons.xml", Assets)

2. Using the workshop mod as a dependency:

It also demonstrates the NOT use of AddInventoryItemAtlas load assets feature. Notice the ATLAS_BUILD asset.

📁 mod_folder/
    📁 images/
        📄 myinventoryimages.xml
        🌆 myinventoryimages.tex
        
    📄 modmain.lua
        >> Assets = {
              Asset("ATLAS", "images/myinventoryimages.xml"),
              Asset("IMAGE", "images/myinventoryimages.tex"),
              Asset("ATLAS_BUILD", "images/myinventoryimages.xml", 256),
           }
        
        >> modimport("../workshop-2840451757/inv_images_API.lua")
        >> AddInventoryItemAtlas("images/myinventoryimages.xml")


Notes:

  • It is NOT necessary to set an inventoryitem.atlasname in each prefab.

  • Ideally, all your images should be in a single or few atlas, otherwise there may be a delay in loading the mod.
    Use some tool like Ztools to do this.

  • In order for icons to appear on mini signs and shelves, the dimension of textures (the images) must be a power of 2 (usually 64x64).



In Game Picture:

Preview Image


📜 License

This project is under MIT license. See the LICENSE file for more details.

About

An API for mod development in the game Don't Starve.

Resources

License

Stars

Watchers

Forks

Languages