GML (solved) select random Boolean only with the value of true

I have four booleans, npc_canmove_up, npc_canmove_down, npc_canmove_left, ncp_canmove-right, in my npc object's variable definitions section. Then in my create event I started this self repeating alarm.

Code:
//start another alarm
alarm[0] = random_range(ncp_rotatetime_min,ncp_rotatetime_max);

//select random number for direction of npc
var i = irandom_range(1,4);

//rotate based on the random number and if npc_canrotate is true
if(i=1 and ncp_canrotate_right) direction = 0;
if(i=2 and ncp_canrotate_up)direction = 90;
if(i=3 and ncp_canrotate_left)direction = 180;
if(i=4 and ncp_canrotate_down)direction = 270;
This works perfectly, but there is one defect I don not like. Lets say the random number is 1, but the ncp_canmove_right is false, the the alarm will happen restart and nothing will happen. Which is fine, but I want the npc to move to move every time the alarm is called so ncp_canrotatetime_min and max to work properly.

I tried putting if statements in irandom_rage to only select the value if the boolean is true, but it doesn't seem to accept if statements inside it. Another idea I had was in the create event make a array based on the variable definitions and select a random value out of the array, but I'm not sure how to do that or if there is a better way to this all together.

That was a long one, if I didn't make sense or if you need you info feel free to ask.
 

FrostyCat

Redemption Seeker
It's really not that hard once you gather the choices inside an array or list.
Code:
var choices_count = ncp_canrotate_right+ncp_canrotate_up+ncp_canrotate_left+ncp_canrotate_down;
if (choices_count > 0) {
  var choices = array_create(choices_count),
      i = -1;
  if (ncp_canrotate_right) choices[++i] = 0;
  if (ncp_canrotate_up) choices[++i] = 1;
  if (ncp_canrotate_left) choices[++i] = 2;
  if (ncp_canrotate_bottom) choices[++i] = 3;
  direction = choices[irandom(i)]*90;
}
 
Sorry for the late response, this works perfectly! Apparently I need to learn more about using arrays. And multiplying the random number by 90 is just pure genius, never though of that. Thanks for your help!
Just noticed I put ncp instead of npc lol.
Again, thanks!
 
Top