mirror of
https://gitlab.com/luk3yx/minetest-flow.git
synced 2025-09-08 07:46:31 +02:00
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:
parent
e7029283ab
commit
2b00758f5b
6
init.lua
6
init.lua
@ -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
|
||||
|
||||
|
6
test.lua
6
test.lua
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user