bevy_mod_scripting 0.19.0

Multi language scripting in Bevy
Documentation
// #main_script lifecycle.lua
SetCurrentLanguage language="@this_script_language"
InstallPlugin emit_responses=true, context_policy="Global"
FinalizeApp

// load script a 
LoadScriptAs as_name="script_a", path="lifecycle.lua"
WaitForScriptLoaded name="script_a"
SpawnEntityWithScript name="test_entity_a", script="script_a"
RunUpdateOnce
AssertCallbackSuccess attachment="EntityScript", label="OnScriptLoaded", script="script_a", entity="test_entity_a", expect_string_value="loaded!"
AssertNoCallbackResponsesEmitted

// reload script_a, with the same content
ReloadScriptFrom script="script_a", path="lifecycle.lua"
RunUpdateOnce 
AssertCallbackSuccess attachment="EntityScript", label="OnScriptUnloaded", script="script_a", entity="test_entity_a", expect_string_value="unloaded!"
AssertCallbackSuccess attachment="EntityScript", label="OnScriptLoaded", script="script_a", entity="test_entity_a", expect_string_value="loaded!"
AssertCallbackSuccess attachment="EntityScript", label="OnScriptReloaded", script="script_a", entity="test_entity_a", expect_string_value="reloaded with: unloaded!"
AssertNoCallbackResponsesEmitted

// load licecycle_b.lua, which prefixes all outputs with "B", the unload callback is called using the old script's state
LoadScriptAs as_name="script_b", path="lifecycle_b.lua"
WaitForScriptLoaded name="script_b"
SpawnEntityWithScript name="test_entity_b", script="script_b"
RunUpdateOnce
AssertCallbackSuccess attachment="EntityScript", label="OnScriptUnloaded", script="script_b", entity="test_entity_b", expect_string_value="unloaded!"
AssertCallbackSuccess attachment="EntityScript", label="OnScriptLoaded", script="script_b", entity="test_entity_b", expect_string_value="B: loaded!"
AssertCallbackSuccess attachment="EntityScript", label="OnScriptReloaded", script="script_b", entity="test_entity_b", expect_string_value="B: reloaded with: unloaded!"
AssertNoCallbackResponsesEmitted

// reload script_b, with the same content
ReloadScriptFrom script="script_b", path="lifecycle_b.lua"
RunUpdateOnce
AssertCallbackSuccess attachment="EntityScript", label="OnScriptUnloaded", script="script_b", entity="test_entity_b", expect_string_value="B: unloaded!"
AssertCallbackSuccess attachment="EntityScript", label="OnScriptLoaded", script="script_b", entity="test_entity_b", expect_string_value="B: loaded!"
AssertCallbackSuccess attachment="EntityScript", label="OnScriptReloaded", script="script_b", entity="test_entity_b", expect_string_value="B: reloaded with: B: unloaded!"
AssertNoCallbackResponsesEmitted

// assert context residents 
AssertContextResidents attachment="EntityScript", script="script_a", entity="test_entity_a", residents_num=2
AssertContextResidents attachment="EntityScript", script="script_b", entity="test_entity_b", residents_num=2


// now first drop the script asset, assert that does nothing yet 
DropScriptAsset script="script_a"
RunUpdateOnce
AssertNoCallbackResponsesEmitted

// now detach the second script, expect unloading, but with the callback from script_b
DespawnEntity entity="test_entity_a"
RunUpdateOnce
AssertCallbackSuccess attachment="EntityScript", label="OnScriptUnloaded", script="script_a", entity="test_entity_a", expect_string_value="B: unloaded!"
AssertNoCallbackResponsesEmitted
AssertContextResidents attachment="EntityScript", script="script_a", entity="test_entity_a", residents_num=1

// drop the second script asset, expect unloading, with callback from script_b
DropScriptAsset script="script_b"
RunUpdateOnce
AssertNoCallbackResponsesEmitted

// detach the second script, expect unloading 
DespawnEntity entity="test_entity_b"
RunUpdateOnce
AssertCallbackSuccess attachment="EntityScript", label="OnScriptUnloaded", script="script_b", entity="test_entity_b", expect_string_value="B: unloaded!"
AssertNoCallbackResponsesEmitted
AssertContextResidents attachment="EntityScript", script="script_b", entity="test_entity_b", residents_num=0