1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
5/5, 2 ratings

Converting IDamageable to Mobile

Summary of the tutorial

i have seen much confusion regarding the replacement of Mobile with IDamageable, specifically when referencing Combatant in the Mobile class.

First off, with certain items being attackable on EA Servers, this change was inevitable. The good news is, converting IDamageable to Mobile is easy as the Mobile class is IDamageable. 99% of all references IDamagable instances are actually mobiles, so we just need to figure out how to convert them.

1 Getting a Mobile Combatant

Since the Mobile property Combatant is a popular one, I'll use a n easy example, followed by a good example you can use as an extension. This example will help you set a Mobile variable. As already stated, the Mobile class is IDamageable, so note the keyword 'as' being used. Now, if the Mobiles Combatant isn't a mobile, the combatant variable will be null, hence the null check.

Code (C#):
  1. public Mobile GetCombatant(Mobile m)
  2. {
  3.     Mobile combatant = m.Combatant as Mobile;
  5.     if (combatant != null)
  6.     {
  7.         return combatant;
  8.     }
  10.     Return null;
  11. }
If you find yourself using this code a lot, you can create an extension:

Code (C#):
  2. public static Mobile GetMobileCombatant(this Mobile m)
  3. {
  4.     Mobile combatant = m.Combatand as Mobile;
  6.     If(combatant != null)
  7.     {
  8.         return Combatant;
  9.     }
  11.     return null;
  12. }