A question about finding and retrieving data from an array.

0 favourites
  • 5 posts
From the Asset Store
Data+ is the best Data Management solution for Construct 3. It contains 4 Addons (Plugin & Behavior).
  • Hi guys. I'm tinkering with various ways of using arrays and/or dictionaries to store various types of data such as character stats and inventory information. I'd like some insight on a specific use case just to be sure that I'm doing things efficiently.

    In tabletop RPG games, characters will typically have Ability Scores or Attributes which determine stats such as Strength, Intelligence, Dexterity, and so forth. There is often also an Ability Modifier, which is a bonus or penalty that applies to attack rolls or skill checks as a reflection of the character's general capability. I have a Dictionary object that holds all of the Player's ability scores, which are generated and assigned at the start of the layout. After assigning the ability scores, I'd like to determine the modifiers based on each of those scores.

    I have an array which is several elements wide, and two elements tall. The X index contains the possible ability scores that could be generated, and the Y index contains the Modifier for each score — so if the player has Strength 5, and we find the number 5 at index (4, 0), then the correct modifier is located at index (4, 1) and should be assigned as the player's Strength Modifier.

    My thought is to loop through the X index looking for player's ability score, and once located, to grab the corresponding modifier from the Y element. Could someone enlighten me as to whether this is the best way to achieve this, and if so, what the code snippet for the loop and for copying the modifier from the array to the dictionary with the player's stats should look like?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • You shouldn't need to use both the dictionary and the array, just use the array. As for the array the number 5 at 4,0 doesn't mean too much. Better to have the name of the stat at 4,0 so "Str" so you can search for it, then once found you have the array.curx, you can then use this to access the other stuff. If I was to do it though I would not use an array for character stats, global variables are fine. You mentioned an inventory, this could be done with an array.

  • The array exists to "translate" the character's Ability Score (Strength = 5) into the Ability Modifier (-3 on strength-based attacks, because 5 is a small amount of Strength in this example). The character is assigned a Strength score between 5 and 15. A strength score of 5 has a -3 modifier, while a strength score of 6 has a -2 modifier, and so forth. I'm ultimately just trying to figure out the most efficient way to "look up" the correct modifier for each score, and then to feed that number to the character's stat sheet.

    The dictionary serves as the character's stat sheet, and I figured it was easier to reference the character's attributes/skills/etc as key names rather than array elements.

  • I find myself using JSON object more and more often, even convert existing arrays (like inventory) to JSON. It's much more flexible than arrays, if you organize its structure right you can access any data with ease, without having to iterate through its values. For example, it can be something like JSON.Get("abilities.attack.strength_modifier")

  • I find myself using JSON object more and more often, even convert existing arrays (like inventory) to JSON. It's much more flexible than arrays, if you organize its structure right you can access any data with ease, without having to iterate through its values. For example, it can be something like JSON.Get("abilities.attack.strength_modifier")

    That's really interesting — thanks for the idea.

    In the meantime, it occurred to me that I can use another dictionary to achieve what I'm trying to do, and it's much simpler as there's no looping or comparing needed. Just a key for each number within the range of possible ability scores, and the corresponding modifier is the value of each key.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)