Jump to content
  • Advertisement
Sign in to follow this  
Matrix357

C# question of design

This topic is 4357 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
Advertisement
yep. you could create 10 monsters either way, but i guess an array would be more preferable (but it all depends on use). then as you put it, when you are done with the monster, you go monster = null.

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
why hell, I dont think I could be classed as a games developer, more like someone who can never get any project finshed (work ones as well)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!