GML How to check array values?

Edwin

Member
Code:
// Create Event
array[0] = 0;
array[1] = 0;
array[2] = 0;

// Step Event
if (keyboard_check_pressed(vk_right)) {
    array[random(array_length_1d(array))] += 1;
}
So basically I want to check if any of the array's value is greater that 0, then keep it adding 1 to the array.

Please help!
 
Do you want the 1 to get added only to the entry that is greater than one? If so:
Code:
for (var i=0;i<array_length_1d(array);i++) {
  if (array[i] > 0) {
    array[i]++;
  }
}
If you want them all to get 1 added if any is above 0 then:
Code:
var inc = false;
var size = array_length_1d(array);
for (var i=0;i<size;i++) {
  if (array[i] > 0) {
    inc = true;
  }
}
if (inc) {
  for (var i=0;i<size;i++) {
    array[i]++;
  }
}
EDIT: You can't just put these in the Step Event btw, otherwise the array values will increment wildly. You'll have to put them behind some sort of check, whether it be an alarm, a boolean check:
Code:
if (variable == true) {
  //Do code
  variable = false;
}
Etc, just something to stop it from happening every step.
 

Pyxus

Member
Code:
// Create Event
array[0] = 0;
array[1] = 0;
array[2] = 0;

// Step Event
if (keyboard_check_pressed(vk_right)) {
    array[random(array_length_1d(array))] += 1;
}
So basically I want to check if any of the array's value is greater that 0, then keep it adding 1 to the array.

Please help!
Code:
var len = array_length_1d(array);
for (var i = 0; i< len; i++) {
 if (array[i] > 0) {
  array[i] ++;
}
}
I cant test this code write now but I beleive it should work. Basically you use a for loop to cycle through all the arrays, and if a array value is greater than 0 increase the array value by 1.

Edit: Ninja'd by @RefresherTowel ;P
 

Edwin

Member
Do you want the 1 to get added only to the entry that is greater than one? If so:
Code:
for (var i=0;i<array_length_1d(array);i++) {
  if (array[i] > 0) {
    array[i]++;
  }
}
If you want them all to get 1 added if any is above 0 then:
Code:
var inc = false;
var size = array_length_1d(array);
for (var i=0;i<size;i++) {
  if (array[i] > 0) {
    inc = true;
  }
}
if (inc) {
  for (var i=0;i<size;i++) {
    array[i]++;
  }
}
EDIT: You can't just put these in the Step Event btw, otherwise the array values will increment wildly. You'll have to put them behind some sort of check, whether it be an alarm, a boolean check:
Code:
if (variable == true) {
  //Do code
  variable = false;
}
Etc, just something to stop it from happening every step.
Code:
var len = array_length_1d(array);
for (var i = 0; i< len; i++) {
 if (array[i] > 0) {
  array[i] ++;
}
}
I cant test this code write now but I beleive it should work. Basically you use a for loop to cycle through all the arrays, and if an array value is greater than 0 increase the array value by 1.
Thanks alot eveyone! I made it and I have a second question here. I'll be glad to you guys help me :p
 
Top