Skip to content

feat: Add GlobalNamespace::system_builder, World::add_system and allow dynamic system creation #335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 28, 2025

Conversation

makspll
Copy link
Owner

@makspll makspll commented Feb 28, 2025

Makes adding new event handlers targeting the same or other scripts possible i.e.:

-- add two systems, one before and one after the existing `on_test_post_update` callback, then assert all systems have run
-- in the `on_test_last` callback

local runs = {}    

-- runs on `Update`
function on_test()
    local post_update_schedule = world.get_schedule_by_name("PostUpdate")
    
    local test_system = post_update_schedule:get_system_by_name("on_test_post_update")
    
    local system_after = world.add_system(
        post_update_schedule,
        system_builder("custom_system_after", script_id)
            :after(test_system)
    )
        
    local system_before = world.add_system(
        post_update_schedule,
        system_builder("custom_system_before", script_id)
            :before(test_system)
    )    
end


function custom_system_before()
    print("custom_system_before")
    runs[#runs + 1] = "custom_system_before"
end

-- runs on post_update 
function on_test_post_update()
    print("on_test_post_update")
    runs[#runs + 1] = "on_test_post_update"
end

function custom_system_after()
    print("custom_system_after")
    runs[#runs + 1] = "custom_system_after"
end

-- runs in the `Last` bevy schedule
function on_test_last()
    assert(#runs == 3, "Expected 3 runs, got: " .. #runs)
    assert(runs[1] == "custom_system_before", "Expected custom_system_before to run first, got: " .. runs[1])
    assert(runs[2] == "on_test_post_update", "Expected on_test_post_update to run second, got: " .. runs[2])
    assert(runs[3] == "custom_system_after", "Expected custom_system_after to run third, got: " .. runs[3])
end

@makspll makspll changed the title feat: add GlobalNamespace::system_builder, World::add_system, World.get_schedule_by_name and Schedule::get_system_by_name feat: Add GlobalNamespace::system_builder, World::add_system and allow dynamic system creation Feb 28, 2025
@makspll makspll enabled auto-merge (squash) February 28, 2025 20:40
@makspll makspll mentioned this pull request Feb 28, 2025
8 tasks
@makspll makspll added the needs documentation For features or changes which still need changes in the book label Feb 28, 2025
@makspll makspll merged commit bfa0498 into main Feb 28, 2025
15 checks passed
@makspll makspll deleted the feat/script-systems branch February 28, 2025 21:27
This was referenced Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs documentation For features or changes which still need changes in the book
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant