PDA

View Full Version : dynamic button trouble



Traveler
24-02-2006, 05:03 PM
I have a few problems with the buttons in my game.

Currently I have a class button that looks a bit like the following



TButton = class
private
(..)
FOnClick : TNotifyEvent;
procedure doClick; dynamic;
public
(..)
property OnClick : TNotifyEvent read FOnClick write FOnClick;
end;


I also have a list that hold these buttons.

At initialization I create an x number of buttons


buttonlist.addButton(x, y, width, height, title);


What I need now, is to attach predesigned procedures or functions to these dynamically created buttons.

My first quess was to create a new class that holds all these procedures and then do something like

ButtonList[1].OnClick := btnEvents.changeMouse;
but somehow thats not really working.

I was kinda hoping someone has any thoughts on how to solve this.

Thanks a bunch!

savage
24-02-2006, 05:36 PM
Can you clarify what you mean by it does not work? Does it not work at compile-time or run-time?

Nitrogen
24-02-2006, 06:16 PM
You've got a very 'OOP' way of making your buttons work there..

I'd go a more traditionalist route and use Function Pointers..

Just define:


Type
ClickProc = procedure(x,y: integer);


Or something like that..
Then inside your class:



OnClick: ClickProc


Then you just write a procedure that conforms to the procedure(x,y) format and set it equal to ClickProc... (IIRC of course!)

savage
24-02-2006, 06:23 PM
You've got a very 'OOP' way of making your buttons work there..
I'd go a more traditionalist route and use Function Pointers..


Events are essentially just fancy function pointers the only difference being that you qualify them as being "of object" when you declare them.

Traveler
24-02-2006, 07:59 PM
Can you clarify what you mean by it does not work? Does it not work at compile-time or run-time?

Well, everything does compile. However the moment I click a button, any button, the game simply ends. I went through the code, but there's nothing that should end the game just like that. Weird thing is I'm not getting any errors too.

From your reply I take it I am on the right path though, so at least I'm not doing anything odd, in a oo way that is.

Nitrogen, thanks for the suggestion. Going to give that a try as well.

Traveler
24-02-2006, 08:25 PM
I think you got the wrong thread :)

savage
24-02-2006, 09:02 PM
Ooops deleted.