How do I prevent button from working if value is already in the array?

0 favourites
  • 7 posts
From the Asset Store
Antisuspend Plugin
$7.20 USD
20% off
Antisuspend Plugin for Construct 3 prevents the runtime from getting suspended.
  • I'm messing around with the "Arrays for Beginners" tutorial and I'm at the section where I create a button to add "cow" into the array. I've got it so that all the values of the array are displayed, so every time that button is pressed, the word "cow" shows up.

    How do I prevent the button from working if "cow" is already in the array?

    Here is the relevant line of code:

    Here is the output when I press the button more than once:

  • Add a second condition to the event:

    System compare two values: Array.indexOf("cow")<0

  • There's also the specific "contains value" condition

    Contains value

    earches the entire array to check if any of the elements contains the given value. For example, you can use this to test if the string "sword" is stored anywhere in the array.

  • Add a second condition to the event:

    System compare two values: Array.indexOf("cow")<0

    That worked, thank you.

    Just so that I can understand what's actually happening, this works because each element in an array is numbered, starting at 0. Initially there is no "cow", so the button works because "cow" is less than 0 meaning it's not there at all.

    Once the button is pressed and "cow" is created, it now has a value greater than 0 and so the action to create the "cow" element is not initiated.

    Is this correct?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Yeah that pretty much is correct. When the value is not in the array, IndexOf(value) will return -1

  • There's also the specific "contains value" condition

    > Contains value

    >Searches the entire array to check if any of the elements contains the given value. For example, you can use this to test if the string "sword" is stored anywhere in the array.

    I tried that initially, but I couldn't figure out how to get it to NOT insert "cow" if it was already there. Wouldn't I need some kind of ELSE statement of some sort? Basically it would be "If array contains value "cow", do nothing, ELSE insert "cow"" though I'm not familiar enough with Construct yet to know how to code that.

  • In this case you don't need an else. If you click the button the first time, the condition (left side) will be true, so the actions (right side) will be executed. The second time you click the button, the conditions will be false, so the actions will not be executed.

    A different story would be for example if you want to play a sound that is either "success" or "error". In that case you want a specific set of actions to play depending on the conditions. So you'd do

    if not cow in array -> add cow to array, play success sound

    else -> play error sound

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