Public Group

# binary array for monster abilities? (pretty easy question)

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

## Recommended Posts

This should be a pretty easy answer, but I decided to ask because its something that must come up A LOT in games, and if there is an accepted way of handling it I'd like to know rather than reinventing the wheel... Say I have a bunch of different monster types (for an rpg), and I also have a bunch of abilities such as spells and skills and such. Every monster type can use some subset of these abilities, and the majority of the abilities can be used by more than one different monster type. So I was thinking that I could just build in a boolean array of static length (length = total # of abilities in the game) for each monster and have each ability with a static key number. Then each monster just has a y/n in each spot in the array which corresponds to a given ability. Make sense? Good way to do it?

##### Share on other sites
It will work, but is extremely limited not only in it's verbosity (which entry was Fireball again...?), but as well as its extendability and robustness. It also isn't very memory friendly (why am I storing all these 0s?)

Might I suggest a component based design, conjoined with a scripting language, instead?

This way you can define what the entity is able to do based on components, and re-use abilities in terms of scrips. So Human Mage and Troll Shaman may both be able to perform magic (both have a spellBook component?), but their fireball spells may be different (different scripts)...

##### Share on other sites
Quote:
 Original post by PlethoraSo I was thinking that I could just build in a boolean array of static length (length = total # of abilities in the game) for each monster and have each ability with a static key number. Then each monster just has a y/n in each spot in the array which corresponds to a given ability.Make sense? Good way to do it?

That's one way. You can use bitfield to store 8 flags per byte too. This would be optimal approach if you plan on having monolithic logic - one which changes as a whole, or not at all.

As you scale such approach, you'll find that adding new abilities or updating existing ones becomes cumbersome. In addition, you cannot specify per-instance behavior. For example, all humans have Attack ability, but John, an instance of human, has StrongAttack instead.

Quote:
 It also isn't very memory friendly (why am I storing all these 0s?)

No, it's quite memory friendly. Assuming there are 8 different abilities. What is the minimum information one needs to carry to fully describe all abilities a particular entity has?

Given there are 256 different possibilities, the entropy is 8 bits.

If each entity will never use anything but a subset, then the required storage will be entropy of this subset instead. How this is implemented is irrelevant, the amount of information will always be the same.

##### Share on other sites
Quote:
 Original post by AntheusNo, it's quite memory friendly. Assuming there are 8 different abilities. What is the minimum information one needs to carry to fully describe all abilities a particular entity has?Given there are 256 different possibilities, the entropy is 8 bits.If each entity will never use anything but a subset, then the required storage will be entropy of this subset instead. How this is implemented is irrelevant, the amount of information will always be the same.

Given that the OP said he was going to use a boolean array, I think it is fair to assume he wasn't using an encoding scheme. Information entropy wasn't really the nature of the question. Given the limited extendability of the OP's solution and the limited data that could be conveyed in his method, I still think my point holds: an array of booleans is a waste of memory given its flexibility and limited verbosity. Sure, at the end of the day, it probably won't be the make or break memory foot-print on his system, but given that it wasn't the crux of my argument against it, I would say it is a moot point.

Though my understanding of information entropy in terms of data representation is limited -- perhaps I am still making an invalid point.

1. 1
2. 2
3. 3
Rutin
19
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633659
• Total Posts
3013208
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!