Sign in to follow this  

C# question of design

This topic is 4106 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 am in the process of learning C# right now. And I have a simple question, in the book that I am learning from it teaches you to create objects from classes like so: Fighter player = new Fighter(); So from the above example I create an instance of the Fighter class, but when programming a game would it be wise to create multiple instances like that without destroying them somehow. So far the book hasn't taught me that yet. For example I am wondering if I had let's say 10 monsters on a screen to deal with, would I create 10 objects, like the above or would I create an array of objects. And how would I remove one of them let's say if creature 1 died and I didn't need access to that object anymore. would I go? player = null; Just some general questions I was wondering about design and dealing with classes. Thanks in advance

Share this post


Link to post
Share on other sites
Thank you for the reply, as I needed some clarification on it.
The book didn't teach me to remove the objects by using null but
it's something I learned back in the day when I learned VB6. So
I assumed Microsoft would use it's functionality the same with C#.

Thank you again...

Share this post


Link to post
Share on other sites
There a Dispose method so you can do this. You may have to override though as I am not too sure, maybe someone can fill in this bit of info. I am only learning C# myself.

// Destory the player object
//
player.Dispose();

If you just set player=null then I think it will be garbage collected at some pre-determined condition or time (once again, I am a bit hazy)

Share this post


Link to post
Share on other sites
So how would one go about overriding the Dispose() method on a basic class object. I know you can do that to objects like a Graphics object GDI+. But if anyone has any ideas please let me know.

Share this post


Link to post
Share on other sites
Quote:
Original post by Adam Hamilton
There a Dispose method so you can do this. You may have to override though as I am not too sure, maybe someone can fill in this bit of info. I am only learning C# myself.

// Destory the player object
//
player.Dispose();

If you just set player=null then I think it will be garbage collected at some pre-determined condition or time (once again, I am a bit hazy)

Dispose is pretty similar to a C++ destructor. And just like how calling obj->~Blah() doesn't result in a call to delete obj in C++, calling Dispose() in C# does not have anything at all to do with the object's memory actually being freed. Also note that Dispose is never called automatically...if you don't personally call it [either directly, or indirectly via a using block], the call will never be made for you.

Dispose() does not free your memory, and it does not get automatically called when the memroy is freed. So what good is it? Unmanaged resources. Closing file handles, releasing system objects, and so forth. Those won't be cleaned up by the system, and even if they do you probably want them cleaned up as early as possible, so you have to explicitly destroy them yourself. Enter Dispose. You use your object, call Dispose() to release its unmanaged resources, and then eventually the system will free its memory.

CM

Share this post


Link to post
Share on other sites
So Dispose() won't actually help with the question I have with the above problem?
See I have class objects representing each type of monster for example in an
rpg. I have 10 different type of monster's, and they are currently
attacking the player right now. The player kills one of the monsters,
and so my routine checks to see if any class objects are not being used
by the current creatures on screen. To remove the use of that class object
that is associated to the no longer alive monster what would I do?

I just assumed monster = null; would work just as well but for game design and
development I don't want the Garbage Collector running rampant because
of unused objects floating around. As they would or could potentially cause
performance problems.

If anyone has any insights I would greatly appreciate them as I want to get
this all sorted out ahead of time before I dive deep in game development with
C#.

Thanks in advance

Share this post


Link to post
Share on other sites

To be honest, it would be easier and better to assume that the garbage collector will not be an issue until you have actually finshed and found performance to be an issue. Just set the handles to null as you say.

An object will be garbage collected at an indeterminate time when the system feels it has not got enough free space, at this time, Dispose is called atuomatically. You only really need to call Dispose manually on an object you wish to free up that holds limited resources (bad example, but an example anyway, file handles)

I would personally either use a list object (.NET 2.0) or implement a list for your monsters by implementing the IList interface.

This way, you can use foreach(Monster m in MonsterList) to iterate through and operate each monster, and when it dies, just remove it from the list!

As an after thought, you could avoid the garbage collector by using a flag in the monsters,

ie a bool isActive = true;

and only update the monsters that are still alive, this way you will not actually dispose of them until you remove all monsters.

Good luck

Share this post


Link to post
Share on other sites
Thanks, that answered quite a few questions I was thinking of.
I am still a little ways from developing a game but I just wanted
to get the structure and design layout in my head correct before
I get to that point. As developing a game takes a little different
approach from your typical application development.

Thank You again

Share this post


Link to post
Share on other sites
Pardon me if I miss something in your explaination but are you basically trying to manage a list of monsters that are attacking the player?

The solution to your problem is either using a built in list collection or creating your own list. I recommend using the List Generic class. With this class you could create an instance of it that represents monsters that are attacking your player. You simply call the add method to throw in new objects of any type (which is good for you if you are using monster objects of varying types). If a player kills a monster, you can simply remove it from the list using the Remove method.

Generics and lists are very strong parts of C# and can make your life a lot easier understanding how to use them. Hope this helps.

Share this post


Link to post
Share on other sites
Well my question really was about destroying class objects but thanks for the
tip though. As I haven't gotten to the chapter on Generic class's yet but
I should be approaching that soon.

Share this post


Link to post
Share on other sites
Quote:
Original post by Matrix357
Well my question really was about destroying class objects but thanks for the
tip though. As I haven't gotten to the chapter on Generic class's yet but
I should be approaching that soon.

You don't destroy them, that's the entire point. They will be destroyed on their own when the time comes. All you do is stop holding references to the object [removing them from your list for instance], and the garbage collector takes care of the rest.

CM

Share this post


Link to post
Share on other sites
Got yah, that is the point of calling player = null; or monster = null;
Thanks for all the help everyone, I believe I understand it well enough now.
And I'll be learning about Generics shortly.

Share this post


Link to post
Share on other sites

This topic is 4106 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