|
-
Jul 12th, 2006, 09:32 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2005] Accessing one instance in many locations
I've got a problem that is very hard to explain. I'd appreciate it if anybody who reads this could perservere through this explaination to help (thanks in advance).
I've got a game with 10 levels. I have monsters in this game. Each monster has stats I need to store (eg strength, speed, hit points, etc) which requires about 8 integers and 4 singles.
The monster is a generic structure that can be adapted to be any monster in my game. I create instances of monster, give them names, and place them in the level's I want them. For example I could have 3 baddies on level 1, 7 baddies on level 2, and 4 baddies on level 4, as well as 5 bandits on level 2, 3 bandits on level 3, and 3 bandits on level 4. The baddies and bandits are all different instances of the monster structure.
To store them I created this array: arrayOfMonsters(levelNumber)(IDNumber). The levelNumber is the number of the level that instance of monster is on, and the IDNumber only used to differentiate different monsters on the same level (it doesn't store anything about what type of monster they are or anything).
I wanted the storage to be efficient, so I only use as much of the array as I need. I've tried to create a picture of the array from the above example:
arrayOfMonsters(level1) = {}
arrayOfMonsters(level2) = {baddie, baddie, bandit, baddie, bandit, bandit, bandit, bandit}
arrayOfMonsters(level3) = {bandit, bandit, bandit}
arrayOfMonsters(level4) = {bandit, baddie, baddie, bandit, bandit, baddie, baddie}
For the remaining levels, there are no monsters. This works great for that information, but now I need to add bosses as well. Bosses are just a different type of monster, except they can be on multiple levels. This means that a boss on the first three levels could be just one instance of a monster. The problem comes when trying to add the boss to my array. I need to be able to keep track of the fact that the boss is the same over many levels, but he can't be assumed to be at the same IDNumber on different levels because of how the others are stored (he could be the first monster on level 1, the 9th on level 2, and the 4th on level 3).
This leads to the two solutions I've thought of and my question. My solutions are 1) Create a new array just for bosses, 2) use pointers in the 1st, 9th, and 4th positions of levels 1, 2, and 3 respectively (from my example) that all point to the same boss monster. Unfortunately, pointers seem to be C++, not vb (I need to use VB.net 2005 for this project).
So my question is, can anybody think of a way of implementing one array with something resembling pointers (or otherwise) to store all that information, or should I just use a second array?
Thanks for the perseverence in reading this (and hopefully responding)
-
Jul 12th, 2006, 10:27 PM
#2
Re: [2005] Accessing one instance in many locations
It sounds to me like you should be defining a Monster class, which is then inherited by your other classes. If you need multiple instances of a type of monster then you simply create multiple instances. If you require a single instance on multiple levels then you simply create a single instance and then refer to that instance from multiple places.
I'm not sure how well-up you are on the differences between value types and reference types but with a structure, which is a value type, you cannot have multiple variables refer to the same object. This is because value type variables actually contain the object, thus each variable must contain a discrete object. With classes on the other hand, which are reference types, each variable contains a reference to an object, which basically means its memory address. That means that you can have multiple variables that all contain the same memory address and thus all refer to the same object.
I'd suggest something like this. You'd have a Monster base class with all the members common to all monsters, which would not include anything relating to the level. That base class would be inherited by the SingleLevelMonster and MultiLevelMonster classes. The SingleLevelMonster class would add a property to store the monsters ID on its level. The MultiLevelMonster class would a different property that was a collection of IDs on the levels on which it existed. Also, I'd suggest using collections rather than arrays if there is even a remote possibility that you will want to add and/or remove monsters dynamically.
Also, does a monster really need to know what it's ID is on a level, or is it just the level that needs to know that? If it's just the level then it should probably not be a property of the monster object. Rather the levels should keep a dictionary where the keys are the IDs and the values are the monster objects.
-
Jul 12th, 2006, 10:57 PM
#3
Thread Starter
Hyperactive Member
Re: [2005] Accessing one instance in many locations
The whole point of the ID is iterating through all the monsters on a level to see what's on that level. Its only there because I need some way to distinguish all the monsters on one level (its just an index really). The key thing is though, I don't care about that ID anywhere else. It is only for iterating through the levels. However, for multilevel bosses, I need to make sure that each reference from the different levels is to the same boss.
You've cleared up a lot of it for me, except this:
 Originally Posted by jmcilhinney
If you require a single instance on multiple levels then you simply create a single instance and then refer to that instance from multiple places.
How do I refer to the one instance of the boss...
Oh... It just occured to me... So I initialise the boss as an instance of the class, then in all the places it needs to be I just put it there (ie arrayOfMonsters(level1)(0) = myboss, arrayOfMonsters(level2)(8) = myboss, arrayOfMonsters(level3)(3) = myboss)... Wow... that's so simple, I'm surprised I missed it...
Thanks a lot for the help (especially the patience to read the whole thing). If I've got anything wrong, could you correct me?
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|