Make flow.get_context() return player too

This is not perfect but I am tired of having to pass player references
around to custom widgets or do something like `ctx.player = player`.
This commit is contained in:
luk3yx 2025-06-18 11:28:16 +12:00
parent e7029283ab
commit 2b00758f5b
2 changed files with 8 additions and 4 deletions

View File

@ -113,12 +113,12 @@ end
local Form = {}
local current_ctx
local current_ctx, current_player
function flow.get_context()
if not current_ctx then
error("get_context() was called outside of a GUI function!", 2)
end
return current_ctx
return current_ctx, current_player
end
-- Returns the new index of the affected element
@ -263,8 +263,10 @@ function Form:_render(player, ctx, formspec_version, id1, embedded, lang_code)
ctx.form = wrapped_form
gui.formspec_version = formspec_version or 0
current_player = player
current_ctx = ctx
local box = self._build(player, ctx)
current_player = nil
current_ctx = nil
gui.formspec_version = 0

View File

@ -1208,8 +1208,10 @@ describe("Flow", function()
})
end)
it("updates flow.get_context", function()
local form = flow.make_gui(function()
assert.equals("inner", flow.get_context().value)
local form = flow.make_gui(function(player_arg)
local ctx, player_from_ctx = flow.get_context()
assert.equals("inner", ctx.value)
assert.equals(player_arg, player_from_ctx)
return gui.Label{label = "Hello"}
end)
test_render(function(p, ctx)