Page 1 of 1

MakeButton to return ID also when ID is given

Posted: Fri Mar 07, 2025 12:35 pm
by Bugala
To demonstrate:

Code: Select all

ID=2
NewID = MakeButton(ID, #SIMPLEBUTTON, 100, 100, 100, 100, {OnMouseDown = Function() DebugPrint("mouse down") EndFunction})
DebugPrint(NewID)
WaitEvent()
If you change ID to nil, it gives userdata.

I was having in my program a makebutton function, which sends, along the other data, ButtonID, which might be a fixed number, or a NIL.

The program at that point is not supposed to know whether there is a number or NIL, but it is supposed to catch it to Button.ButtonID variable.

Now problem is that if I am using:
Button.ButtonID = MakeButton(...

then if it is sent a fixed number, it fails, since it doesn't return the ButtonID:

Therefore, it would be good if MakeButton would return ButtonID in every case, regardless if it is NIL, userdata, or actual number.

This is also what you would expect from the description code in manual:
[id] = MakeButton(id, #SIMPLEBUTTON, x, y, width, height, t[, userdata])

Re: MakeButton to return ID also when ID is given

Posted: Sat Mar 08, 2025 9:09 am
by emeck
@Bugala
From the description, the returned "id" will only be returned when you pass Nil as the "id" argument to the function. And that is also consistent with other functions like CreateAnim() or CreateBrush() for example.

Using Nil makes use of Hollywood's "private" pool of IDs, which doesn't mix with the numbers you can use to assign to your objects if you need.

The advantage of Nil for IDs is that way you can manage your objects' IDs easier by names instead of numbers when you have a large collection of numbers.

I don't know were the user data you see comes from, but you either use Nil or pass an ID to the object you need to create. I don't think mixing both is a good idea, unless I have misunderstood something.

Regards

Edit: typo

Re: MakeButton to return ID also when ID is given

Posted: Sat Mar 08, 2025 10:46 am
by Flinx
And if you think you really need it in this way, then you could make your own function:

Code: Select all

Function p_MakeButton(ID, ...)
	Local nid=MakeButton(ID, Unpack(arg))
	If IsNil(nid) Then nid=ID
	Return(nid)
EndFunction

ID=2
NewID = p_MakeButton(ID, #SIMPLEBUTTON, 100, 100, 100, 100, {OnMouseDown = Function() DebugPrint("mouse down") EndFunction})
DebugPrint(NewID)
WaitEvent()

Re: MakeButton to return ID also when ID is given

Posted: Sat Mar 08, 2025 11:18 am
by jPV
Flinx wrote: Sat Mar 08, 2025 10:46 am And if you think you really need it in this way, then you could make your own function:
+1

Nice and easy solution, not everything should be offered by Hollywood itself when you can do your own custom features when you need them, that's called "programming" :D

Re: MakeButton to return ID also when ID is given

Posted: Sat Mar 08, 2025 8:29 pm
by Bugala
@flinx

Thanks!
That works and solves my problem of needing to do two different makebuttons.