Repository navigation
Expand file tree
/
Copy pathmain.lua
More file actions
45 lines (43 loc) · 2.08 KB
/
Copy pathmain.lua
File metadata and controls
45 lines (43 loc) · 2.08 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
-- SPDX-License-Identifier: LicenseRef-CDAL-BSX-Source-Available-1.0
-- Lua API 2 example: logs the player's own weapon switch. Copy this folder to BSX/scripts/weapon_watch.
-- It only reads. The equipment component keeps the current weapon or tool selection as a
-- slot-name key and an occurrence (CDAL IDs 3047 and 3048); BSX WEAPON_SWAP does not see this switch.
-- The names below are the keys Crimson Live Edit uses; another key is logged as a number.
local names = {
[0x4425304D] = "main weapon", [0x7AA1C455] = "secondary weapon",
[0x54BF2C70] = "tool", [0x0BB2BA9B] = "ranged or special",
}
local key_at = assert(cdal.value("CommonEquipSlotActorComponent::ActiveSlotNameKey"))
local occurrence_at = assert(cdal.value("CommonEquipSlotActorComponent::ActiveSlotOccurrence"))
local last, switches, every = nil, 0, 10 -- read every 10th frame
local function selection()
local actor = game.player()
if not actor then return nil end
local equipment = game.equipment(actor)
if not equipment then return nil end
local key, why = mem.read_u32(equipment + key_at)
if not key then return nil, why end
local occurrence, why_occurrence = mem.read_u32(equipment + occurrence_at)
if not occurrence then return nil, why_occurrence end
return {key = key, occurrence = occurrence, actor = actor}
end
local function describe(s)
if s.key == 0 then return "nothing selected" end
return (names[s.key] or string.format("key 0x%08X", s.key)) .. " #" .. s.occurrence
end
bsx.on("frame", function(e)
if e.frame % every ~= 0 then return end
local now, why = selection()
if not now then
if why and not last then bsx.log("weapon watch: read failed (" .. tostring(why) .. ")") end
last = nil
return
end
if not last or last.actor ~= now.actor then
bsx.log("weapon watch: start with " .. describe(now))
elseif last.key ~= now.key or last.occurrence ~= now.occurrence then
switches = switches + 1
bsx.log("weapon watch: switch " .. switches .. ": " .. describe(last) .. " -> " .. describe(now))
end
last = now
end)