Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 04-15-2007, 02:44 AM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
communicator-tree class

PHP Code:
/*********************************
(+) addControl( obj ) returns BOOL
  - adds sub-controller
  - MUST be in class "communicator-tree"

(+) remControl( obj ) returns BOOL
  - removes sub-controller

(+) getControls( ctype ) returns controls[];
  - Returns array of all objects with this.ctype == ctype
  - Recursive ( applies to sub-controllers, and thenforth )

(-) removeEmpty() returns BOOL
  - Cleans up controls list of empty objects (object.type() != 2 )

( ) onEvent( event, param1, param2, param3, ... )
  - called through obj.trigger( "Event", param1, param2, param3 );
  - Is recursive
  - to override, do this:
    function onEvent()
      {
      // Stuff
      controller::onEvent( params[0], ..., params[n] );
      }

( ) onEvent[EventName]( params )
  - Event Called when onEvent( EventName, params ) is.
  - no default.

( ) onDestroy()
  - SHOULD BE USED INSTEAD OF obj.destroy();
  - called to destroy object

( ) onParentDestroy()
  - Event Called when Parent Destroyed
  - default:
    if other parent, stay alive, else
      call Destroy
  - to override, do this:
    function onParentDestroyed( obj )
      {
      this.parentControls.remove( obj );
      return;
      }
*********************************/

public function addControlobj )
  {
  if ( ! 
obj.isinclass"communicator-tree" ) )
    {
    echo( 
obj.id SPC "is not a communicator-tree.");
    return 
false;
    }

  if ( 
this.controls.indexobj ) == -)
    
this.controls.addobj );
  if ( 
obj.parentControls.indexthis ) == -)
    
obj.parentControls.add(this);

  return 
true;
  }

public function 
remControlobj )
  {
  
obj.parentControls.removethis );
  
this.controls.removeobj );
  return 
true;
  }

public function 
getControlsctype )
  { 
// Searched entire directory
  
removeEmpty(); // Clean Up
  
for ( cthis.controls )
    {
    
temp.arr c.getControlsctype );
    if ( 
temp.arr.type() == )
      
temp.array.addarraytemp.arr );
    }

  if ( 
this.ctype == ctype )
    
temp.array.addthis );

  return 
temp.array;
  }

function 
onEvent()
  { 
// Recursive
  
param params;
  
removeEmpty(); // Clean up!
  
this.trigger"Event"param[0], param[1] );
  for ( 
cthis.controls )
    
c.trigger"Event"param );
  }

function 
onDestroy()
  {
  
removeEmpty(); // Clean up!
  
for ( cthis.parentControls )
    
c.remControlthis );
  for ( 
cthis.controls )
    
c.trigger"ParentDestroyed"this );

  
this.destroy();
  }

function 
onParentDestroyedthis )
  {
  
removeEmpty();
  
this.parentControls.removethis );

  
// No parent? Destroy itself!
  
if ( this.parentControls.size() == )
    
this.trigger"Destroy""" );
  }

function 
removeEmpty()
  {
  for ( 
0this.controls.size(); ++ )
    {
    if ( 
this.controls].type() != )
      {
      
this.controls.delete);
      -- 
i;
      }
    }

  return 
true;
  } 
A tree-based communicator.
Trigger an event on the top of the tree, and it goes all the way down.
Reply With Quote
  #2  
Old 04-15-2007, 04:42 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
Quote:
Originally Posted by Novo View Post
PHP Code:
/*********************************
(+) addControl( obj ) returns BOOL
  - adds sub-controller
  - MUST be in class "communicator-tree"

(+) remControl( obj ) returns BOOL
  - removes sub-controller

(+) getControls( ctype ) returns controls[];
  - Returns array of all objects with this.ctype == ctype
  - Recursive ( applies to sub-controllers, and thenforth )

(-) removeEmpty() returns BOOL
  - Cleans up controls list of empty objects (object.type() != 2 )

( ) onEvent( event, param1, param2, param3, ... )
  - called through obj.trigger( "Event", param1, param2, param3 );
  - Is recursive
  - to override, do this:
    function onEvent()
      {
      // Stuff
      controller::onEvent( params[0], ..., params[n] );
      }

( ) onEvent[EventName]( params )
  - Event Called when onEvent( EventName, params ) is.
  - no default.

( ) onDestroy()
  - SHOULD BE USED INSTEAD OF obj.destroy();
  - called to destroy object

( ) onParentDestroy()
  - Event Called when Parent Destroyed
  - default:
    if other parent, stay alive, else
      call Destroy
  - to override, do this:
    function onParentDestroyed( obj )
      {
      this.parentControls.remove( obj );
      return;
      }
*********************************/

