Repository navigation
Expand file tree
/
Copy pathmain.lua
More file actions
49 lines (43 loc) · 1.92 KB
/
Copy pathmain.lua
File metadata and controls
49 lines (43 loc) · 1.92 KB
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
-- Copy this folder to BSX/scripts/item_tracker and choose an exact row in settings.ini.
-- Lines go to BSX/bsx.log. These helpers read the controlled character's own inventory.
local row = config.int("ItemTracker", "ItemRow", -1)
local interval = config.int("ItemTracker", "IntervalMs", 5000)
local last_count, last_error = nil, nil
if interval < 1000 or interval > 86400000 then
bsx.log("item tracker: IntervalMs must be 1000..86400000; using 5000")
interval = 5000
end
local function check_count()
-- Counts are decimal strings, so even very large counts stay exact.
local count, why = game.inventory.count(row)
if count == nil then
local message = tostring(why)
if message ~= last_error and bsx.log("item tracker: count unavailable (" .. message .. ")") then
last_error = message
end
return
end
last_error = nil
if count == last_count then return end
-- A cached name may be unavailable even when the count was read successfully.
local name, name_why = game.item_name(row)
if name == nil then name = "name unavailable: " .. tostring(name_why) end
if bsx.log("item tracker: row " .. row .. " (" .. name .. ") count " .. count) then
last_count = count
end
end
local function poll()
check_count()
bsx.after(interval, poll)
end
if row < 0 or row > 65534 then
bsx.log("item tracker: set ItemRow in settings.ini to an exact item row from 0 to 65534")
else
-- Completion events can be unavailable on a build. Keep the timer and other events working.
for _, event in ipairs({"save_loaded", "equip_applied", "unequip_applied"}) do
local ok, why = pcall(bsx.on, event, check_count)
if not ok then bsx.log("item tracker: " .. event .. " unavailable (" .. tostring(why) .. ")") end
end
-- Polling also finds changes unrelated to equipment and handles a save loaded before startup.
bsx.after(interval, poll)
end