Doubts about the structure of my scripts

Despite everything going fairly smoothly, I still have doubts about the structure of my scripts. Now that my game is getting more complex again, if statements threaten to become extremely long with all kinds of conditions. Is this normal or am I on the wrong track?

Example one:

if (this.avoidSpeed === 0 || this.moveSpeed < 0.5 || (!this.avoidInBetween && this.targetIsVisible) || (!this.avoidInBetween && this.targetDistance < 1)) {

Example two:

if (enemyEntity.enabled === true && enemyEntity.script.enemy.state !== "Combat" && enemyEntity.script.enemy.state !== "Chase" && enemyEntity.script.enemy.state !== "Dead") {

Yes, that can be a problem in bigger games. I’d say something that helps a lot is a decent state manager. So you can group those big states and even smaller substates to meaningful names.

1 Like

Something like: Finite State Machine

Like a dedicated library that handles the code for state transitions.

1 Like

Thank you both! I already have a custom made kind of state machine. Probably I need an extra function that divides particular conditions to one new condition that I can use else where.

Something like this:

if (this.entity.enabled === true && this.state !== "Combat" && this.state !== "Chase" && this.state !== "Dead") {
    this.enemyIsAvailable = true;

else {
    this.enemyIsAvailable = false;

It will change example two to:

if (enemyEntity.script.enemy.enemyIsAvailable === true) {

In that case I will get one big update function that handles all conditions, is that a good way to go?

1 Like