[SOLVED][1.14.0] - Any Method To Reduce These Long Lua Source Codes For GUI Buttons On Title Scene?

Hi,

Making good progress…

Is there any method to reduce below long Lua source codes for GUI buttons on title scene?
Let me know, thank you!

NOTE: Game is MIT open-source if you would like to see it…

Savant Savior

local globals = require "Global_Variables"
local consts = require "Constants"

local NORMAL_SCALE = vmath.vector3(1.0, 1.0, 1.0)
local PRESSED_SCALE = vmath.vector3(0.9, 0.9, 1.0)
local ANIM_DURATION = 0.1

function init(self)
	msg.post(".", "acquire_input_focus")
	gui.set_render_order(7)
	globals.Button_Clicked = -1
	globals.Button_Animation_Timer = -1
end

function final(self)
end

function update(self, dt)
	if globals.Button_Animation_Timer > -1 then
		globals.Button_Animation_Timer = globals.Button_Animation_Timer - 1
	end

	local button_Start = gui.get_node("GUI_Start")
	if globals.Button_Animation_Timer == 10 and globals.Button_Clicked == 1 then
		gui.animate(button_Start, "scale", NORMAL_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
	end
	if globals.Button_Animation_Timer == 0 and globals.Button_Clicked == 1 then
		globals.Scene_To_Display_Next = consts.Scene_Title
		globals.Scene_Fade_Status = 1
		globals.Scene_Fade_Alpha = 0.0
		sound.play("#SFX_Button_Click")
	end

	local button_Options = gui.get_node("GUI_Options")
	if globals.Button_Animation_Timer == 10 and globals.Button_Clicked == 2 then
		gui.animate(button_Options, "scale", NORMAL_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
	end
	if globals.Button_Animation_Timer == 0 and globals.Button_Clicked == 2 then
		globals.Scene_To_Display_Next = consts.Scene_Title
		globals.Scene_Fade_Status = 1
		globals.Scene_Fade_Alpha = 0.0
		sound.play("#SFX_Button_Click")
	end

	local button_How_To_Play = gui.get_node("GUI_How_To_Play")
	if globals.Button_Animation_Timer == 10 and globals.Button_Clicked == 3 then
		gui.animate(button_How_To_Play, "scale", NORMAL_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
	end
	if globals.Button_Animation_Timer == 0 and globals.Button_Clicked == 3 then
		globals.Scene_To_Display_Next = consts.Scene_Title
		globals.Scene_Fade_Status = 1
		globals.Scene_Fade_Alpha = 0.0
		sound.play("#SFX_Button_Click")
	end

	local button_High_Scores = gui.get_node("GUI_High_Scores")
	if globals.Button_Animation_Timer == 10 and globals.Button_Clicked == 4 then
		gui.animate(button_High_Scores, "scale", NORMAL_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
	end
	if globals.Button_Animation_Timer == 0 and globals.Button_Clicked == 4 then
		globals.Scene_To_Display_Next = consts.Scene_Title
		globals.Scene_Fade_Status = 1
		globals.Scene_Fade_Alpha = 0.0
		sound.play("#SFX_Button_Click")
	end

	local button_About = gui.get_node("GUI_About")
	if globals.Button_Animation_Timer == 10 and globals.Button_Clicked == 5 then
		gui.animate(button_About, "scale", NORMAL_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
	end
	if globals.Button_Animation_Timer == 0 and globals.Button_Clicked == 5 then
		globals.Scene_To_Display_Next = consts.Scene_Title
		globals.Scene_Fade_Status = 1
		globals.Scene_Fade_Alpha = 0.0
		sound.play("#SFX_Button_Click")
	end

	local button_Exit = gui.get_node("GUI_Exit")
	if globals.Button_Animation_Timer == 10 and globals.Button_Clicked == 6 then
		gui.animate(button_Exit, "scale", NORMAL_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
	end
	if globals.Button_Animation_Timer == 0 and globals.Button_Clicked == 6 then
		sound.play("#SFX_Button_Click")
		if globals.Platform.system_name ~= "HTML5" then
			msg.post("@system:", "exit", {code = 0})
		else
			globals.Scene_To_Display_Next = consts.Scene_Title
			globals.Scene_Fade_Status = 1
			globals.Scene_Fade_Alpha = 0.0
		end
	end

	local button_BGM_Test = gui.get_node("GUI_BGM_Test")
	if globals.Button_Animation_Timer == 10 and globals.Button_Clicked == 7 then
		gui.animate(button_BGM_Test, "scale", NORMAL_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
	end
	if globals.Button_Animation_Timer == 0 and globals.Button_Clicked == 7 then
		globals.Scene_To_Display_Next = consts.Scene_Title
		globals.Scene_Fade_Status = 1
		globals.Scene_Fade_Alpha = 0.0
		sound.play("#SFX_Button_Click")
	end
end

function on_message(self, message_id, message, sender)
end

function on_input(self, action_id, action)
	if action_id == hash("touch") then
		local button_Start = gui.get_node("GUI_Start")
		if action.pressed and globals.Button_Clicked == -1 then
			if gui.pick_node(button_Start, action.x, action.y) then
				gui.animate(button_Start, "scale", PRESSED_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
				globals.Button_Animation_Timer = 20
				globals.Button_Clicked = 1
			end
		end
		local button_Options = gui.get_node("GUI_Options")
		if action.pressed and globals.Button_Clicked == -1 then
			if gui.pick_node(button_Options, action.x, action.y) then
				gui.animate(button_Options, "scale", PRESSED_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
				globals.Button_Animation_Timer = 20
				globals.Button_Clicked = 2
			end
		end
		local button_How_To_Play = gui.get_node("GUI_How_To_Play")
		if action.pressed and globals.Button_Clicked == -1 then
			if gui.pick_node(button_How_To_Play, action.x, action.y) then
				gui.animate(button_How_To_Play, "scale", PRESSED_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
				globals.Button_Animation_Timer = 20
				globals.Button_Clicked = 3
			end
		end
		local button_High_Scores = gui.get_node("GUI_High_Scores")
		if action.pressed and globals.Button_Clicked == -1 then
			if gui.pick_node(button_High_Scores, action.x, action.y) then
				gui.animate(button_High_Scores, "scale", PRESSED_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
				globals.Button_Animation_Timer = 20
				globals.Button_Clicked = 4
			end
		end
		local button_About = gui.get_node("GUI_About")
		if action.pressed and globals.Button_Clicked == -1 then
			if gui.pick_node(button_About, action.x, action.y) then
				gui.animate(button_About, "scale", PRESSED_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
				globals.Button_Animation_Timer = 20
				globals.Button_Clicked = 5
			end
		end
		local button_Exit = gui.get_node("GUI_Exit")
		if action.pressed and globals.Button_Clicked == -1 then
			if gui.pick_node(button_Exit, action.x, action.y) then
				gui.animate(button_Exit, "scale", PRESSED_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
				globals.Button_Animation_Timer = 20
				globals.Button_Clicked = 6
			end
		end
		local button_BGM_Test = gui.get_node("GUI_BGM_Test")
		if action.pressed and globals.Button_Clicked == -1 then
			if gui.pick_node(button_BGM_Test, action.x, action.y) then
				gui.animate(button_BGM_Test, "scale", PRESSED_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
				globals.Button_Animation_Timer = 20
				globals.Button_Clicked = 7
			end
		end
	end
end

function on_reload(self)
end

https://teamjezxlee.itch.io/project0-defold

Hi,

I solved this with a Lua array of button names, please see code below

Savant Savior

local globals = require "Global_Variables"
local consts = require "Constants"

local NORMAL_SCALE = vmath.vector3(1.0, 1.0, 1.0)
local PRESSED_SCALE = vmath.vector3(0.9, 0.9, 1.0)
local ANIM_DURATION = 0.1

local button_Names = {"GUI_Start", "GUI_Options", "GUI_How_To_Play", "GUI_High_Scores", "GUI_About", "GUI_Exit", "GUI_BGM_Test"}

function init(self)
	msg.post(".", "acquire_input_focus")
	gui.set_render_order(7)
	globals.Button_Clicked = -1
	globals.Button_Animation_Timer = -1
end

function final(self)
end

function update(self, dt)
	if globals.Button_Animation_Timer > -1 then
		globals.Button_Animation_Timer = globals.Button_Animation_Timer - 1
	end

	for index = 1, 7 do
		local button = gui.get_node(button_Names[index])

		if globals.Button_Animation_Timer == 10 and globals.Button_Clicked == index then
			gui.animate(button, "scale", NORMAL_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
		end
		if globals.Button_Animation_Timer == 0 and globals.Button_Clicked == index then
			sound.play("#SFX_Button_Click")
			globals.Scene_To_Display_Next = consts.Scene_Title
			globals.Scene_Fade_Status = 1
			globals.Scene_Fade_Alpha = 0.0

			if index == 6 then
				if globals.Platform.system_name ~= "HTML5" then
					msg.post("@system:", "exit", {code = 0})
				end
			end
		end
	end
end

function on_message(self, message_id, message, sender)
end

function on_input(self, action_id, action)
	if action_id == hash("touch") then
		for index = 1, 7 do
			local button = gui.get_node(button_Names[index])

			if action.pressed and globals.Button_Clicked == -1 then
				if gui.pick_node(button, action.x, action.y) then
					gui.animate(button, "scale", PRESSED_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
					globals.Button_Animation_Timer = 20
					globals.Button_Clicked = index
				end
			end
		end
	end
end

function on_reload(self)
end