A way to send a table to a button when taking functions from a table list

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1181
Joined: Sun Feb 14, 2010 7:11 pm

A way to send a table to a button when taking functions from a table list

Post by Bugala »

To demonstrate my problem that I am trying to figure out a solution, here is some code:

Code: Select all

TempVar = "something"

Connectors = {}
For n=0 To 5
	Connectors[n] = {var}
Next

AllButtons = {
	[0] = {text="button 1", Func = {OnMouseUp = Function() DebugPrint("Button 1 Clicked") EndFunction} },
	[1] = {text="button 2", Func = {OnMouseUp = Function() DebugPrint("Button 2 Clicked") EndFunction} },
	[2] = {text="button 3", Func = {OnMouseUp = Function()
					CertainConnectorThatWasSentHere.Var = TempVar
						    EndFunction} }
	     }

OtherButtons = {
	[0] = {text="Other button 1", Func = {OnMouseUp = Function() DebugPrint("Other Button 1 Clicked") EndFunction} },
	[1] = {text="Other button 2", Func = {OnMouseUp = Function() DebugPrint("Other Button 2 Clicked") EndFunction} },
	[2] = {text="Other button 3", Func = {OnMouseUp = Function() DebugPrint("Other Button 3 Clicked") EndFunction} }
	     }



CurrentButtons = {}

Function MakeButtonBox(X, Y, W, H, Text, Func)
	SetFillStyle(#FILLCOLOR)
	Box(X, Y, W, H, #BLACK)
	SetFillStyle(#FILLNONE)
	Box(X, Y, W, H, #WHITE, {thickness=3})
	SetFont(#SANS, 20)
	TextOut(X, Y, Text, {color=#WHITE})
	MakeButton(Nil, #SIMPLEBUTTON, X, Y, W, H, Func)
EndFunction

Function Update()
	
	For n = 0 To TableItems(CurrentButtons)-1
		MakeButtonBox(100, 100 + (40 * n), 200, 40, CurrentButtons[n].text, CurrentButtons[n].Func)
	Next
EndFunction	

CurrentButtons = AllButtons
Update()

Repeat
	WaitEvent()
Forever
Idea is that I have this list of ALLBUTTONS, which contains info of all the buttons. Idea in this being that I can change the list of buttons to be created, where the idea is that I click on one option, and it brings more options.

As an example, there could be button "Change Room" and then it would replace the Current Buttons list with buttons that would contain numbers of rooms, so I can first click on "Change Room" and then on the number of the room. After that I can again change the CurrentButtons list to the default ALLBUTTONS list.

However, while this system is otherwise very nice, I am now having one problem I am trying to figure out a good solution for.

For one of the buttons is such, that it requires to be sent a table.

I have made here this CONNECTORS table, which contains in this example 6 connectors (0-5).

Idea is that when I click on one of these connectors, lets say, connector number 2, then it will open this list of buttons. And when I click on that Button number 3, that one is trying to save to this (in this case) connector number 2 the TEMPVAR.

Problem is in how do I send this Connector into this Button func. I am mainly thinking of sending it somehow as userdata, but problem is in how do I send this specific Connector as userdata, and only to this button.

For thing is, of course I can send everyone of these this same Connector as userdata, and solve the problem that way for this button. But what is some other button requires some different userdata than the connector, then how do I solve this problem then?
Post Reply