Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
does a case (switch statement) has to be "case 1/2/3/...)?
Apjjm Offline
Is easy to say

Posts: 496
Threads: 18
Joined: Apr 2011
Reputation: 52
#7
RE: does a case (switch statement) has to be "case 1/2/3/...)?

An array is just a collection of values. So you may ask:
  • How do i make an array?
  • How do i get items from an array?
The first part is answered by example - from the code earlier:
float[] oilTargets = {0.0f,5.0f,12.5f};
That line says make me a float array called "oilTargets" that initially contains 3 elements (the numbers in the brackets).

To be able to get items that were put in the array you can use the square brackets to "index" the array:
oilTargets[0]
The above expression says pick the element at index 0 (the first element - computers typically start counting from 0). We could also choose element 1 or element 2 in a similar manner. Note that if our array has N items (length=N) then the last accessible index is N-1 (e.g. if our array has 4 items we can only access 0,1,2,3 indexes).

Arrays become useful when as we don't have to hardcode the number in the square brackets in - we can use a variable there instead:
for(i=0; i<3; i+=1;)
  AddDebugMessage("Entry " + i + " is " + oilTargets[i],false);
The above code loops through the first 3 elements in the array and prints out the value of that element (The variable i takes the values 0,1,2). This loop and look at elements stuff is really handy for avoiding code repetition.

This was very much a superficial introduction to arrays to give some intuition into what is going on in the code i wrote.
02-22-2013, 05:50 PM
Find


Messages In This Thread
RE: does a case (switch statement) has to be "case 1/2/3/...)? - by Apjjm - 02-22-2013, 05:50 PM



Users browsing this thread: 1 Guest(s)