Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Simulating Interfaces (https://forums.graalonline.com/forums/showthread.php?t=77375)

Novo 10-30-2007 08:31 PM

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.

Inverness 10-30-2007 10:36 PM

We must be long lost brothers, I believe we think similarly about scripting :D
Quote:

Originally Posted by Novo (Post 1355461)
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.


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.