Skip to main content
Clarifying goal in title — ask about the problem to solve, not whether a particular solution would work
Source Link
DMGregory
  • 141k
  • 23
  • 258
  • 401

I'm still learning C++ and am no way an expert so I could be conceptualizing this wrong. this is a little complicated but 

I'm having trouble getting the right data structure for items in my game. I don't need actual object instance data (x, y, z locations etc. There's an "item" object that just gets filled with the data from the item table when an instance is created)

The type of system I want is similar to Bethesda's creation engine. Where, where I could basically start an array from 0 and increment it as an item is added to the game. Then I could just make each item in the array a struct holding all the itemsitem's variables. The reason I'm doing it this way is it makes it simple to get an item variable just item[56].size =: item[56].size returns that items sizeitem's size variable. However the (Meaning its gameplay dimensions, not its memory footprint)

The issue I'm having is different types of items require different variables. So for weapons there would need to be a damage variable that wouldn't exist for say a healing potion, or a gun requires different variables than a chair. So my current solution is to have many different arrays of structs, each array containing it'sits own struct base that only has only the relevant variables for that type. This leads to an unconnected series of arrays that get called like item_weapon[22].damage. I can't do a multi-dimensional array because it requires each type to have the same amount of items which they won't.

It'd be more ideal to just have 1 unified data structure holding all the itemsitems' data. (I would think, I'm still new to programming). So my idea would be to use an enum to increment the different types, then each enum holds an array of all the items of that type and each item in the arrays are structsis a struct that only havehas variables similar to those in the same enum number. However my instinct is that doing this would still require each array to be the same length, but I'm not sure. 

I can't find much on enum of arrays of structs. Is there a better solution? Is this maybe too complicated or not a good idea? Any help would be appreciated!

I'm still learning C++ and am no way an expert so I could be conceptualizing this wrong. this is a little complicated but I'm having trouble getting the right data structure for items in my game. I don't need actual object data (x, y, z locations etc. There's an "item" object that just gets filled with the data from the item table when an instance is created)

The type of system I want is similar to Bethesda's creation engine. Where I could basically start an array from 0 and increment it as an item is added to the game. Then I could just make each item in the array a struct holding all the items variables. The reason I'm doing it this way is it makes it simple to get an item variable just item[56].size = returns that items size variable. However the issue I'm having is different types of items require different variables. So for weapons there would need to be a damage variable that wouldn't exist for say a healing potion or a gun requires different variables than a chair. So my current solution is to have many different arrays of structs, each array containing it's own struct base that only has the relevant variables for that type. This leads to an unconnected series of arrays that get called like item_weapon[22].damage. I can't do a multi-dimensional array because it requires each type to have the same amount of items which they won't.

It'd be more ideal to just have 1 unified data structure holding all the items data. (I would think, I'm still new to programming). So my idea would be to use an enum to increment the different types, then each enum holds an array of all the items of that type and each item in the arrays are structs that only have variables similar to those in the same enum number. However my instinct is that doing this would still require each array to be the same length, but I'm not sure. I can't find much on enum of arrays of structs. Is there a better solution? Is this maybe too complicated or not a good idea? Any help would be appreciated!

I'm still learning C++ and am no way an expert so I could be conceptualizing this wrong. 

I'm having trouble getting the right data structure for items in my game. I don't need actual object instance data (x, y, z locations etc. There's an "item" object that just gets filled with the data from the item table when an instance is created)

The type of system I want is similar to Bethesda's creation engine, where I could basically start an array from 0 and increment it as an item is added to the game. Then I could just make each item in the array a struct holding all the item's variables. The reason I'm doing it this way is it makes it simple to get an item variable: item[56].size returns that item's size variable. (Meaning its gameplay dimensions, not its memory footprint)

The issue I'm having is different types of items require different variables. So for weapons there would need to be a damage variable that wouldn't exist for say a healing potion, or a gun requires different variables than a chair. So my current solution is to have many different arrays of structs, each array containing its own struct base that has only the relevant variables for that type. This leads to an unconnected series of arrays that get called like item_weapon[22].damage. I can't do a multi-dimensional array because it requires each type to have the same amount of items which they won't.

It'd be more ideal to just have 1 unified data structure holding all the items' data. (I would think, I'm still new to programming). So my idea would be to use an enum to increment the different types, then each enum holds an array of all the items of that type and each item in the arrays is a struct that only has variables similar to those in the same enum number. However my instinct is that doing this would still require each array to be the same length, but I'm not sure. 

I can't find much on enum of arrays of structs. Is there a better solution? Is this maybe too complicated or not a good idea? Any help would be appreciated!

Clarifying goal in title — ask about the problem to solve, not whether a particular solution would work
Link
DMGregory
  • 141k
  • 23
  • 258
  • 401

Is an enum of arrays of structs possible? Data structure for storing / looking up type objects representing different item types

edited tags
Link
Pikalek
  • 13.4k
  • 5
  • 49
  • 54
Source Link
Loading