Building interfaces part 2: controller collection and script
- Create at least two additional collections so that you have the following structure:
- main.collection (to be used as the loading/unloading game world controller)
- menu.collection (to be used as the landing screen the player sees when the game is loaded. main.collection loads this screen when it is initialised)
- game.collection (the collection where your game is played)
Of course you can have many more collections if your game requires it such as level1.collection, level2.collection etc. Collections can share scripts, game objects and other components so you don't need to create multiple copies of these objects for each collection.
Because collections are loaded when they are needed, the interface controller collection requires a "collection proxy" for each collection it will load later. It is this collection proxy object that you refer to in code.
- In main.collection, in the outline panel, create a game object with the Id, 'controller'. This is simply a home for the collection proxies and the script to load the collections.
- In the outline panel, right click ' controller' and select, 'Add Component', 'Collection Proxy'.
- In the properties panel, change the 'Id' to match the collection name with the word, 'proxy' and set the 'Collection' property to be the collection to load.
- Do this for each of the collections in your game.
- In the assets panel, create a new script (not a GUI script) called 'controller' to handle the loading and unloading of collections.
function init(self)
msg.post("#menu_proxy", "load")
self.current_collection = "#menu_proxy"
end
function on_message(self, message_id, message, sender)
-- show the collection once loaded
if message_id == hash("proxy_loaded") then
msg.post(sender, "enable")
-- load the collection when message received
elseif message_id == hash("show_menu") then
msg.post(self.current_collection, "unload")
msg.post("#menu_proxy", "load")
self.current_collection = "#menu_proxy"
elseif message_id == hash("show_game") then
msg.post(self.current_collection, "unload")
msg.post("#game_proxy", "load")
self.current_collection = "#game_proxy"
end
end
This script handles the loading and unloading of collections.
As it is used infrequently there is no need to use pre-hashing, but you could.
The strings such as #menu_proxy and #game_proxy need to match what you called the collection proxy objects in main.collection. The hash symbol is not part of the name you set in the properties and simply refers to a component of the current game object(controller).
"show_menu" and "show_game" are strings we will pass using a message to this script later when we want to load those associated collections.
Note, by using a controller collection we can handle receiving a message from a collection script to say it is loaded with just two lines of code that are not duplicated anywhere else in our project:
if message_id == hash("proxy_loaded") then
msg.post(sender, "enable")
- Add the script as a component of the 'controller' object in main.collection.
(In the outline panel right click, 'controller', 'Add Component File'.)
That's the main controller setup. Now we need to send a message from another script when we want to instruct the controller to load a collection.