Calling a function from inside a table inside a table

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
fkinCURRY
Prole
Posts: 4
Joined: Thu Nov 29, 2018 12:31 pm

Calling a function from inside a table inside a table

Post by fkinCURRY »

Hey, I got stuck on a kinda tricky problem (atleast for me, since I never really used functions inside of tables)

So I have tables called windowfunc and buttonfunc, buttonfunc is stored inside of windowfunc.
There is windowfunc[1] and windowfunc[2], buttonfunc[1-3] in each.

How can I call a function from here? I tried windowfunc[1]:buttonfunc[1]() and I tried windowfunc[1]:buttonfunc[1] but it crashes with an error "function arguments expeced near '[' ".

Souce code is zipped, does not contain the line that tries to execute the button function.
Controls: Arrows, Z/Y, X, ESC and ;

Example (actually code from the game I'm trying to make):

Code: Select all


-- loadscreen() runs in update loop
--there's also for loops in the draw function to draw the buttons on screen

function loadscreen()
	--int or clear window or button arrays
	window = {}
	windowfunc = {}
	
	button = {}
	buttonfunc = {}
		
		--Empty button
		button.empty = function() end
		
		--button.name = { " Name ", x, y, ">SelectName<" }
		--button.namefunc = function
		button.start = { " Start ", 10, 10, ">Start<" }
		button.startfunc = function()
			triangle(10)
		end
		
		button.reset = { " Reset ", 10, 20, ">Reset<" }
		button.resetfunc = function()
			reload( false )
		end
		
		button.num = { number, 10, 30, ">"..number.."<" }
		
		button.image = { " Image ", 10, 40, ">Image<" }
		button.imagefunc = function()
			if imagetoggle then
				imagetoggle = false
			else
				imagetoggle = true
			end	
		end
		
		--insert buttons into button array
		--Make sure that indexs of button.name and button.namefunc match
		table.insert( button, button.start )
		table.insert( button, button.reset )
		table.insert( button, button.num )
		table.insert( button, button.image )
		
		--insert button functions into buttonfunc array
		table.insert( buttonfunc, button.startfunc )
		table.insert( buttonfunc, button.resetfunc )
		table.insert( buttonfunc, button.empty )
		table.insert( buttonfunc, button.imagefunc )
		
		--put buttons into their window
		table.insert( window, button )
		table.insert( windowfunc, buttonfunc )
		
	button = {}
	buttonfunc = {}
	
		button.load = { " Load ", 80, 10, ">Load<" }
		button.loadfunc = function()
			load()
		end
		
		button.add = { " Add ", 80, 20, ">Add<" }
		button.addfunc = function()
			if number < 99 then
				number = number + 1
			elseif number == 99 then
				number = 1
			end
		end
		
		button.sub	= { " Sub ", 80, 30, ">Sub<" }
		button.subfunc = function()
			if number > 1 then
				number = number - 1
			elseif number == 1 then
				number = 99
			end
		end
		
		table.insert( button, button.load )
		table.insert( button, button.add)
		table.insert( button, button.sub)
		
		table.insert( buttonfunc, button.loadfunc )
		table.insert( buttonfunc, button.addfunc)
		table.insert( buttonfunc, button.subfunc)
		
		table.insert( window, button )
		table.insert( windowfunc, buttonfunc )
end

function love.keypressed( k )

	--other keys to navigate in the windows/buttons
	
	if k == "space"  then
		--calls function in windowfunc[cursor.window] buttonfunc[cursor.pos]
	end
	
end
Attachments
game.zip
(5.76 MiB) Downloaded 116 times
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Calling a function from inside a table inside a table

Post by grump »

Code: Select all

    local buttons = windowfunc[cursorwin]
    local buttonfunc = buttons[cursorpos]
    buttonfunc()

    -- or the equivalent brief version
    windowfunc[cursorwin][cursorpos]()
fkinCURRY
Prole
Posts: 4
Joined: Thu Nov 29, 2018 12:31 pm

Re: Calling a function from inside a table inside a table

Post by fkinCURRY »

Works perfectly, thanks alot!
Post Reply

Who is online

Users browsing this forum: darkfrei, dusoft and 83 guests