Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    628
    Thanks
    51
    Thanked 67 Times in 67 Posts

    Get Class by Field Value

    What I want to be able to do is give each class an id (for easy storage in a database) and be able to use that id to call the class whenever I may need to.

    So, let's say I'm building an MMORPG. When a player obtains a sword (let's call it Sword A), the id for that sword will be stored in a database (so when the player logs out they can log back in and still have the sword). When the player gets into a fight, I would need to be able to take the id or Sword A and use it to access the class of Sword A so that I can retrieve its stats for damage calculation.

    Code:
    int[] inventory = {1476} //Player has 1 item in their inventory with id 1476
    ...
    class SwordA {
    	public static int id = 1476;
    	public static byte attack = 14;
    }
    Is there any way to access class SwordA using its id, apart from a method featuring an if statement for every item?
    Last edited by djh101; 12-15-2011 at 12:04 AM.
    "Yeah science!"

  • #2
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    628
    Thanks
    51
    Thanked 67 Times in 67 Posts
    This is the only way I know of that could accomplish this, but I'd prefer a method that avoids going through up to several thousand if statements:
    Code:
    Item getItem(int id){
    	if(id == 1) return BootsA();
    	else if(id == 2) return ShieldC();
    	else if(id == 3) return Cabbage76();
    	...
    	else if(id == 1476) return SwordA();
    }
    Item foo = getItem(1476);
    "Yeah science!"

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Not easily. If its hard coded, you need to look it up manually.
    Here's an example of what would work. Although, if you have a class called ShieldC, is it fair to assume you also have one called ShieldA and ShieldB? If so, this is overly complex, unless these are explicitly different datatypes. id is typically related to the object itself, and not that of the class.

    Code:
    public class ItemGetIDTest
    {	
    	public Object constructByID(ArrayList<Class<?>> c, int i) throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException, InstantiationException, InvocationTargetException, NoSuchMethodException
    	{
    		Class<?> create = null;
    		for (Class<?> itor : c)
    		{
    			if (itor.getDeclaredField("id").getInt(itor) == i)
    			{
    				create = itor;
    				break;
    			}
    		}
    		
    		return create.getConstructor().newInstance();
    	}
    	
    	public static void main(String... argv)
    	{
    		ItemGetIDTest t = new ItemGetIDTest();
    		ArrayList<Class<?>> al = new ArrayList<Class<?>>();
    		al.add(SwordA.class);
    		al.add(Item.class);
    		try
    		{
    			SwordA s = (SwordA)(t.constructByID(al, 1476));
    			System.out.println(s);
    		}
    		catch (Exception ex)
    		{
    			System.out.println("Error: " + ex.getMessage());
    		}
    	}
    }
    
    class Item
    {
    	public static int id = 0;
    	public int getID()
    	{
    		return id;
    	}
    }
    
    class SwordA extends Item
    {
    	public static int id = 1476;
    	public SwordA()
    	{
    		
    	}
    }

  • Users who have thanked Fou-Lu for this post:

    djh101 (12-14-2011)

  • #4
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    628
    Thanks
    51
    Thanked 67 Times in 67 Posts
    Thank you very much. And no, that was just an example. All items would have unique names (e.g. ultimate chaotic shield of doom) and would be extensions of different classes (i.e. class SwordA extends sword which extends item).
    "Yeah science!"

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by djh101 View Post
    Thank you very much. And no, that was just an example. All items would have unique names (e.g. ultimate chaotic shield of doom) and would be extensions of different classes (i.e. class SwordA extends sword which extends item).
    This still sounds like a data issue, and not a structural one.
    Code:
    class Item
    {
        protected int id;
        protected double value;
        protected String name;
        // whatever else + methods
    }
    
    interface IWeapon
    {
        public double getDamage();
        public void setDamage(double d);
    }
    
    class Sword extends Item implements IWeapon
    {
        private double damage;
        public Sword(int id, String name, double value, double damage)
        {
            super(id, name, value);
            setDamage(damage);
        }
        public void setDamage(double d)
        {
            this.damage = d;
        }
        public double getDamage()
        {
            return this.damage;
        }
    
        public static void main(String... argv)
        {
            Sword s1 = new Sword(1, "Ultimate Weapon", 0, 255);
            Sword s2 = new Sword(2, "Wooden Sword", 10, 10);
            // etc.
        }
    }
    Same logic applies to any type.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •