Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-29-2007, 01:48 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
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.
__________________

Last edited by Inverness; 12-29-2007 at 02:21 PM..
Reply With Quote
  #2  
Old 12-29-2007, 03:35 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
So this replicates catchevent? Nice work, but seems like overkill. I thought you could use catchevent with other things, such as requesturl.
__________________
Reply With Quote
  #3  
Old 12-30-2007, 01:51 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
This replicates the functionality with stuff you script yourself, not just built-in Graal stuff.
__________________
Reply With Quote
  #4  
Old 12-30-2007, 02:05 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Works for me. Good job.
__________________
Reply With Quote
  #5  
Old 12-31-2007, 06:36 AM
Kyranki Kyranki is offline
Freelance Coder
Join Date: Aug 2007
Location: At the end of the rainbow, in the pot of gold.
Posts: 202
Kyranki is on a distinguished road
Send a message via AIM to Kyranki Send a message via MSN to Kyranki
EDIT:

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

Last edited by Kyranki; 12-31-2007 at 08:30 AM..
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 06:11 AM.


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