I am writing a game which can switch between multiple ‘scenes’ for a school project.
I’m using the bootstrap script scenemanager.script
to manage this through the use of collection proxies for each scene.
I have two scenes present in the project, TitleScreen and ExampleScene. Upon startup, TitleScreen is displayed and the user can click the “Start Game” button to switch to ExampleScene.
Within ExampleScene, there is a button intended to switch back to TitleScreen, but when clicked it does nothing and prints
ERROR:SCRIPT: main/TitleScreen.gui_script:13: No such node: StartGame/bg
stack traceback:
[C]:-1: in function get_node
main/TitleScreen.gui_script:13: in function <main/TitleScreen.gui_script:12>
to the log.
The node StartGame/bg
is present in both TitleScreen.gui and TitleScreen.collection, and works the first time TitleScreen is loaded, so the issue only occurs when loading TitleScreen after ExampleScene.
scenemanager.script:
-- Acquire input focus and load title screen
function init(self)
msg.post(".", "acquire_input_focus")
msg.post("#TitleScreenProxy", "load")
end
-- Manage switching between scenes in response to a message
function on_message(self, message_id, message, sender)
-- Load the Test scene and unload the Title Screen
if message_id == hash("LoadExampleScene") then
msg.post("#TitleScreenProxy", "disable")
msg.post("#TitleScreenProxy", "final")
msg.post("#TitleScreenProxy", "unload")
msg.post("#ExampleSceneProxy", "load")
-- Return to the Title Screen from the Test scene
elseif message_id == hash("LoadTitleScreen") then
msg.post("#ExampleSceneProxy", "disable")
msg.post("#ExampleSceneProxy", "final")
msg.post("#ExampleSceneProxy", "unload")
msg.post("#TitleScreenProxy", "load")
-- Confirm when the target proxy has been loaded
elseif message_id == hash("proxy_loaded") then
print("proxy_loaded", sender)
msg.post(sender, "init")
msg.post(sender, "enable")
elseif message_id == hash("proxy_unloaded") then
print("proxy_unloaded", sender)
end
end
-- Release input focus when not needed
function final(self)
msg.post(".", "release_input_focus")
end
TitleScreen.gui_script:
-- Acquire input focus when loaded
function init(self)
msg.post(".", "acquire_input_focus")
end
-- Release input focus when not needed
function final(self)
msg.post(".", "release_input_focus")
end
-- Draw a sample message to the screen to confirm the scene has been loaded
function on_input(self, action_id, action)
if action.pressed and gui.pick_node(gui.get_node("StartGame/bg"), action.x, action.y) then
msg.post("SceneManager:/SceneManager", "LoadExampleScene")
end
end
Attached is the full project if anyone knows where I went wrong.
Any help would be appreciated and if you need more information/clarification please ask, I’ve been losing my sanity spending the past 2 days reading everything I can on collections and proxies and still not understanding why TitleScreen refuses to load.
Project.zip (580.5 KB)