I saw this in the manual:
Here's an example of using the array:
health[3] = 50;
health[4] = 100;
health[player.ID] = 10;
this sets Health 3 to 50, Health 4 to 100, and the Health index that corresponds to the player character's ID number to 10.
I understand what arrays are and how to use them, except I read this code and wonder what this means : health[player.ID] = 10;
I read the explanation about 5 times and I still don't understand.
Can someone explain this one to me?
Thanks in advance.
Quote from: nneader on Tue 25/06/2013 23:38:39
I understand what arrays are and how to use them, except I read this code and wonder what this means : health[player.ID] = 10;
It is a combination of commands:
1) take object "player";
2) get property "ID" of taken object;
3) use property's value as an index in the array.
More simple example is
int a = 6;
health[a] = 10;
second line means: take variable "a" and use it as an index in the array.
Ok..that makes sense.
I guess that would save some time.
Thanks!
It's not really about saving time (in a way it is though); each Character has a unique ID, so by using health[Character's_Script_Name.ID] you don't need to worry about the index number.
Imagine having to write something like:
if (player == cDave) health[1] = health[1] + 5;
if (player == cBernard) health[2] = health[2] + 5;
... // repeat for every single character in the game
This is precisely how you SHOULDN'T code.
And health[player.ID] will always refer to the current player character's health, regardless which Character it is (which would come in handy in a game in which you can control multiple different characters).
It's a bit like math; 3x + 2 isn't a completely new thing, it's just the sum of 2 and a product, which are known, simple concepts.