Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 10-30-2007, 08:31 PM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Simulating Interfaces

One thing I was working on today was on a trade system for Relic. A criteria I valued was that I didn't want a central class for deciding what the trading does. So I was brain-storming, and I came across an idea from my Java course... Interfaces does not force how a function works... Instead, it mainly states that the object inherits a particular function subset.

So I was thinking of how I can imply this. My main goal is to have it so when I drag an item over a tradable object, it shows a trade-icon... So... I decided that what I needed was an interface for all items that can trade.

In Graal, I know that classes contain codes inside them... But of particular interest, classes are in the joinedclasses variable ( which happens to be set both clientside and serverside ). So what I did was made the object join an interface. If the object complies to the interface, it stays in the class list... If it doesn't, it just removes itself.

This way, in clientside... I know ALL objects that inherit the interface-tradable all contain the necessary code to handle such items.

I hope this concept helps. ( This doesn't make a distinction between private/public functions, nor do they imply that the functions do what they should )

PHP Code:
function onCreated()
{
  if ( !
isTradable() )
    
leave"interface-tradable" );
}

function 
isTradable()
{
  
// Test to see if the functions in the object correspond to those of the interface
  
temp.functions getFunctions();
  
temp.tradeFunctions = {"addItem""getItems" };
  for ( 
temp.functemp.tradeFunctions )
  {
    if ( !(
temp.func in temp.functions) )
    { 
// Object doesn't contain a function in interface criteria
      
return false;
    }
  }
  return 
true;

Please note that the onCreated trigger is executed in the NEXT tick. That is, the object will still be in the class until 0.05 seconds later. If you require an immediate answer, either sleep(0.05) before testing, OR use this:

PHP Code:
if ( isinclass("interface-tradable") && isTradable() )
  
stuff
Also note that if the functions were inherited by other classes, and those classes were removed at a later date, that the class itself may still be in the interface class but doesn't fill the interface requirements. Again, if such an issue arises, the previous counter-measure will still work as expected.

Last edited by Novo; 10-30-2007 at 09:24 PM..
Reply With Quote
  #2  
Old 10-30-2007, 10:36 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
We must be long lost brothers, I believe we think similarly about scripting
Quote:
Originally Posted by Novo View Post
Please note that the onCreated trigger is executed in the NEXT tick. That is, the object will still be in the class until 0.05 seconds later. If you require an immediate answer, either sleep(0.05) before testing,
That caused me some irritation a few months ago, my suggestion for fixing that is an onJoin() function event specifically for classes that would be executed at the end of the joining process and before you can join another class or continue the function that called the join. The event would have to complete before the script could continue. The event would also not be added to the object the class is being joined to. There would also be the onLeave() counterpart.
__________________
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 06:32 AM.


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