Sign in to follow this  

Collection of Objects - problems and thoughts (C#)

This topic is 4666 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having problems working with a collection of objects. I'm currently working on a simple Text-Based RPG. I've created a MonsterHandler class, which is in charge of creating and destroying Monsters, as well as keeping track of spawned monsters. Inside this class is the Monsters class, which contains the pertinent information, such as name, hit points, stats, etc. Starting my program, i create an instance of the MonsterHandler, and Spawn a Monster. Now, I'm from VB6 and VBScript by nature, so I was looking for the equivalent of the Collection system. I think I found it in ArrayLists. After Spawning a Monster, I add it to my tracker ArrayList. The problem I'm having is pulling that data back in the main program body. I found out that when pulling it back out of the ArrayList, I gotta cast it back as it's default object type to return it's properties. I can't manage to do that in the main program loop, as the Monster class is part of the MonsterHandler class, and isn't exposed in any way. So, I tried to create a GetMonster method inside the MonsterHandler class. I had some problems figuring out the proper return type, but it appears "public object GetMonster()" works to that purpose. Now, inside this method i create a instance of Monster, and assign it the one I'm looking for out of the ArrayList, and return that object. The object returns, but the main program loop doesn't recognize any properties, methods or the like. It's frustrating to say the least. I'm new at the whole C# thing, being my first language outside of rudimentary VB6. Any help here would be appreciated. For reference:
using System;
using System.Collections;
namespace DemoGame1
{

	class CMonsterHandler
	{
		private Random rannum = new Random();
		public ArrayList monsterlist = new ArrayList(1);

		public object GetMonster(int monnumber)
		{
			object mob;
			mob = (CMonster) monsterlist[monnumber];

			return mob;

		}
		public void SpawnNewMonster()
		{
			CMonster mob = new CMonster();
			mob.ItemName = "Displacer Beast";
			mob.SetStat(STR, 10);
			mob.SetStat(DEX, 10);
			mob.SetStat(CON, 10);
			mob.SetStat(INT, 10);
			mob.SetStat(WIS, 10);
			mob.SetStat(LUK, 10);

			monsterlist.Add(mob); //ADD TO COLLECTION			
			
		}
		//HANDLES THE CREATION AND MAINTENANCE OF MONSTERS
		class CMonster : CMOB
		{
			private int gold;
			private Random rannum = new Random();
			public CMonster()
			{
				gold = rannum.Next(1, 10);

			
			}
			public int Gold
			{
				get
				{
					return gold;
				}
			}
		
		}
		class CMOB									//SETS BASIC STATS FOR PLAYER AND MONSTERS
		{
			private string name;
			private int health;
			private int mana;
			private int[] attributes = new int[6];

			public const int STR = 0;
			public const int DEX = 1;
			public const int CON = 2;
			public const int INT = 3;
			public const int WIS = 4;
			public const int LUK = 5;

			public CMOB()
			{
				health = 0;
				mana = 0;
				attributes[STR] = 0;
				attributes[DEX] = 0;
				attributes[CON] = 0;
				attributes[INT] = 0;
				attributes[WIS] = 0;
				attributes[LUK] = 0;
			}

			public int GetStat(int stat)
			{
				return attributes[stat];
			}
			public void SetStat(int stat, int val)
			{
				attributes[stat] = val;
			}

			public string ItemName
			{
				get
				{
					return name;
				}

				set
				{
					name = value;
				}
			}
		}

	}

	class Class1
	{
		[STAThread]
		static void Main(string[] args)
		{
			//CREATE A MONSTERHANDLER INSTANCE
			CMonsterHandler monsterhndlr = new CMonsterHandler();
			
			//CREATE NEW MONSTER
			monsterhndlr.SpawnNewMonster();
			
			Object mob = new Object();
			mob = monsterhndlr.GetMonster(0);
                        Console.WriteLine(mob.ItemName);//DOESN'T WORK!!
			Console.WriteLine("HI");
		}
	}
	
}


Share this post


Link to post
Share on other sites
set
{
name = value;
}

hmm, did you declare "value"? o_O

[edit] oops my bad I guess value is whatever the user set itemname to.

[edit] try this...


using System;
using System.Collections;
namespace DemoGame1
{

class CMonsterHandler
{
private Random rannum = new Random();
public ArrayList monsterlist = new ArrayList(1);

public object GetMonster(int monnumber)
{
object mob;
mob = (CMonster) monsterlist[monnumber];

return mob;

}
//changed here
public CMonster SpawnNewMonster()
{
CMonster mob = new CMonster();
mob.ItemName = "Displacer Beast";
mob.SetStat(STR, 10);
mob.SetStat(DEX, 10);
mob.SetStat(CON, 10);
mob.SetStat(INT, 10);
mob.SetStat(WIS, 10);
mob.SetStat(LUK, 10);

monsterlist.Add(mob); //ADD TO COLLECTION
return mob;

}
//HANDLES THE CREATION AND MAINTENANCE OF MONSTERS
class CMonster : CMOB
{
private int gold;
private Random rannum = new Random();
public CMonster()
{
gold = rannum.Next(1, 10);


}
public int Gold
{
get
{
return gold;
}
}

}
class CMOB //SETS BASIC STATS FOR PLAYER AND MONSTERS
{
private string name;
private int health;
private int mana;
private int[] attributes = new int[6];

public const int STR = 0;
public const int DEX = 1;
public const int CON = 2;
public const int INT = 3;
public const int WIS = 4;
public const int LUK = 5;

public CMOB()
{
health = 0;
mana = 0;
attributes[STR] = 0;
attributes[DEX] = 0;
attributes[CON] = 0;
attributes[INT] = 0;
attributes[WIS] = 0;
attributes[LUK] = 0;
}

public int GetStat(int stat)
{
return attributes[stat];
}
public void SetStat(int stat, int val)
{
attributes[stat] = val;
}

public string ItemName
{
get
{
return name;
}

set
{
name = value;
}
}
}

}

class Class1
{
[STAThread]
static void Main(string[] args)
{
//CREATE A MONSTERHANDLER INSTANCE
CMonsterHandler monsterhndlr = new CMonsterHandler();

//CREATE NEW MONSTER

//changed here
Object mob = new Object();
mob = monsterhndlr.SpawnNewMonster();

Console.WriteLine(mob.ItemName);//DOESN'T WORK!!
Console.WriteLine("HI");
}
}

}




[Edited by - piccahoe on March 10, 2005 12:11:10 AM]

Share this post


Link to post
Share on other sites
If you wish to use your Monster class to deal with monsters, then you are going to have to expose it to the program. It's really that simple. However, if you wish to restrict the creation of monsters to the MonsterHandler, simply declare the Monster constructor internal. At which point only that assembly can create a monster.

Edit: something like (i haven't compiled this...):


using System;
using System.Collections;
namespace DemoGame1
{

public class MonsterHandler
{
private Random rannum = new Random();
private ArrayList monsterlist = new ArrayList(1);

public Monster GetMonster(int monnumber)
{
Monster mob = (Monster) monsterlist[monnumber];

return mob;

}

public void SpawnNewMonster()
{
Monster mob = new Monster();
mob.ItemName = "Displacer Beast";
mob.SetStat(MOB.STR, 10);
mob.SetStat(MOB.DEX, 10);
mob.SetStat(MOB.CON, 10);
mob.SetStat(MOB.INT, 10);
mob.SetStat(MOB.WIS, 10);
mob.SetStat(MOB.LUK, 10);

monsterlist.Add(mob); //ADD TO COLLECTION

}
}

public class Monster : MOB
{
private int gold;
private Random rannum = new Random();
public Monster()
{
gold = rannum.Next(1, 10);

}
public int Gold
{
get
{
return gold;
}
}

}

public class MOB
{
private string name;
private int health;
private int mana;
private int[] attributes = new int[6];

public const int STR = 0;
public const int DEX = 1;
public const int CON = 2;
public const int INT = 3;
public const int WIS = 4;
public const int LUK = 5;

public MOB()
{
health = 0;
mana = 0;
attributes[STR] = 0;
attributes[DEX] = 0;
attributes[CON] = 0;
attributes[INT] = 0;
attributes[WIS] = 0;
attributes[LUK] = 0;
}

public int[] Stat { get { return attributes; } }

public string ItemName
{
get
{
return name;
}

set
{
name = value;
}
}
}

class Program
{
static void Main(string[] args)
{
//CREATE A MONSTERHANDLER INSTANCE
MonsterHandler monsterhndlr = new MonsterHandler();

//CREATE NEW MONSTER
monsterhndlr.SpawnNewMonster();

Monster mob = monsterhndlr.GetMonster(0);
Console.WriteLine(mob.ItemName);//DOESN'T WORK!!
Console.WriteLine("HI");
}
}

}

Share this post


Link to post
Share on other sites
My suggestions:


  1. Declare the monster class as it's own seperate class instead of a member class of the monster handler

  2. Look at System.Collection.Generic and use ArrayList<CMonster>



The first point will fix the problem of not being able to cast to CMonster because the whole program will know about this class. This is absolutely necessary if you wish to do anything monster-related outside of the MonsterHandler class.

The second point will make it so you don't need to do a cast to pull something out of your list, it's just prettier :)

Share this post


Link to post
Share on other sites
Excellent! Moving the CMonster class outside worked great, after a quick cast when pulling it. All works good now.

I'll look into the System.Collection.Generic soon enough. I did a few google searches, but it's a bit more than I'm ready to try and understand at this point. Many thanks!

Share this post


Link to post
Share on other sites

This topic is 4666 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this