Better An Array or instance variables?

  • Hi, i have a doubt. At the moment i'm using 2 arrays to handle the card hand of players. In this array i have 5 rows and 5 columns (5,5,1)

    The first value identify the card, the other 4 values in the column the values. These values can change during play.

    i'm in doubt if it's better to handle those 4 values with the array or to put, instead, 4 instance variables in the card object...

    Any suggestions?

  • To make your game more extensible (allowing more/less users or more/less cards in a players hand) it would be better to give each card it's own stats. However, this can still be done with an array if you like. Simply put a card object and it's associated array (in this case it would be a 5x1x1 array) in a container. Using the container, each time you spawn a card, it's associated array is created and each time you reference a card, you also reference it's array. If you prefer working with arrays as opposed to instance variables this may be the way to go.

    Personally, I prefer to use named instance variables because it helps me keep track of what each variable is doing. Referencing a position of an array doesn't really tell me anything about what information I am accessing. If you need to make a change a year from now, will you still know exactly what each array position represents? What if you get a team to help you and want one of them to make the change. How easy will it be for someone else to figure out what the array is doing? Even if you comment it really well, it will probably still take longer to change the array based objects than one with named instance variables.

  • Wow... thanks for your kind reply!!! I chosed the array because i thought that was better to handel 2 arrays instead that 4 instance variables multiplied for every instance of the object... then i found myself inside of a Function, trying to apply it to an array instead of the other depending on the game turn and was a little bit messy. All easier with instance variables. But mine was a baseless idea, because i do not really know if those variables are heavier of 2 arrays or if it's absolutely not important

    What do you think

    However thanks again for your precious help, was really appreciated!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If there is a size difference (and I say "if" because in most programming languages, there is no size difference after the project is compiled) between an array and the same number of instance variables, it isn't enough to worry about. It will really just come down to how you prefer to store the data.

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