How to pick a random range of variables stored in an array?

0 favourites
  • 7 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • I'm sorry for such a simple question, but I can't seem to find a straightforward answer to this.

    My AI enemies choose a target direction to face before moving.

    I have one tilechecker sprite that checks for obstacles before allowing my AI enemies to face and move in said direction.

    With that, I have an array where my enemies will keep track of all of their possible directions they can face.

    If the tilechecker covers an obstacle, they need to remember that they cannot go in that direction anymore so that target direction is deleted from the array before checking for another suitable direction until it finds a clear direction to start facing. This is to prevent enemies rotating to face a direction they cannot go in and look silly.

    I want my enemies to pick a random available direction that is in the array, but I am doing this a bit wrong I think.

    Whenever I set i_TargetDirection to random(enemy.IndexOf(enemy.i_TargetDirection))

    Instead of picking these values:

    It picks these values:

    Or in the case of the above expression example it actually randomises the number like 0.5234523452345 and ignores the fact that I want to select from the enemy's instance variable.

    What's weird about this is I can choose to delete the value accurately with Self.IndexOf(Enemy.i_TargetDirection) on the array object. But how do I randomly pick a range of values stored in the array and not the element number? If I delete the element number, the element numbers re-count themselves and it changes, the values stored don't do this, and my enemy gets stuck going into an obstacle they can't move through because the wrong value was deleted from the array.

    Also for your information, the i_TargetDirection values mean the following:

    0 = idle (This is not seen in the array because it is not a value that is used to tell the character to face a direction)

    1 = right

    2 = Down

    3 = Left

    4 = Up

    Your assistance is greatly appreciated. Thanks!

  • To pick a random value from the array, use this expression:


  • Thanks dop2000 but I think I am still doing something wrong here.

    It seems to be deleting the correct variable when it is not available but then picking it right after it gets deleted. Here's a screenshot of my events:

    This is where it deletes if it it's overlapping an obstacle then calls the function for the enemy to pick an available direction:

    This is where it picks an available direction:

    It deletes first then picks, I don't know why it's picking a deleted direction though.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Your events seem ok to me, it's difficult to say why they don't work.

    I suggest you output Array.AsJSON and i_TargetDirection into Browser Log in all these events, to see what exactly is happening with the array and the variable.

    I understand, this character is moving on a grid? Why don't you use Tile Movement behavior? This may be easier.

  • Sorry for being so vague with my events Stan.

    I am using the tilemovement behaviour. It is core to character movement in my game.

    However, I found what the problem is. It is this:

    Because Chomp's current direction is not facing the target direction, they are not matching and because of that, this event does not run, hence he gets stuck.

    So What I did was disable this event to test it, but then this happens:

    And then two things happen:

    Chomp spawns then does not grow or Construct crashes to the desktop with no error message.

    This is really irritating because I can't replicate this issue in a fresh project in order to make a bug report but this only happens when I disable that event condition. I don't really feel like posting my whole project and events in a public repository just for Ashley to see what's going on and fix it.

    I may have to look at a work around but I don't know how else to work around this issue at the moment. The most logical solution to fix it was to disable this event and it makes sense for it to keep working but I'm getting other problems happening. I may have to look at how this is impacting his spawning now. It's hard to ask to send this to you because I've already done that with my last problem I shared on the forum. :P

  • This is not a bug. Your game is crashing because you created an infinite loop, probably recursively calling the same function.

    Here is a simple demo I made with Tile Movement, maybe it will help you:

  • Ahh, I know what's causing the infinite loop. I created this:

    So when I disabled i_CurrentDirection = i_TargetDirection condition on Change_Chomp_Direction it exceeds the call stack.

    I've had a look at your example. Unfortunately my project is too complicated to restructure around that at this current point. I will look at major restructure options because I do believe I could clean a lot of this up, and I could use your help with that of course, but at the moment with the way I have designed my game, this is the only thing I need to do for enemy AI so I can finish all of my levels off.

    What I've done is re-enable the disabled condition and disabled the event that was causing the infinite loop (I'll probably end up deleting it). So now he is back to only getting stuck if the tile checker sprite chooses a direction where an obstacle already is. It seems like he is picking an unsuitable direction after it gets deleted from the array.

    I think it's best if I send you my project so you can see all of it properly so you can tell me what's going wrong here. I know I'm really close to getting this right, I just keep making the wrong turn with it so I don't think it will take too much of your time.

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