System based off randomized features

Hello, I’m making a game where you act as a police sketch artist. I have to code a system that detects what the true features are and change variables (such as character dialogue and accuracy of the players sketch) depending on that.

I was planning to just do a bunch of if statements to check what feature is the true one, but I was wondering if there was a more efficient way to do so. Sorry if this question is somewhat incoherent. I’m confused and would like some tips or advice.

1 Like

Using if statements is not a bad thing … but, since you’re programming in JavaScript, the switch statement is a little more elegant:

switch (level) {
    case 1:
        // Do something
        break

    case 2:
        // Do something else
        break

    default:
        // Code here will run if no other cases were selected
}

A much better way to do this, though, would be to use arrays if that makes sense for your code.

let witnessImages: Image[] = [
    assets.image`witness1`,
    assets.image`witness2`,
    ....
]

witness.setImage(witnessImages[level])
1 Like