Appkit: /appkit_flow_callbacks.lua — code sample - Stingray Lua API Reference

Appkit: /appkit_flow_callbacks.lua — code sample

Code

--[[

Appkit Flow Nodes require Appkit, WorldWrapper and LevelWrapper to be used. If
not they only print a console message notification when triggered.

]]--

AppkitFlowCallbacks = AppkitFlowCallbacks or {}

local Application = stingray.Application
local Unit = stingray.Unit
local Script = stingray.Script

local function has_appkit()
    if not Appkit then
        print "AppkitFlowCallbacks disabled - no Appkit."
        return false
    end
    if not Appkit.was_setup then
        print "AppkitFlowCallbacks disabled - Appkit was not setup."
        return false
    end
    return true
end

function AppkitFlowCallbacks.set_active_camera(t)
    if has_appkit() == false then return end

    local unit = t.Unit or t.unit
    if unit == nil then
        print "Error in AppkitFlowCallbacks.set_active_camera. unit is nil. unit is required to set camera."
        return
    end
    local camera = t.Camera or t.camera
    if not camera then
        if Unit.num_cameras(unit) > 0 then
            camera = Unit.camera(unit, Script.index_offset())
        else
            print "Error in AppkitFlowCallbacks.set_active_camera. No camera input given and Unit has no cameras."
            return
        end
    end
    local world = Application.flow_callback_context_world()
    local world_wrapper = Appkit.get_managed_world_wrapper(world)
    if not world_wrapper then
        print ("Error: AppkitFlowCallbacks.set_active_camera in world that is not managed by Appkit.")
        return
    end
    local current_camera = world_wrapper:get_enabled_camera()
    if current_camera then
        world_wrapper:set_camera_enabled(current_camera, nil, false) -- unit not required when disabling
    end
    world_wrapper:set_camera_enabled(camera, unit, true)

    -- make sure that camera will get removed from world_wrapper render list
    -- when level is shutdown.
    local level = Application.flow_callback_context_level()
    local level_wrapper = Appkit.get_managed_level_wrapper(level)
    if not level_wrapper then
        print ("Error: AppkitFlowCallbacks.set_active_camera in level that is not managed by Appkit.")
        return
    end
    level_wrapper:add_camera_to_cleanup(camera)
end

function AppkitFlowCallbacks.get_active_camera(t)
    if has_appkit() == false then return end

    local world = Application.flow_callback_context_world()
    local world_wrapper = Appkit.get_managed_world_wrapper(world)
    local out_camera = nil
    local out_unit = nil
    if world_wrapper then
        out_camera, out_unit = world_wrapper:get_enabled_camera()
    end
    return {camera = out_camera, unit = out_unit, Camera = out_camera, Unit = out_unit}
end

-- unloads the current level, if any, and loads the given level
function AppkitFlowCallbacks.load_level(t)
    if has_appkit() == false then return end

    local level_resource_name = t.Name or t.name

    -- this flow node cannot be used to only unload the current level
    if level_resource_name == nil or level_resource_name == "" then return end

    -- cannot load right now as we must allow flow callback processing to finish
    Appkit.set_pending_level_change(Application.flow_callback_context_level(), level_resource_name)
end

function AppkitFlowCallbacks.print_to_screen(t)
    if has_appkit() == false then return end

    local text = t.Text or t.text or ""
    local label = t.Label or t.label or ""
    if text == "" and label == "" then return end

    local world = Application.flow_callback_context_world()
    if not world then print "Error in AppkitFlowCallbacks.print_to_screen: No world." end

    local world_wrapper = Appkit.get_managed_world_wrapper(world)
    if not world_wrapper then print "Error in AppkitFlowCallbacks.print_to_screen: World is not managed by Appkit." end

    local color_vec = t.Color or t.color
    local color
    if color_vec then color = {255, color_vec.x, color_vec.y, color_vec.z} end

    world_wrapper:debug_display_text(label .. text, color)
end

return AppkitFlowCallbacks