public function addControlobj )
  {
  if ( ! 
obj.isinclass"communicator-tree" ) )
    {
    echo( 
obj.id SPC "is not a communicator-tree.");
    return 
false;
    }

  if ( 
this.controls.indexobj ) == -)
    
this.controls.addobj );
  if ( 
obj.parentControls.indexthis ) == -)
    
obj.parentControls.add(this);

  return 
true;
  }

public function 
remControlobj )
  {
  
obj.parentControls.removethis );
  
this.controls.removeobj );
  return 
true;
  }

public function 
getControlsctype )
  { 
// Searched entire directory
  
removeEmpty(); // Clean Up
  
for ( cthis.controls )
    {
    
temp.arr c.getControlsctype );
    if ( 
temp.arr.type() == )
      
temp.array.addarraytemp.arr );
    }

  if ( 
this.ctype == ctype )
    
temp.array.addthis );

  return 
temp.array;
  }

function 
onEvent()
  { 
// Recursive
  
param params;
  
removeEmpty(); // Clean up!
  
this.trigger"Event"param[0], param[1] );
  for ( 
cthis.controls )
    
c.trigger"Event"param );
  }

function 
onDestroy()
  {
  
removeEmpty(); // Clean up!
  
for ( cthis.parentControls )
    
c.remControlthis );
  for ( 
cthis.controls )
    
c.trigger"ParentDestroyed"this );

  
this.destroy();
  }

function 
onParentDestroyedthis )
  {
  
removeEmpty();
  
this.parentControls.removethis );

  
// No parent? Destroy itself!
  
if ( this.parentControls.size() == )
    
this.trigger"Destroy""" );
  }

function 
removeEmpty()
  {
  for ( 
0this.controls.size(); ++ )
    {
    if ( 
this.controls].type() != )
      {
      
this.controls.delete);
      -- 
i;
      }
    }

  return 
true;
  } 
A tree-based communicator.
Trigger an event on the top of the tree, and it goes all the way down.
Out of curiosity ... what does that do?
__________________
Reply With Quote
  #3  
Old 04-15-2007, 06:30 AM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Quote:
Originally Posted by cbkbud View Post
Out of curiosity ... what does that do?
It's a tree communicator. Basically...

There is a parent object, and child objects. You can attach an object to the parent object... When you trigger an event to the parent object, it sends it out to all children objects.

For instance...

If you have a house ( parent object ) and you have furniture in ( children objects ). Sadly, the house burns down, and everything in it. ( Event ). One way of doing this is house.trigger( "Event", "burnDown", 4 );
So every object in the house gets a object.onEventburnDown( 4 ); called.
Reply With Quote
  #4  
Old 04-15-2007, 06:58 AM
theHAWKER theHAWKER is offline
**FLIP OUT**
theHAWKER's Avatar
Join Date: Mar 2006
Location: canada, vancouver
Posts: 768
theHAWKER is an unknown quantity at this point
wow, what server would u use this on?
__________________
**FLIP OUT**
Reply With Quote
  #5  
Old 04-15-2007, 07:01 AM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Quote:
Originally Posted by theHAWKER View Post
wow, what server would u use this on?
I personally used it for earthquakes and weather... When there was rain, the plants would get a rain trigger, and would get the required water.
Reply With Quote
  #6  
Old 04-15-2007, 07:22 AM
Chandler Chandler is offline
Banned
Join Date: Jan 2007
Posts: 656
Chandler will become famous soon enough
I'll be using this =D
Thank you!
Reply With Quote
  #7  
Old 04-15-2007, 02:08 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
Quote:
Originally Posted by Novo View Post
It's a tree communicator. Basically...

There is a parent object, and child objects. You can attach an object to the parent object... When you trigger an event to the parent object, it sends it out to all children objects.

For instance...

If you have a house ( parent object ) and you have furniture in ( children objects ). Sadly, the house burns down, and everything in it. ( Event ). One way of doing this is house.trigger( "Event", "burnDown", 4 );
So every object in the house gets a object.onEventburnDown( 4 ); called.
Okay, I didn't quite understand it.

Nice, fancy code! Good work, keep it up

Thanks for contributing, I'll sure use this.
__________________
Reply With Quote
Reply


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:06 AM.


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