Is there a better way: Looping through .json and adding object variables

Greetings,

Sorry for the confusing title, not sure what to have it as. I am hoping to have the following looked at to see if there is a better way to do this.

My main concern at the moment is the more data (in the example below, weapons) I add to the .JSON file the longer it will take to loop/repeat through which may add a delay to the game or worse cause issues if the player is swapping weapons prior loop/repeat finishing. If you have any thoughts/ideas I am all ears.

Basic weapons.jsonwith two weapons in it: [ { "name": "wpn_pump_action_shotgun", "animation": 0, "stats" - Pastebin.com

I am grabbing the .json with the send a request to a web page action as this is being hosted localy and previewed within the IDE. Stores the .json as string.

I convert the string to global variable, this way the data is stored between scenes rather then doing the above for every new scene. (Felt this would be smarter/faster)

Then I added the relevant variables to the object when they change weapons. (Repeat # is how many weapons there are)

I did some testing to see what the load times were. First lot was with x30 entries, the second was 200, third 500 (13k lines). I did x5 load test each time, first test was for loading the data and storing in the scene/global variables the second was looping thought and adding variables to object (vaild object was always last)

x30 Load .Json

  1. 5 MS
  2. 5 MS
  3. 4 MS
  4. 5 MS
  5. 5 MS

x30 Store on Object

  1. 1 MS
  2. 2 MS
  3. 1 MS
  4. 2 MS
  5. 2 MS

x100 Load .Json

  1. 11 MS
  2. 8 MS
  3. 9 MS
  4. 9 MS
  5. 9 MS

x100 Store on Object

  1. 3 MS
  2. 2 MS
  3. 2 MS
  4. 3 MS
  5. 2 MS

x500 Load .Json

  1. 24 MS
  2. 25 MS
  3. 23 MS
  4. 24 MS
  5. 23 MS

x500 Store on Object

  1. 7 MS
  2. 7 MS
  3. 7 MS
  4. 7 MS
  5. 7 MS

At 500 entries, load times are very fast. More then fast enough to do it on the main menu before the player starts.
I can’t imagine the player swapping weapons faster then 7MS, however I can always ad a isLoaded variable to check prior to swapping.

Still open to ideas for better implementation.

Why don’t you trigger the change when the weapon is changed? :flushed:
Conditionless loops aren’t the best for performance :sweat_smile:

1 Like

I do only trigger the check when weapons are swapped, but it still needs to loop through the variables to get the data. Maybe on that initial load I run a single loop and store the weapon name with it’s corresponding array number(Like a hashmap), that way instead of a loop each weapon swap I just call the variable of data.weapon.[key.weapon_name.value].name etc. I guess I’ll give it a try after work tomorrow :slight_smile:

So I played around with the logic tonight to remove any Conditionless loops and have a nice setup now.

Game Loads

  1. Within 20 MS, loads all the data from my .json file into a scene variable as a string.
  2. Convert the string into global variables as I will be using them in any scene from here on out.
  3. Once loaded into the global variables, I call GlobalVariableChildCount() to see how many weapons I have. (They’re stored as data.weapon.value.#.name, data.weapon.value.#.variable2, etc)
  4. Run a single however many GlobalVariableChildCount()'s there are, each time adding +1 to a current loop variable.
  5. Each loop stores a data.weapon.key. with corresponding current loop variable (eg: data.weapon.key.weapon_1 0, data.weapon.key.weapon_2 1, etc).

When I want to switch weapons, I check the objects variable to see if has the right stats and if not I simply add them on using data.weapon.value[data.weapon.key[object_name].variable1, data.weapon.value[data.weapon.key[object_name].variable2, etc.

1 Like