Good work on the Scaleform project, you're almost there.
Here's a way you can modify the main_menu.lua file to pick up the "ZoneX" message coming from Scaleform.
--[[
Customized functions for controlling what happens for a particular loaded level.
These classes should define any of
init(level)
start(level)
update(level, dt)
shutdown(level)
render(level)
]]--
local Util = require 'core/appkit/lua/util'
local SimpleProject = require 'core/appkit/lua/simple_project'
local DebugMenu = require 'core/appkit/lua/debug_menu'
Project.MainMenu = Project.MainMenu or {}
local MainMenu = Project.MainMenu
MainMenu.custom_listener = MainMenu.custom_listener or nil
MainMenu.action = nil
local start_time = 0
local function perform_action()
-- Load empty level
if MainMenu.action == "start" then
MainMenu.shutdown()
local new_level_name = MainMenu.new_level_name or "content/levels/Zone_1"
SimpleProject.change_level(new_level_name)
MainMenu.new_level_name = nil
-- Exit the program
elseif MainMenu.action == "exit" then
stingray.Application.quit()
end
MainMenu.action = nil
end
function MainMenu.start()
if stingray.Window then
stingray.Window.set_show_cursor(true)
end
if scaleform then
scaleform.Stingray.load_project_and_scene("content/ui/simple_menu.s2d/simple_menu")
--Register menu button mouse listener
local custom_listener = MainMenu.custom_listener
custom_listener = scaleform.EventListener.create(custom_listener, MainMenu.on_custom_event)
MainMenu.custom_listener = custom_listener
scaleform.EventListener.connect(custom_listener, scaleform.EventTypes.Custom)
else
local enter_game = function()
MainMenu.action = "start"
perform_action()
end
local exit = function()
MainMenu.action = "exit"
perform_action()
end
MainMenu.debug_menu = DebugMenu(SimpleProject.world, {
title = "Main Menu",
items = {
{text="Enter Game", func=enter_game, target=nil},
{text="Exit", func=exit, target=nil}
}
})
end
local level = SimpleProject.level
start_time = stingray.World.time(SimpleProject.world)
-- make sure camera is at correct location
local camera_unit = SimpleProject.camera_unit
local camera = stingray.Unit.camera(camera_unit, 1)
stingray.Unit.set_local_pose(camera_unit, 1, stingray.Matrix4x4.identity())
stingray.Camera.set_local_pose(camera, camera_unit, stingray.Matrix4x4.identity())
Appkit.manage_level_object(level, MainMenu, nil)
end
function MainMenu.shutdown(object)
if scaleform then
scaleform.EventListener.disconnect(MainMenu.custom_listener)
scaleform.Stingray.unload_project()
end
MainMenu.evt_listener_handle = nil
Appkit.unmanage_level_object(SimpleProject.level, MainMenu, nil)
if stingray.Window then
stingray.Window.set_show_cursor(false)
end
end
function MainMenu.on_custom_event(evt)
if evt.name == "action" then
if evt.data.message:find("^Zone%d") then
MainMenu.new_level_name = "content/levels/" .. evt.data.message
MainMenu.action = "start"
elseif evt.data.message == "exit" then
MainMenu.action = "exit"
end
end
end
-- [[Main Menu custom functionality]]--
function MainMenu.update(object, dt)
if MainMenu.debug_menu then
MainMenu.debug_menu:update()
end
if MainMenu.action == nil then
local time = stingray.World.time(SimpleProject.world)
local p = stingray.Application.platform()
if time - start_time > 1 then
if Appkit.Util.is_pc() then
if stingray.Keyboard.pressed(stingray.Keyboard.button_id("1")) then
MainMenu.new_level_name = "content/levels/Zone1"
MainMenu.action = "start"
elseif stingray.Keyboard.pressed(stingray.Keyboard.button_id("2")) then
MainMenu.new_level_name = "content/levels/Zone2"
MainMenu.action = "start"
elseif stingray.Keyboard.pressed(stingray.Keyboard.button_id("esc")) then
MainMenu.action = "exit"
end
elseif p == stingray.Application.XB1 or p == stingray.Application.PS4 then
if stingray.Pad1.pressed(stingray.Pad1.button_id(Appkit.Util.plat(nil, "a", nil, "cross"))) then
MainMenu.action = "start"
elseif stingray.Pad1.pressed(stingray.Pad1.button_id(Appkit.Util.plat(nil, "b", nil, "circle"))) then
MainMenu.action = "exit"
end
end
end
end
perform_action()
end
return MainMenu
There are two main changes I made.
In on_custom_event(), which gets triggered by the event listener on the Scaleform project, I look at the name of the message coming from Scaleform and see if it starts with "Zone" followed by a number. If so, I save that desired level name in MainMenu.new_level_name and call perform_action() to do the level swap.
I also changed perform_action() so that when the "start" event is detected, it reads the level it should start from that new_level_name variable, instead of just always starting the basic level.
Also, if you want to support starting a numbered zone level from pressing the number keys, you might want to change the update() function to copy the
elseif stingray.Keyboard.pressed(stingray.Keyboard.button_id("2")) then
MainMenu.new_level_name = "content/levels/Zone2"
MainMenu.action = "start"
block once for each zone, and update the button ID and the new_level_name there.