Repository navigation
Expand file tree
/
Copy pathmain.lua
More file actions
78 lines (68 loc) · 2.35 KB
/
Copy pathmain.lua
File metadata and controls
78 lines (68 loc) · 2.35 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
local panel = bsx.ui.panel("API 4 Live Panel")
local status_id = panel:text("Waiting for world...")
panel:separator()
local auto_update = true
local poll_seconds = 2.0
local world_ready = false
local logged_panel = false
local function read_player_line()
local actors, why_a = game.actors(32)
if not actors then
return "Snapshot unavailable: " .. tostring(why_a)
end
for i = 1, #actors do
if actors[i].controlled then
local st, why_s = game.stats.read(actors[i].actor, 0)
if st then
return string.format("HP: %d / %d (actors=%d)", st.current, st.max, actors.total or #actors)
end
return "HP unavailable: " .. tostring(why_s)
end
end
return "No controlled actor in snapshot"
end
panel:button("Refresh & Log", function()
local line = read_player_line()
panel:set_text(status_id, line)
bsx.bus.emit("api4_panel.refreshed", { text = line, dev = bsx.dev_mode })
bsx.log("api4_panel: " .. line)
end)
panel:checkbox("Auto refresh", auto_update, function(checked)
auto_update = checked
bsx.log("api4_panel: auto_update=" .. tostring(checked))
end)
panel:slider("Poll seconds", 1.0, 10.0, poll_seconds, function(value)
poll_seconds = value
end)
bsx.bus.provide("api4_panel.state", function()
return { active = panel:active(), auto = auto_update, poll = poll_seconds, world_ready = world_ready }
end)
local function on_world_entered()
if world_ready then return end
world_ready = true
panel:set_text(status_id, "World ready | " .. read_player_line())
end
local function sync_world_ready()
if world_ready then return end
local st = game.player_state()
if (st and st.world_ready) or game.player_position() then
on_world_entered()
end
end
bsx.on("world_ready", on_world_entered)
bsx.on("save_load", function() world_ready = false end)
pcall(bsx.on, "title_screen", function() world_ready = false end)
local last_poll = 0
bsx.on("frame", function()
if not logged_panel then
logged_panel = true
bsx.log("api4_panel: panel ready dev=" .. tostring(bsx.dev_mode))
end
sync_world_ready()
if not auto_update or not world_ready then return end
local now = os.clock()
if now - last_poll >= poll_seconds then
last_poll = now
panel:set_text(status_id, read_player_line())
end
end)