• Advertisement
Sign in to follow this  

What do you guys use as a key in RPG data structures?

This topic is 969 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've been using a lot of std::map<std::string, int> or something. An example would be:
 
std::map<std::string, int> attributes_;
attributes_["str"] = 5
attributes_["def"] = 5

 

I'm not sure I'm good enough at C++ yet to know, but using "std::string" as a key too much feels like it could go incredibly wrong in the future. What do you guys suggest for readable/usable code?

 

I considered a global enum, but that strikes me as worse than the string idea.

Share this post


Link to post
Share on other sites
Advertisement

I should use an unordered map! The issue with the global enum is that I also use strings as keys to a map which keeps track of what item is equipped where.  Also, using an unordered map would lessen the changes I'd have to make to any codes using iterators. ;)

 

Thanks, by the way. I totally forgot unordered_map was a thing.

Edited by AmagicalFishy

Share this post


Link to post
Share on other sites

Mm... for something really basic. What I do is

 

Struct Attributes{
HP
MP
STR
DEX
WIS
CON
PER
}

 

attribute *ptr_Characters

map<std::string, Attributes> _Character

ptr_Characters = Character[Name]; //Cache the address of what we just accessed.

 

ptr_Characters->HP = 5; //set HP of selected critter to 5

 

Reason for this set up is that just about all RPG characters will share these attributes. But listing things individually would be counter productive. Especially if entities are going to be placed in the same map.

Edited by Tangletail

Share this post


Link to post
Share on other sites

What's wrong with just having strongly typed fields? Will you be inventing new stat types at run time? I think that in the quest to build the I-can-build-any-game-with-my-custom-engine engine people go overboard with the dynamic data structures. That code isn't as reusable as you think it is. And you probably won't want reuse it anyway because you'll find 100 reasons during your first implementation that you'll feel it necessary to build your RPG engine 2.0 from scratch. Also when choosing to go dynamic, you're not taking advantage of the facilities of the c++ language such as static typing that will help you avoid some bugs and enables certain types of developer productivity tools like refactoring helpers built in to Visual Studio.

I second this. Also as if you happen to find new stat a that that would better fit as double instead of int, you still have it in the same structure without runtime type checks or introducing another map.

Share this post


Link to post
Share on other sites
You are being dynamic when you use a map for the stats. I would recommend using composition of components to indicate that a thing has a certain category of stats (not saying go down the component based entity rabbit hole). I think for most games this would align with the use case anyway. So you might have a class called DefensiveStats that carries along physical defense, magic resistance, etc. then you might have a class called HealthStats that might carry the HP. Then StatusStats or some such that carries any status ailments or buffs. You see where I'm going here. Your game object will have a field for each of these stat categories. When the object has a concept of defense, you'll stick a defense stat in defense stats field, etc. This approach will be dynamic enough while also maintaining static typing and avoiding the overhead of using a map to store stats. You'll also save yourself a ton of casting.

Share this post


Link to post
Share on other sites

Ah! 

 

I was planning to implement status-objects (buffs, debuffs, etc.) but I didn't think of following this same route with base-stats (str., def., etc.) 

 

The way it works now is:
- There are "attack" objects, which all inherit from a base attack class (and may be composed of some other things in the future)—they do all the calculations given a character's stats and some damage multiplier. This will make it easier to give specific characters specific abilities. When performing an attack, one of these objects is created, passed to an attack-event, and an attack-handler applies all the appropriate damage. 

- Items have the same attribute-map as characters. When items are equipped, an iterator just adds the appropriate item stats to the character stats by key (equipping, picking up items, etc. is actually handled by a handler, too, since it requires things like removing an item from a room and putting it in the character's inventory.)

 

This might be an inefficient system, but it's my first game~

 

Would creating a separate object for each "set" of stats really be less overhead than just using a map? Considering that attacks use these stats, items affect these stats, etc. I didn't really have dynamism in mind when I started using maps, I just thought it was the easiest way to store a base-attribute integer.

Share this post


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

  • Advertisement