Implementation Help

I for the life of me cannot figure out why when switching months my buttons do not clone to the next month. Any help would be appreciated.

This script is over 500 lines, so ill include the relevant variables and functions here.
If you would like to see the full script:Click Here

Variables:

local DATEYEAR = os.date("%Y")
local DATEMONTH = os.date("%m")
local CALYEAR = 2025
local CALMONTH = tonumber(os.date("%m"))
local CALENDARSTATE = {}
local APPOINTMENTS = {}
local cloned_buttons = {}
local cloned_texts = {}

local MERGED_APPOINTMENTS = {}  -- Table to store split/merged appointments

local SAVEDAPPOINTMENTS = {}

changemonth():

local function changemonth(direction)
	save_calendar_state()
	
	-- Change month
	CALMONTH = CALMONTH + direction
	if CALMONTH < 1 then
		CALMONTH = 12
		CALYEAR = CALYEAR - 1
	elseif CALMONTH > 12 then
		CALMONTH = 1
		CALYEAR = CALYEAR + 1
	end

	local num_days = get_num_days(CALYEAR, CALMONTH)
	local start_time = os.time({year = CALYEAR, month = CALMONTH, day = 1})
	local start_wday = tonumber(os.date("%w", start_time))

	updatecalendardays(num_days, start_wday)
	clone_day_buttons() 
	load_calendar_state()
end

clone_day_buttons():

local function clone_day_buttons()
	local num_days = get_num_days(CALYEAR, CALMONTH)
	local start_time = os.time({year = CALYEAR, month = CALMONTH, day = 1})
	local start_wday = tonumber(os.date("%w", start_time))

	-- Clear existing buttons
	for _, button in pairs(cloned_buttons) do
		gui.delete_node(button)
	end
	cloned_buttons = {} -- Reset to an empty table

	for i = start_wday, num_days + start_wday do

		local day_container = gui.get_node("ui_day" .. i)
		if day_container then
			local cloned_button = gui.clone(gui.get_node("button_day"))

			-- Set unique ID
			local new_id = i
			gui.set_id(cloned_button, new_id)

			-- Set text to match the corresponding day
			gui.set_text(cloned_button, tostring(i))

			-- Parent button to the correct day container
			gui.set_parent(cloned_button, day_container, false)

			-- Position the button inside the container
			gui.set_position(cloned_button, vmath.vector3(61, 25, 0))

			-- Store in table with specific ID
			cloned_buttons[new_id] = cloned_button
			-- pprint(cloned_buttons)

		else
			print("Warning: Missing container for day " .. i)
		end
	end
	gui.set_enabled(gui.get_node("button_day"), "disable")
	gui.set_visible(gui.get_node("button_day"), false)
	-- pprint(cloned_buttons)
end

Thank you in advance. Also, if you look at the full script any tips you have would be well received. This is my first defold project and i’m learning lua as i go along.

After a quick look I noticed this: gui.set_enabled(gui.get_node("button_day"), "disable"). It should be false instead of "disable". My eyes didn’t catch anything else wrong and it sounds strange that it works on the init but not when changing the month, if I understood correctly.

Omg, thank you. I know what the issue is after you pointed out that the button is disabled. I am cloning a disabled button on changemonth. I need to re enable button_day at the beginning of the clone_day_buttons function then clone the buttons.

So the updated function should look like this

local function clone_day_buttons()
	local num_days = get_num_days(CALYEAR, CALMONTH)
	local start_time = os.time({year = CALYEAR, month = CALMONTH, day = 1})
	local start_wday = tonumber(os.date("%w", start_time))
gui.set_enabled(gui.get_node("button_day"), true)
gui.set_visible(gui.get_node("button_day"), true)

	-- Clear existing buttons
	for _, button in pairs(cloned_buttons) do
		gui.delete_node(button)
	end
	cloned_buttons = {} -- Reset to an empty table

	for i = start_wday, num_days + start_wday do

		local day_container = gui.get_node("ui_day" .. i)
		if day_container then
			local cloned_button = gui.clone(gui.get_node("button_day"))

			-- Set unique ID
			local new_id = i
			gui.set_id(cloned_button, new_id)

			-- Set text to match the corresponding day
			gui.set_text(cloned_button, tostring(i))

			-- Parent button to the correct day container
			gui.set_parent(cloned_button, day_container, false)

			-- Position the button inside the container
			gui.set_position(cloned_button, vmath.vector3(61, 25, 0))

			-- Store in table with specific ID
			cloned_buttons[new_id] = cloned_button
			-- pprint(cloned_buttons)

		else
			print("Warning: Missing container for day " .. i)
		end
	end
	gui.set_enabled(gui.get_node("button_day"), false)
	gui.set_visible(gui.get_node("button_day"), false)
	-- pprint(cloned_buttons)
end

Aah, yes of course! I have done that once or twice myself now that you said it. :smile:

1 Like

I spent 4 hours yesterday trying to tweak and figure it out and i did it in 30 seconds after i woke up this morning with your help.

2 Likes