Hey folks. I’m not a professionally trained programmer and I’m always looking for better practices, so this is a post asking for advice on script architecture and hygiene.
I tend to try to keep scripts fairly small and contained - usually if I go over 300 lines I start thinking about how to split the script up, and if I hit 400 lines I stop everything and break up the script in question.
However, I’m currently writing a large input script / controller for my main character and I’m hitting 300 lines with a lot more relevant, related code to write.
About 30 lines of this were dedicated to variables and registering and unregistering events related to the new InputSystem - as follows…
private PlayerControls controls;
protected Vector2 moveInputRaw; // Hot off the controls
protected bool runInputRaw = false;
protected float fwdInputRaw;
public virtual void Awake()
{
controls = new PlayerControls();
}
public virtual void OnEnable()
{
controls.Enable();
controls.BipedMovement.MoveWalk.performed += ctx => moveInputRaw = ctx.ReadValue<Vector2>();
controls.BipedMovement.MoveWalk.canceled += ctx => moveInputRaw = Vector2.zero;
controls.BipedMovement.RunModifier.started += ctx => runInputRaw = ctx.ReadValueAsButton();
controls.BipedMovement.RunModifier.canceled += ctx => runInputRaw = ctx.ReadValueAsButton();
controls.BipedMovement.fwdInputRaw.performed+= ctx => fwdInputRaw = ctx.ReadValue<float>();
controls.BipedMovement.fwdInputRaw.canceled += ctx => fwdInputRaw = 0.0f;
}
public virtual void OnDisable()
{
controls.BipedMovement.MoveWalk.performed -= ctx => moveInputRaw = ctx.ReadValue<Vector2>();
controls.BipedMovement.MoveWalk.canceled -= ctx => moveInputRaw = Vector2.zero;
controls.BipedMovement.RunModifier.started -= ctx => runInputRaw = ctx.ReadValueAsButton();
controls.BipedMovement.RunModifier.canceled -= ctx => runInputRaw = ctx.ReadValueAsButton();
controls.BipedMovement.fwdInputRaw.performed-= ctx => fwdInputRaw = ctx.ReadValue<float>();
controls.BipedMovement.fwdInputRaw.canceled -= ctx => fwdInputRaw = 0.0f;
controls.Disable();
}
Nothing shocking or surprising there, but it’s taking up a lot of space in my input class. My solution has been to offload this to a base class that my Input script can inherit from.
However - I’m used to thinking about Inheritance usually being used for, say, parent classes of NPCs that an NPC Soldier could inherit from, for example. My current use of inheritance seems to stray from that pattern.
Additionally, I’m afraid that I’m basically ‘hiding’ code (commonly used methods etc.) that another developer (or my future self) may need to find in order to debug or extend the code - is it worth enduring larger scripts but have the entire flow of the operations available in one document, or should I be stashing methods with clear names in a parent class, so the overall purpose and action of the script is evident (‘hiding’ some details)?
I’m confused. Any advice would be appreciated. =)