When does it make more sense to use arrays instead of variables and vice versa, and why?

0 favourites
  • 3 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Hi,

    So if you want to store lots of information it is better to use an array instead of creating a hundred local or global variables to store the information. Is it purely for practical reasons, because it's easier to handle the information in arrays? Is there a difference in performance between the methods?

    I'm planning to create rpg style stat system for around 100 units. Each unit has probably 20 - 30 different stats and they can all be upgraded and manipulated by different effects. In this case it seems obvious that either each unit should have it's own array for stats or all that information should be stores in a single array. Is there a benefit in choosing one big array or many smaller ones, or is it irrelevant performance wise and you should just choose to do whatever makes most practical sense?

    Then I have a biome system with 6 different biomes, a weather system with 3 weather states (clear, raining/snowing, foggy), and a season system with 1 - 4 seasons depending on the biome the player has chosen. I can set up all of these with relatively few LOCAL variables. The only global variables related to these systems are counters that count down when the player last played in each biome type, so let's say 6 global variables are needed to set up that. Is there a benefit to use an array to control that type of information as well?

  • Arrays only noticeably affect the performance if you do some heavy looping and if they're big. I use arrays mostly for inventory related stuff, because it's good to have a single central storage for all the loot across the player inventory, as well as any loot containers etc..

    Another thing I used an array for recently was calculating world temperatures for each hour and storing them into an array. So then you can access Arr.At(hour, 0). It makes sense for things like that. But when you need to store data like player variables - health, mana, stamina etc.., it's better to use 20-30 instance variables on the Player object, than an array, because it's hard to read an array, Array.At(0, 6) = mana... Maybe if you're a machine. It would be too hard to read.

    A biome system array won't likely have any performance impacts if you loop through it from time to time. It starts being critical I think after an array has a height over 10 and couple hundred width, then it can freeze the game for a second when you loop.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hi,

    I think it could make sense storing the object related info in object variable if there is ever only one instance of that object, like a player character for example.

    But if you have a project that could potentially have 100 different "unit" objects and each unit has 30 "stats" (like max hp, max mp, attack, defense etc.), and there can be several instances of one unit simultaneously on the playfield, I don't think it makes sense to use object variables for that data if the stats are universal for all object instances. It doesn't make sense that each instance would have the same data cloned to its variables multiple times instead of using a single database.

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