Wintermute Engine Forum

Wintermute Engine => Technical forum => Topic started by: Kaz on February 19, 2010, 09:23:37 PM

Title: Create multiple event handlers on the fly
Post by: Kaz on February 19, 2010, 09:23:37 PM
Hi all

I'm running a window that uses CreateButton() to create 150 buttons on demand. That bit's ok, a for() loop creates the button names and positions.

What I don't get though is how to then in the same routine, to create code to be run when those buttons are pressed - Metamorphium hinted tantalisingly at it in a previous post, with the enigmatic
"insertButton will as a parameter take name of the button (which will be further referenced in script as on "" event)"

but I can't find any documentation that explains how that 'on "" event' is created and where it gets its code from.

I tried ApplyEvent, but that doesn't seem to do it - in any case I can't see how ApplyEvent could create 150 different code sequences.

Any ideas?

Thanks
Title: Re: Create multiple event handlers on the fly
Post by: Azrael on February 20, 2010, 08:05:38 AM
I think you have two way:

1) setting on "NameButton" for each script on the main window's script

Code: WME Script
  1. on "Button1"
  2.  {
  3.  Do something
  4.  }
  5.  
  6. on "Button2"
  7.  {
  8.  Do something
  9.  }
  10.  
  11. on "Button3"
  12.  {
  13.  Do something
  14.  }
  15.  
  16. ...
And so on for all the 150 buttons.

2) create a single script and attach it (with AttachScript method) to each button when you create them. In this case you should use the button name to know what button is pressed, something like this:

Code: WME Script
  1. on "LeftClick"
  2.  {
  3.  var ButtonPressed = this.Name//Name of the button pressed
  4.  
  5.  Do something....
  6.  }

The first way it's a little boring because you have to create all the 150 button's script on the main window script, but allow you to create unique script for each button.

The second way it's a lot less expensive, but you have to manage all the buttons with one script.

So it depends on what you have to do ;)
Title: Re: Create multiple event handlers on the fly
Post by: Mnemonic on February 20, 2010, 09:38:54 AM
2) create a single script and attach it (with AttachScript method) to each button when you create them. In this case you should use the button name to know what button is pressed, something like this:

Code: WME Script
  1. on "LeftClick"
  2.  {
  3.  var ButtonPressed = this.Name//Name of the button pressed
  4.  
  5.  Do something....
  6.  }

You can also use the "Press" event for buttons, instead of LeftClick.


The second way it's a lot less expensive, but you have to manage all the buttons with one script.

You can, however, route the button events to their parent window. So button's script might look like this:

Code: WME Script
  1. on "Press"
  2. {
  3.   var parentWin = this.Parent;
  4.   parentWin.OnButtonPressed(this.Name);
  5. }
  6.  

And window's script would contain a single method for handling all buttons:

Code: WME Script
  1. method OnButtonPressed(buttonName)
  2. {
  3.   Game.Msg("Player pressed a button called: " + buttonName);
  4. }
  5.  
Title: Re: Create multiple event handlers on the fly
Post by: metamorphium on February 20, 2010, 09:40:00 AM
you can also dynamically attach various scripts to your buttons:

Code: WME Script
  1. for (var a=0;a<150;a=a+1)
  2. {
  3.    var but = newWindow.CreateButton("Button"+a);
  4.    but.AttachScript("buttons\script"+a+".script);
  5. }
  6.  

150 buttons doing entirely different job sounds scary though. Are you doing Nokia N900 simulator? :D


Title: Re: Create multiple event handlers on the fly
Post by: Kaz on February 20, 2010, 03:55:10 PM
Hello chaps

The Attachscript method worked like a charm. The buttons all have to do the same thing to themselves, there's no actual difference between them, so one script can handle them all.

The puzzle is based on cells in a matrix, changing their contents. Cracked it. Thanks for your help.

Cheers