Spotter | Writing a script for the controller.

  1. In the assets panel, right click, 'main' and select, 'New...', 'Script'.
  2. Name the script, 'main'.
  3. Change the functions to handle the collection proxies.
function init(self)
	msg.post(".", "acquire_input_focus")
	-- show the title screen when the game is loaded
	msg.post("#title_proxy", "load")
	self.current_collection = "#title_proxy"
end

function final(self)
	msg.post(".", "release_input_focus")
end

function on_message(self, message_id, message, sender)
	-- show the collection once loaded into memory
	if message_id == hash("proxy_loaded") then
		msg.post(sender, "enable")

		-- load the correct collection when a message is received
	elseif message_id == hash("show title screen") then
		-- title screen
		msg.post(self.current_collection, "unload")
		msg.post("#title_proxy", "load")
		self.current_collection = "#title_proxy"

	elseif message_id == hash("show level 1") then
		-- level 1
		msg.post(self.current_collection, "unload")
		msg.post("#level1_proxy", "load")
		self.current_collection = "#level1_proxy"

	elseif message_id == hash("show level 2") then
		-- level 2
		msg.post(self.current_collection, "unload")
		msg.post("#level2_proxy", "load")
		self.current_collection = "#level2_proxy"
	end
end
What does this code do?

When main.collection starts it aquires the input focus, loads the title screen and sets the currently open collection to be '#title_proxy'. This matches the 'Id' property of the collection proxy we made in the previous stage. '#' simply means the current game object. We could have used the full Url of the proxy: '/controller#title_proxy'.

When the script is unloaded it will release the input focus.

When this script receives a message from another collection script it responds.

If the message received was, "proxy loaded", that means the collection that was requested is now in memory and ready. Therefore we send a message back to the sender script of the loaded collection to say it can be enabled and shown to the player.

Later, in the other collection scripts we will send a message to this script telling it which game world to show next. In this script we repond to the message received.

For example, if the message was "show level 1" then we post a message to unload the currently visible collection and load the level 1 collection instead. We always refer to the proxy object and not the actual collection itself since at this stage it is not loaded in memory.

We set an attribute called 'current_collection' to be the proxy we sent the message to. This enables us to use this in each condition with the command, msg.post(self.current_collection, "unload") Thinking ahead, it makes handling more complex navigation much easier with the code.

Note in this example we have not used pre-hashing. While it does improve performace, the collections are only loaded infrequently compared to the code executing in the update and on_message in other collections. Therefore, to keep this code easy to read we have decided not to use pre-hashing. Often in programming there is a balance to strike between easy to read, maintainable code, memory usage and code execution. It is acceptable to use different techniques depending on the situation at hand.

  1. Save the changes by pressing CTRL-S or 'File', 'Save All'.
  2. Attach the main.script file to the controller game object in main.collection.
    Step-by-step guide
    • In the assets panel, in the 'main' folder, double click, 'main.collection'.
    • In the outline panel, right click, 'controller' and select, 'Add Component File'.
    • Select, '/main/main.script' and click, 'OK'.

If you were to run the game at this point you will see a blank screen and it feels like not much was achieved! What actually happened was that main.collection was loaded. The init function of main.script executed. It loaded title.collection and rendered it on the screen. In other words, what you are now looking at is the title screen for the game. However, because both collections are just black screens at the moment, it looked like nothing happened! The next stage is to build a simple interface for our title screen so that the player can choose a level to play. Stage 2a >