Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest WME version: WME 1.9.1 (January 1st, 2010) - download

Author Topic: Create multiple event handlers on the fly  (Read 4842 times)

0 Members and 1 Guest are viewing this topic.

Kaz

  • Arberth Studios
  • Regular poster
  • ***
  • Karma: 1
  • Offline Offline
  • Posts: 228
  • The story is the game
    • View Profile
    • Info on 'Rhiannon' & 'Coven'
Create multiple event handlers on the fly
« 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
Logged
\"Fans of popular horror adventures like Scratches and Barrow Hill should start bracing themselves for another haunting experience, as independent developer Arberth Studios has announced production on its debut title Rhiannon - Curse of the Four Branches.\" - AdventureGamers.com

Azrael

  • Regular poster
  • ***
  • Karma: 9
  • Offline Offline
  • Gender: Male
  • Posts: 155
    • View Profile
    • Mad Orange
Re: Create multiple event handlers on the fly
« Reply #1 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 ;)
« Last Edit: February 20, 2010, 08:08:08 AM by Azrael »
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: Create multiple event handlers on the fly
« Reply #2 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.  
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Re: Create multiple event handlers on the fly
« Reply #3 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


Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

Kaz

  • Arberth Studios
  • Regular poster
  • ***
  • Karma: 1
  • Offline Offline
  • Posts: 228
  • The story is the game
    • View Profile
    • Info on 'Rhiannon' & 'Coven'
Re: Create multiple event handlers on the fly
« Reply #4 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
Logged
\"Fans of popular horror adventures like Scratches and Barrow Hill should start bracing themselves for another haunting experience, as independent developer Arberth Studios has announced production on its debut title Rhiannon - Curse of the Four Branches.\" - AdventureGamers.com
 

Page created in 0.058 seconds with 19 queries.