Custom default NPC class
PostPosted: Wed Sep 16, 2009 11:30 pm
Mark's addition of a server property for specifying the default NPC class is a very nice feature for customization. It does have some shortcomings, however...
- Any custom NPCs you have already created that extend GameNPC have to be updated to extend your custom class
- Scripts that create NPCs need to create NPCs of the custom class, rather than GameNPC
- Code: Select all
public static GameNPC CreateDefaultNPC()
{
string defaultClassType = ServerProperties.Properties.GAMENPC_DEFAULT_CLASSTYPE;
if( defaultClassType == "DOL.GS.GameNPC" )
{
return new GameNPC();
}
return CreateInstance( defaultClassType, typeof( GameNPC ) ) as GameNPC;
}
- Code: Select all
public static Object CreateInstance( string className, Type baseClass )
{
Object instance = null;
Assembly gameServerAssembly = Assembly.GetAssembly( typeof( GameServer ) );
try
{
instance = gameServerAssembly.CreateInstance( className );
}
catch
{
instance = null;
}
if( instance == null )
{
foreach( Assembly scriptAssembly in ScriptMgr.Scripts )
{
try
{
instance = scriptAssembly.CreateInstance( className );
}
catch
{
instance = null;
}
if( instance != null )
{
break;
}
}
}
if( instance != null && baseClass != null )
{
try
{
instance = Convert.ChangeType( instance, baseClass );
}
catch
{
instance = null;
}
}
return instance;
}