Color slide improvement help (SOLVED)

Hello I want to continue the color slide games.

The tutorial suggests changing static header “level 1” to change the current level, but I don’t know how to get the UI from loader / from proxy when load the game level, and pass to gameObject or level.gui so it can read by the script.

1 Like

found the solution when I clone another sample from benjames on github, and saw the code using data.lua

and then data.lua become transit data, then put on loader.script

local data = require "main.data" -- add in the beginning 
   function on_message(self, message_id, message, sender)
    	if message_id == hash("load_level") then
    		self.current_level = message.level
    		local proxy = "#proxy_level_" .. self.current_level
    		msg.post(proxy, "load")
    		data.level = self.current_level -- set data 
    	elseif message_id == hash("next_level") then
    		msg.post("#", "unload_level")
    		msg.post("#", "load_level", {level = self.current_level +1})
    	elseif message_id == hash("unload_level") then
    		local proxy = "#proxy_level_" ..self.current_level
    		msg.post(proxy, "disable")
    		msg.post(proxy, "final")
    		msg.post(proxy, "unload")
    	elseif message_id == hash("proxy_loaded") then
    		msg.post(sender, "init")
    		msg.post(sender, "enable")
    	end
    end

next is open level.script and at beginning call again data.lua

l ocal data = require(“main.data”)

function init(self)
	self.bricks = {}
	self.completed = false
	populate_level(self)
	msg.post("#gui", "set_current_level", {level = data.level}) -- message for gui
	msg.post(".", "acquire_input_focus")
end

last is open level.gui_script and on message function add new else if

function on_message(self, message_id, message, sender)
	-- Add message-handling code here
	-- Learn more: https://defold.com/manuals/message-passing/
	-- Remove this function if not needed
	if message_id == hash("level_complete") then
		print("done")
		local done = gui.get_node("done")
		gui.animate(done, "position.x", 320, gui.EASING_OUTBACK, 1, 1.5)
	elseif message_id == hash("smg")then
		print("got message")
	elseif message_id == hash("set_current_level")then
		local currentlevel = message.level
		gui.set_text(gui.get_node("level_text"), ("Level " ..currentlevel)) -- here the code change level
	end
end

I’m happy to hear that you managed to solve it!

The file data.lua is a standard Lua module. Lua modules are really useful for code reuse and most games will use Lua modules at some point!

More info:

2 Likes