❔ I have a basic structuring issue

this is unity but it doesn't really matter for the sake of this I have a character which can move around and look around, but sometimes the movement changes and the mouse movements might be menus so I have a main class and an array of possible active classes for controlling movement, and another array for controling viewing, along with a variable for the current one of each. Sometimes the movement class has to get information from the viewing class, how should i structure this i think that having a reference between the two will make it bad, I am not sure honestly, and I am not sure with the rest of the structure
3 Replies
boiled goose
boiled goose14mo ago
when in doubt, add a level of indirection
cap5lut
cap5lut14mo ago
not sure what kind of mess that would become, but u could do it with Action handler; fields and stuff it all in one partial class with multiple sources: PlayerController.cs
public partial class PlayerController : MonoBehavior
{
// your states and other stuff

private Action movementHandler;
private Action cameraHandler;

private void FixedUpdate() => movementHandler.Invoke();
private void LateUpdate() => cameraHandler.Invoke();
}
public partial class PlayerController : MonoBehavior
{
// your states and other stuff

private Action movementHandler;
private Action cameraHandler;

private void FixedUpdate() => movementHandler.Invoke();
private void LateUpdate() => cameraHandler.Invoke();
}
PlayerController.Grounded.cs
public partial class PlayerController
{
private void Grounded_MovementHandler()
{
if (!isGrounded)
{
Airborne_MovementHandler();
return;
}

movementHandler = Grounded_MovementHandler();

// actual handler code
}
}
public partial class PlayerController
{
private void Grounded_MovementHandler()
{
if (!isGrounded)
{
Airborne_MovementHandler();
return;
}

movementHandler = Grounded_MovementHandler();

// actual handler code
}
}
PlayerController.Airborne.cs
public partial class PlayerController
{
private void Airborne_MovementHandler()
{
if (isGrounded)
{
Grounded_MovementHandler();
return;
}

movementHandler = Airborne_MovementHandler();

// actual handler code
}
}
public partial class PlayerController
{
private void Airborne_MovementHandler()
{
if (isGrounded)
{
Grounded_MovementHandler();
return;
}

movementHandler = Airborne_MovementHandler();

// actual handler code
}
}
and so on. so u sorta have a separation, not too much nesting and still access to the same states
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.