Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Event Binding (Catching) (https://forums.graalonline.com/forums/showthread.php?t=78120)

Inverness 12-29-2007 01:48 PM

Event Binding (Catching)
 
After lamenting over the fact that catchevent() did not function beyond GuiControls, I decided to throw together this class to replicate the functionality of catchevent().

This class when joined to an object will allow other objects to bind events so that when triggerboundevent() is called in the class object it is triggered on the objects bound to it; using the same event name or a different one.

The object you join the class to needs to have an object name or the variable this.bindname set so it can properly track the objects that have bound its event. This is also using a gloval variable 'static' which is a TStaticVar. I created this TStaticVar to organize 'global' variables that would be corrupting by being viewed in client flags or server flags (object refs and huge arrays).

PHP Code:

public function bind(eventnameobjectnewname) {
  if (
this.name == null && this.bindname == null)
    return;
  if (
object.type() != 2)
    return;
  
temp.bn = (this.bindname == nullthis.namethis.bindname);
  
this.bind2(temp.bneventnameobjectnewname);
}
public function 
bind2(bneventnameobjectnewname) {
  if (static.(@ 
"binds_" bn "_" eventname).index(object) < 0) {
    static.(@ 
"binds_" bn "_" eventname).add(object);
    if (
newname == null)
      
newname eventname;
    static.(@ 
"binds2_" bn "_" eventname).add(newname);
  }
}
public function 
unbind(eventnameobject) {
  if (
this.name == null && this.bindname == null)
    return;
  if (
object.type() != 2)
    return;
  
temp.bn = (this.bindname == nullthis.namethis.bindname);
  
this.unbind2(temp.bneventnameobject);
}
public function 
unbind2(bneventnameobject) {
  
temp.ix = static.(@ "binds_" bn "_" eventname).index(object);
  if (
temp.ix > -1) {
    static.(@ 
"binds_" bn "_" eventname).delete(temp.ix);
    static.(@ 
"binds2_" bn "_" eventname).delete(temp.ix);
  }
}
public function 
bindgc() {
  if (
this.name == null && this.bindname == null)
    return;
  
temp.bn = (this.bindname == nullthis.namethis.bindname);
  
this.bindgc2(temp.bn);
}
public function 
bindgc2(bn) {
  
temp.keys 0;
  
with (static) {
    
temp.keys getstringkeys("this.binds_" bn "_");
  }
  for (
temp.eventnametemp.keys) {
    for (
temp.= static.(@ "binds_" bn "_" eventname).size() - 1> -1--) {
      if (static.(@ 
"binds_" bn "_" eventname)[i].type() != 2) {
        static.(@ 
"binds_" bn "_" eventname).delete(i);
        static.(@ 
"binds2_" bn "_" eventname).delete(i);
      }
    }
  }
}
function 
onBindGarbageCollect() {
  
this.bindgc();
}
function 
triggerboundevent(eventnamep0p1p2p3p4p5) {
  if (
this.name == null && this.bindname == null)
    return;
  
temp.bn = (this.bindname == nullthis.namethis.bindname);
  
this.triggerboundevent2(temp.bneventnamep0p1p2p3p4p5);
}
function 
triggerboundevent2(bneventnamep0p1p2p3p4p5) {
  for (
temp.0< static.(@ "binds_" bn "_" eventname).size(); ++) {
    if (static.(@ 
"binds_" bn "_" eventname)[i].type() == 2) {
      static.(@ 
"binds_" bn "_" eventname)[i].trigger(static.(@ "binds2_" temp.bn "_" eventname)[i], p0p1p2p3p4p5);
    }
  }


I had split each function into two parts in the case that the object you want to call bind on isn't existing yet.
Here are two examples of how I use it (with most of script cut out):
PHP Code:

// WEAPON: System
//#CLIENTSIDE
function onPlayerEnters() {
  
// Stuff
  
worldCheck();
}
function 
onWorldCheck() {
  
// Check to see if you're in a different world
  
triggerboundevent("WorldCheck");
  if (
yourindifferentworld) {
    
// World Change Functions
    
triggerboundevent("WorldChange");
  }
}

// WEAPON: SysTime
//#CLIENTSIDE
function onCreated() {
  
// stuff
  
System.bind("WorldCheck"this);
  
System.bind("WorldChange"this);
  
setTimer(0.05);
}
function 
onWorldCheck() onTimeout();
function 
onWorldChange() addmsg(5"You are now in " client.world);
function 
onTimeout() {
  
// Apply Day/Night effect depending on world
  // If the world does not have day/night enabled, apply the seteffect()
  
setTimer(5);
}

// WEAPON: GuiInventory
//#CLIENTSIDE
function onCreated() {
  
// Stuff
  
System.bind2("MudObject_player_" player.account"ReflectedObject"this);
}
function 
onReflectedObject() {
  
// New mud data from server, update the inventory view.
}

// OBJECT: MudObject_player_YourAccount
//#CLIENTSIDE
function onReflect(variables) {
  
// Set the variables
  
triggerboundevent("ReflectedObject");


You'll also notice that the class defines an event onBindGarbageCollect(), this is because every 60 seconds in the System weapon bindgc() is triggered to clear any objects that might have been destroyed from the bind array, after that is has triggerboundevent("BindGarbageCollect").

This is so any objects that are using the eventbind class besides System can bind that event so bindgc() is run for them automatically:
PHP Code:

//#CLIENTSIDE
function onCreated() {
  
join("util_eventbind"); // The class
  
System.bind("BindGarbageCollect"this); // No need for separate scheduling.


Obviously, one has to make adjustments so it will fit on their server.

cbk1994 12-29-2007 03:35 PM

So this replicates catchevent? Nice work, but seems like overkill. I thought you could use catchevent with other things, such as requesturl.

Inverness 12-30-2007 01:51 AM

This replicates the functionality with stuff you script yourself, not just built-in Graal stuff.

cbk1994 12-30-2007 02:05 AM

Works for me. Good job.

Kyranki 12-31-2007 06:36 AM

EDIT:

Nevermind, understand it completely took a bit of slow reading is all. Nice work Inver, ++ rep for you.


All times are GMT +2. The time now is 06:31 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.