local Recipients = import_namespace("bms").Recipients
local ScriptAttachment = import_namespace("bms").ScriptAttachment
local TestComponent = import_namespace("test_utils").TestComponent
local bms = import_namespace("bms")
local world = bms.world
local function attachment_of(e)
return ScriptAttachment.new_entity_script(e, assert(bms.current_script()))
end
local function other_entity()
for _, result in pairs(world.query():component(world.get_type_by_name("bevy_mod_scripting_core::script::ScriptComponent")):build()) do
local e = result[1]
if e:index() ~= bms.current_entity():index() then
return e
end
end
error("could not find the other entity")
end
function double(x)
return x * 2
end
function add(a, b)
return a + b
end
function bounce(back_to)
return bms.run_script_callback("double", attachment_of(back_to), 1)
end
function record(value)
world.insert_component(assert(bms.current_entity()), world.get_type_by_name("test_utils::test_data::TestComponent"), TestComponent.new({ strings = { value } }))
end
function on_test()
local other = other_entity()
local doubled = bms.run_script_callback("double", attachment_of(other), 21)
assert(doubled == 42, "Expected 42, got: " .. tostring(doubled))
local sum = bms.run_script_callback("add", attachment_of(other), 1, 2)
assert(sum == 3, "Expected 3, got: " .. tostring(sum))
local results = bms.run_script_callback("double", Recipients.entity_script(other, assert(bms.current_script())), 5)
assert(#results == 1, "Expected 1 result, got: " .. #results)
assert(results[1] == 10, "Expected 10, got: " .. tostring(results[1]))
assert_throws(function()
bms.run_script_callback("double", attachment_of(bms.current_entity()), 1)
end, "it is the script currently running")
assert_throws(function()
bms.run_script_callback("bounce", attachment_of(other), bms.current_entity())
end, "further up the current chain")
bms.queue_script_callback("record", attachment_of(bms.current_entity()), "queued!")
world.spawn()
assert(not world.has_component(assert(bms.current_entity()), world.get_type_by_name("test_utils::test_data::TestComponent")), "Queued callback ran before the callback queueing it finished")
return "ok"
end
function on_test_post_update()
return assert(world.get_component(assert(bms.current_entity()), world.get_type_by_name("test_utils::test_data::TestComponent"))).strings[1]
end