RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

How to create Items based on a Type

Lokai

Knight
How to create Items based on a Type

OK, this one is hard even to describe, so I will post some pseudo code to illustrate.

Code:
using System;
 
namespace MyWorld
{
   public class BaseShape : Item
   {
      //code here
   }
}

Code:
using System;
 
namespace MyWorld
{
   public class BaseSquare : BaseShape
   {
      //code here
   }
}

Code:
using System;
 
namespace MyWorld
{
   public class BaseCircle : BaseShape
   {
      //code here
   }
}

Code:
using System;
 
namespace MyWorld
{
   public class BaseTriangle : BaseShape
   {
      //code here
   }
}

Now, what I want to do is based on this next bit of code, so its the most important part. Lets pretend that I have many classes that are all Shapes. Some of them are Derived from Square, some from Circle, and some from Triangle. The one below is Derived from Square. I have named it DerivedSquare1 for simplicity. It has 2 variables, both Types, because they can be any type of Shape, one based on Circle, and the other based on Triangle. But I do not know which during Scripting, because there are many, like DerivedTriangle1, DerivedTriangle2, etc. SO HERE IS THE QUESTION: How do I create the items based on a Type? I don't just want to create BaseTriangle and BaseCircle, because they don't have all the code I need, it has to be specific Types which are derived from the base types.

Am I missing something easy?

[/code]

Code:
using System;
 
namespace MyWorld
{
   public class DerivedSquare : BaseSquare
   {
      public Type TriangleType;
      public Type CircleType;
      // HERE IS WHERE I WANT TO CREATE SOME ITEMS
      Item i = new Item; //not what I want.... I want somehow to call typeof(TriangleType) or something...??
      Item j = new Item;
   }
}
 

Khaz

Knight
The .NET Framework provides a method for doing this: Activator.CreateInstance( Type type )
You could implement as such:
Code:
using System;

namespace MyWorld
{
	public class DerivedSquare : BaseSquare
	{
		public Type TriangleType = typeof( Triangle );
		public Type CircleType = typeof( Circle );

		TriangleType i = null;

		try{ i = Activator.CreateInstance( TriangleType ) as TriangleType; }
		catch{}

		if( i != null )
			//creation was successful
	}
}
You have to cast the value returned from CreateInstance because it is returned as an Object. It is also a good idea to use the try/catch as this method is not always "safe."
 

Lokai

Knight
One problem.....

TriangeType is not a class, its a variable....

TriangleType is a Type variable. That Type could be DerivedTriangle1, DerivedTriangle2, etc.

I can't use:

Code:
TriangeType i = null;

because TriangleType is not a class or Type, its a variable.

And I don't want to use:

Code:
DerivedTriange1 i = null;
try{ i = Activator.CreateInstance( typeof( DerivedTriange1 ) ) as DerivedTriange1; }

because I don't know if it's DerivedTriange1 or not.

:( Help!
 

Khaz

Knight
Use object instead, then:
Code:
object i = null;

try{ i = Activator.CreateInstance( TriangleType ); }
catch{}

if( i != null ) {
	//creation successful, start checking via reflection...
	if( i is DerivedTriangle1 ) {
		//yay
	} else if( i is DerivedTriangle2 ) {
		//yay #2
	}
}
 

Lokai

Knight
Thank you again.

Now, lets move this into RunUO terms, if you don't mind. An example a little closer to home...(this is not my real code, but simulates the concept.)

Tell me if this code would work for what I am trying to do:


PHP:
public class CreateMobile
{
            public Type MobileType { get{ return m_MobileType; } set{ m_MobileType = value; } }
 
            public void DoCreate( Mobile from )
            {
                        Mobile m = null;
                        try { m = Activator.CreateInstance( MobileType ); }
                        catch{}
                        if( m != null ) //creation was successful
                        {
                                    if ( m is from.Combatant ) //If we are fighting the same type of Monster...
                                                from.SendGump( new SuccessGump( from, m ) );
                        }
            }
}
 

Khaz

Knight
Close. Highlighted recommended changes:
Code:
public class CreateMobile
{
            public Type MobileType { get{ return m_MobileType; } set{ m_MobileType = value; } }
 
            public void DoCreate( Mobile from )
            {
                        Mobile m = null;
                        try { m = Activator.CreateInstance( MobileType ) [highlight]as Mobile[/highlight]; }
                        catch{}
                        if( m != null ) //creation was successful
                        {
                                    if ( [highlight]m.Combatant != null &&[/highlight] m is from.Combatant[highlight].GetType()[/highlight] ) //If we are fighting the same type of Monster...
                                                from.SendGump( new SuccessGump( from, m ) );
                        }
            }
}
 

Jeff

Lord
You are looking at this all wrong, or I am not understanding, But it seems to me you want to do something like this instead.

Code:
	public class DerivedSquare : BaseSquare
	{
		public BaseShape TriangleType;
		public BaseShape CircleType;
	
		public DerivedSquare()
		{
			//You can make TriangleType anything you want since all shapes derive from the same BaseShape
			TiangleType = new BaseTriangle();
			//Or even this if you REALLY wanted to
			TiangleType = new BaseCircle();
		}

Make sense?
 
Top