Can I Attach Fixed variable result to Makebutton Function.

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

Can I Attach Fixed variable result to Makebutton Function.

Post by Bugala »

Code: Select all

I might want to do soemthing like this:

for local n = 1 to 5

MakeButton(n, #SIMPLEBUTTON, 1, 1, 200, 200, {OnMouseUp = Function () DebugPrint(""..n) EndFunction})

next

n=7

WaitEvent
These buttons are all in same location actually, but suppose they in different locations.

What I would now like to happen is that when I click Button number 1, it would debugprint: 1.
When I would click on Button number 2, it would debugprint: 2.

However, in reality, regardless which button I press, it would result in Debugprintt: 7.

But is there a way for me to actually get that Debugprints "n" to become a fixed number instead of a variable?
Flinx
Posts: 192
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Can I Attach Fixed variable result to Makebutton Function.

Post by Flinx »

Why don't you use the structure as given in the examples of the manual? I am surprised that there is any output at all.
Here is a working example

Code: Select all

Function p_EventFunction(msg)
	DebugPrint(msg.id, msg.action)
EndFunction

For Local n = 1 To 5
	Box(n*70, n*70, 100, 100, 50+n*40)
	MakeButton(n, #SIMPLEBUTTON, n*70, n*70, 200, 200, {OnMouseUp = p_EventFunction})
Next

WaitEvent
Flinx
Posts: 192
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Can I Attach Fixed variable result to Makebutton Function.

Post by Flinx »

Ah, now I found this syntax in the description of the callback functions, tried to build a string argument for DebugPrint and understand why you asked. Apparently, a variable specified directly in the argument list of MakeButton() is considered as reference to the variable, and therefore the output is always the content of the variable at the time of the event. I guess you can't do it the way you wanted.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Can I Attach Fixed variable result to Makebutton Function.

Post by Bugala »

Thanks Flinx!

Although you didnt provide me with a solution, your code reminded me of the "UserData" option that Makebuttons (and many others) have, and that fixed the problem:

Code: Select all

n=100

MakeButton(1, #SIMPLEBUTTON, 1, 1, 200, 200, {OnMouseUp = Function (userdata) 
		DebugPrint(userdata.userdata.var)
		EndFunction}, 
		{var=n})

n=50
	
WaitEvent
Here you can see that I am assigning "n" to the userdatas "var" variable, and even I change "n" later to 50, it still gives me "100" when I click it on WaitEvent.
Post Reply