4
\$\begingroup\$

Would it be better to have one script that switches or have multiple scripts with each with their own function?

In example, Lets say I am making a powerup script.

Option 1: Create a single powerup script that switches the effects

switch (/*Enum of effects*/) 
{
    case Effect.IncreaseHealth:
        health += amount;
}

Option 2: Create multiple scripts that each does its individual function

Class IncreaseHealthPowerup
{
    protected void Start()
    {
        health += amount
    }
}

This is different from the question of Multiple Script versus Single Script as it is already in component form. The question is whether to further segregate down to the types of component.

I would also like to ask the Pros and Cons of each option with regards to potential development issues, or lack of, as well as the overhead caused in either cases.

\$\endgroup\$
2
\$\begingroup\$

The Unity3D tutorials actually have a chapter about this:

https://unity3d.com/learn/tutorials/modules/intermediate/scripting/coding-practices

The author would argue that your switch already gives you a structure to modularize from. You have different effects like Increase Health, Take Damage or Give Item. Those can be astracted in interfaces like

  • IDamageable – Interface for classes which can accept damage
  • IInventory – Interface for classes which can store and retrieve items

You then compose your game objects of different implementations of those interfaces like a wizard has a different implementation of IDamageable than a dog.

\$\endgroup\$
1
\$\begingroup\$

Single switch script

  • Everything is in one place, better readability.
  • It will be a lot easier to add cases (powerups) when needed.
  • Centralized ressources = good.

Multiple scripts

  • Can help when the cases have too much lines.
  • Object-oriented, can be easier to call cases.

Looking at your example, the best way to go would be to have one script with a switch case. The example case you gave is simple and I don't really see the need to put it in another file. This would only cause the code to be difficult to maintain, mainly because each case would be in it's own file of 7 lines. For simple cases, keep them in a switch case.

You can, however, put some of your cases in seperate files if they take too much space and harm readability. The switch case would then call the said script.

Keep this in mind: concise code is ofter better, especially when programming video games. Yes, there are good things about OOP but making a class for one line of code is just absurd. KISS (Keep it stupid simple).

\$\endgroup\$
0
\$\begingroup\$

I use multiple scripts.

A power-up is a prefab which is then composed of multiple components which will then apply the changes separately:

  • Resize Changes
  • Speed Changes (can have configurable values)
  • Health Changes (can have configurable values)
  • Visual Effects (reacts to the start and end of the other scripts)

This way each script can be configured to apply its effect for a longer or shorter period of time.

Each script then emits Messages when its effects starts or when it reaches the end of its effect.

This way I can add visual effects which will trigger at the end of some of the effects.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.