Reference member of type by position?

Previous topic - Next topic

jaywat

Can I reference the members of a type based upon the order of membership? ie, it's ID.

In other words...

Assume I have a type set up like:

Code (glbasic) Select


GLOBAL allCharacterStats[] AS tCharacterStats

TYPE tCharacterStats
strength
dexterity
constitution
ENDTYPE


and I want to use a one-size-fits all function when these members are updated, something like :

Code (glbasic) Select

FUNCTION userEditsStat:statBeingEdited

IF addButtonPressed = TRUE
inc allCharacterStats[0].statBeingEdited, 1  // no, you cant really do this. that's the point!
ENDIF

ENDFUNCTION



my point is, right now, I have to do something like:

Code (glbasic) Select

SELECT statBeingEdited
CASE 0
inc allCharacterStats[0].strength, 1
CASE 1
inc allCharacterStats[0].dexterity, 1
CASE 2
inc allCharacterStats[0].constitution, 1
ENDSELECT



... which means I duplicate a lot of code. Obviously my real life example is MUCH more complex than this, and while type members give me far more readable code, not being able to reference them by index, I'm duplicating so much code it's getting unwieldy.

So, is there a way to reference the members by ID that I'm missing?

If not, it would be a REALLY welcome addition. I would think they must be internally referenced by an ID of their dimension in an array anyway, no? So it would surely not be too hard to implement?

Slydog

You could simulate it by using constants, such as:

Code (glbasic) Select
CONSTANT ATTRIBUTE_STRENGTH = 0
CONSTANT ATTRIBUTE_DEXTERITY = 1
CONSTANT ATTRIBUTE_CONSTITUTION = 2

TYPE tCharacterStats
  stats[3]#
ENDTYPE

GLOBAL allCharacterStats[] AS tCharacterStats

allCharacterStats[0].stats[ATTRIBUTE_STRENGTH] = 100
allCharacterStats[0].stats[ATTRIBUTE_DEXTERITY] = 50
allCharacterStats[0].stats[ATTRIBUTE_CONSTITUTION] = 10


FUNCTION userEditsStat:statBeingEdited

  IF addButtonPressed = TRUE
    inc allCharacterStats[0].stats[statBeingEdited], 1
  ENDIF

ENDFUNCTION
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

jaywat

I totally never thought of that! My real world example is a somewhat more nested data arrangement (imagine if strength, constitution and dexterity themselves had multiple members, say, currentValue and maxValue), but I *think* it'll be scaleable once I get my head round it all :)

Thanks for that.

Slydog

#3
No problem.

For Current and Maximum values for each attribute, you could try two dimensional arrays, like this:

Code (glbasic) Select
CONSTANT VALUE_CUR = 0
CONSTANT VALUE_MAX = 1

CONSTANT ATTRIB_STRENGTH = 0
CONSTANT ATTRIB_DEXTERITY = 1
CONSTANT ATTRIB_CONSTITUTION = 2

TYPE tCharacterStats
  stats[3][2]#
ENDTYPE

GLOBAL allCharacterStats[] AS tCharacterStats

allCharacterStats[0].stats[ATTRIB_STRENGTH][VALUE_CUR] = 50
allCharacterStats[0].stats[ATTRIB_STRENGTH][VALUE_MAX] = 100

allCharacterStats[0].stats[ATTRIB_DEXTERITY][VALUE_CUR] = 50
allCharacterStats[0].stats[ATTRIB_DEXTERITY][VALUE_MAX] = 100

allCharacterStats[0].stats[ATTRIB_CONSTITUTION][VALUE_CUR] = 5
allCharacterStats[0].stats[ATTRIB_CONSTITUTION][VALUE_MAX] = 10


FUNCTION userEditsStat:statBeingEdited
  IF addButtonPressed = TRUE
    inc allCharacterStats[0].stats[statBeingEdited][VALUE_CUR], 1
  ENDIF
ENDFUNCTION
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

jaywat

Of course. Seems so obvious when you see it like that. So much for trying to be smart and use types. lol.

If I'd just thought of using well named constants as my named references, I'd have had perfectly readable code for getting and setting my variables no matter how complex my array, my code would be 10 times shorter, and I wouldn't be faced with a large rewrite now. Doh.

jaywat

Whilst it in no way excuses my overlooking this as a solution to my problem, it's none the less interesting to note that the word 'constant' is not mentioned once in the entire glBasic help file. If GLOBAL, LOCAL and STATIC have pages, surely CONSTANT deserves an honourable mention, if only for completeness when you press F1 over the keyword declaration.

MrTAToad

Originally, CONSTANT wasn't going to be available - apparently there were (are) cases where it's possible to overwrite the variable.

However, I've never been able to, so its possible this loop-hole has been fixed :)