Build Basic Template with menus to select Levels for Engineering Walk through

Build Basic Template with menus to select Levels for Engineering Walk through

Shawn_Hendershot
Participant Participant
810 Views
5 Replies
Message 1 of 6

Build Basic Template with menus to select Levels for Engineering Walk through

Shawn_Hendershot
Participant
Participant

Working on Developing a Basic Menu Template for Company.

 Objectives

- Main menu have Levels 1-5 as Options (done)

- Quit Button

 

I have a basic template Started with 5 buttons and Quit on it.

I got the Buttons and Handlers created for 1-5

 

I understand I need to do the following to get this working.

- add the new level under the project.lua file under Project.level_names

 Project.level_names = {
 --menu = "content/levels/main_menu",
 --basic = "content/levels/basic"
 Zone1 ="content/levels/one"
 Zone2 ="content/levels/two"
 Zone3 ="content/levels/three"
 Zone4 ="content/levels/four
 Zone5 ="content/levels/five
 Zone6 ="content/levels/six
}

 

-change the button handler

 

I am having problems on this step though

-Then add an action to under the main_menu.lua

Do I need to Create a new Function for each or just under each perform action?

 

Also do I need to modify Main_menu_handler to reflect the Additional menu buttons?

 

I have watched this tutorial

https://youtu.be/oiCHLXjTKpk?list=PLTjhBiJe1i2Gjg058V2tskjaBLrwgM4U9

 

 

 

 

 

Reply
Reply
0 Likes
Accepted solutions (1)
811 Views
5 Replies
Replies (5)
Message 2 of 6

_robbs_
Alumni
Alumni

Would you be able to share your project? It's hard to know what to tell you without knowing how your Scaleform project is set up.

Reply
Reply
0 Likes
Message 3 of 6

Shawn_Hendershot
Participant
Participant

Yes I have attached the zipped files.

Reply
Reply
0 Likes
Message 4 of 6

_robbs_
Alumni
Alumni

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.

Reply
Reply
0 Likes
Message 5 of 6

Shawn_Hendershot
Participant
Participant
Accepted solution

_robbs_

 

Thanks for your Quick and accurate response. That did work.  I am sure many others will use this as well. I am having one Error in my Log Console on the mani_menu _handler.lua.

 

localhost / Scaleform Studio: Error: content/ui/simple_menu.s2d/Scripts/main_menu_keyboard_handler.lua:57: Expected Object, got nil as argument #1
localhost / Scaleform Studio: Error: content/ui/simple_menu.s2d/Scripts/main_menu_keyboard_handler.lua:42: attempt to index a nil value
localhost / Application: Lua signals application exit

looks like it is with the. main_menu_scripts[buttonIdx].over()

 

 

local enterFrameListener = scaleform.EventListener.create(enterFrameListener, function(e, thisListener)
    if buttonIdx ~= prevButtonIdx then
        if prevButtonIdx > 0 and prevButtonIdx < widgetCount + 1 then
                main_menu_scripts[prevButtonIdx].normal()
        end
        main_menu_scripts[buttonIdx].over()

        prevButtonIdx = buttonIdx
    end
end )

 

 

Again I appreciate this.

If I can get this one bug Figured out. I would like to close the Forum Post and start another similar For setting up a similar template for OCULUS VR template.

 

Shawn Hendershot

 

 

Reply
Reply
0 Likes
Message 6 of 6

_robbs_
Alumni
Alumni

Hm, look at line 50 of that main_menu_keyboard_handler.lua file. If your Scaleform project doesn't have a button named start anymore, I think you will have to change the name of the first button on line 50 from "main_menu.main.start" to "main_menu.main.Zone_1" or whatever your button is called.

Reply
Reply
0 Likes