Is this a bug?

BlitzPlus Forums/BlitzPlus Beginners Area/Is this a bug?

A button, as well as a window, a canvas, image, timer etc. etc. is just a (int) variable. They can be declared locally in a function, but if you want it to work, then it must be within a loop where there's a WaitEvent() command.

In this case the main repeat-until loop is the mainloop containing the WaitEvent, the function call gives the button as argument so that the function knows what it's supposed to be

window=CreateWindow("oO",0,0,640,480)

button=CreateButton("x",0,0,32,32,window)

Repeat
	WaitEvent()
	If EventID()=$803 quit=1
	
	dostuff button
Until quit
End

Function dostuff(b)
	If EventID()=$401
		If EventSource()=b
			Notify "yay!"
		EndIf
	EndIf
End Function



another option: the whole creation + mainloop in a function
dostuff
End

Function dostuff()
	window=CreateWindow("oO",0,0,640,480)
	
	button=CreateButton("x",0,0,32,32,window)

	Repeat
		WaitEvent()
		If EventID()=$803 quit=1
		If EventID()=$401
			If EventSource()=button
				Notify "yay!"
			EndIf
		EndIf
		
	Until quit
	
	FreeGadget button
	FreeGadget window

End Function



here's how I've done these things for years:
window=CreateWindow("bank demo",0,0,640,480)

bank=createstuff(window)

Repeat
	WaitEvent()
	If EventID()=$803 quit=1
	dostuff bank
Until quit

End


Function createstuff(window)
	bank=CreateBank(16)
	
	PokeInt bank,0 ,CreateButton("1",0,0,32,32,window)
	PokeInt bank,4 ,CreateButton("2",32,0,32,32,window)
	PokeInt bank,8, CreateButton("3",0,32,32,32,window)
	PokeInt bank,12,CreateButton("4",32,32,32,32,window)
	
	Return bank
End Function

Function dostuff(b)
	button1=PeekInt(b,0)
	button2=PeekInt(b,4)
	button3=PeekInt(b,8)
	button4=PeekInt(b,12)
	
	If EventID()=$401
		Select EventSource()
			Case button1 Notify "1 monkey"
			Case button2 Notify "2 giraffe"
			Case button3 Notify "3 lions"
			Case button4 Notify "4 dogs"
		End Select
	EndIf
End Function


This bank stuff might appear tricky, but it's just memory you can fill with whatever. And since a gadget is just an int, and since you can store ints in banks, you can store gadgets in banks. This is quite a good method for datahiding. As you see there are no globals, and yet the functions do all the work.