Graal Forums  

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

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #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
 


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 08:48 AM.


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