BlandLife
BlandLife
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
For each class that inherits from StateMachine I'll just make a constructor and assign to the currentState there, like so:
C#
public class PlayerSM : StateMachine
{
// [SerializeField] private static InputActionAsset playerActions;
PlayerIdleState idleState;
public PlayerSM() {
idleState = new PlayerIdleState(this);
currentState = idleState;
}

}
C#
public class PlayerSM : StateMachine
{
// [SerializeField] private static InputActionAsset playerActions;
PlayerIdleState idleState;
public PlayerSM() {
idleState = new PlayerIdleState(this);
currentState = idleState;
}

}
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
So I just did away with the constructor in the StateMachine class
C#
using UnityEngine;

public abstract class StateMachine : MonoBehaviour
{
protected IState currentState {get; set;}

void Start() {
currentState.Start();
}

void Update() {
currentState?.Update();
}

public void TransitionState(IState nextState) {
currentState?.Exit();
currentState = nextState;
currentState.Start();
}
}
C#
using UnityEngine;

public abstract class StateMachine : MonoBehaviour
{
protected IState currentState {get; set;}

void Start() {
currentState.Start();
}

void Update() {
currentState?.Update();
}

public void TransitionState(IState nextState) {
currentState?.Exit();
currentState = nextState;
currentState.Start();
}
}
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
I think i got it where I want it
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
All good all good, thanks for the help
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
C#
public class PlayerSM : StateMachine
{
[SerializeField] private static InputActionAsset playerActions;
static PlayerIdleState idleState = new PlayerIdleState(playerActions);
This.currentState = idleState; // couldn't write any line like this
PlayerSM.currentState = idleState; // or this, since neither exist in the current context, or so it says
public PlayerSM(IState defaultState) : base(idleState) {}

}
C#
public class PlayerSM : StateMachine
{
[SerializeField] private static InputActionAsset playerActions;
static PlayerIdleState idleState = new PlayerIdleState(playerActions);
This.currentState = idleState; // couldn't write any line like this
PlayerSM.currentState = idleState; // or this, since neither exist in the current context, or so it says
public PlayerSM(IState defaultState) : base(idleState) {}

}
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
I was doing all this bc I couldn't seem to just assign currentState myself in the PlayerSM
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
But yeah I don't seem to see any other cause for the error aside from that line in the file itself rip, it seems perfectly happy the moment I add in the static keyword
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
And the statemachine it inherits from
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
C#
public abstract class StateMachine : MonoBehaviour
{
protected IState currentState {get; private set;}

// References to states goes here
// e.g public IdleState idleState;

public StateMachine(IState defaultState) {
currentState = defaultState;
}

void Start() {
currentState.Start();
}

void Update() {
currentState?.Update();
}

public void TransitionState(IState nextState) {
currentState?.Exit();
currentState = nextState;
currentState.Start();
}
}
C#
public abstract class StateMachine : MonoBehaviour
{
protected IState currentState {get; private set;}

// References to states goes here
// e.g public IdleState idleState;

public StateMachine(IState defaultState) {
currentState = defaultState;
}

void Start() {
currentState.Start();
}

void Update() {
currentState?.Update();
}

public void TransitionState(IState nextState) {
currentState?.Exit();
currentState = nextState;
currentState.Start();
}
}
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
Was doing small stuff to get it generally working so that I can build up the actual state machines after
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
Ye real short lol
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
C#
public class PlayerSM : StateMachine
{
[SerializeField] private static InputActionAsset playerActions;
PlayerIdleState idleState = new PlayerIdleState(playerActions);
public PlayerSM(IState defaultState) : base(idleState) {}

}
C#
public class PlayerSM : StateMachine
{
[SerializeField] private static InputActionAsset playerActions;
PlayerIdleState idleState = new PlayerIdleState(playerActions);
public PlayerSM(IState defaultState) : base(idleState) {}

}
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
uh here's the whole file btw
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
Keep getting that message when I remove static
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
An object reference is required for the non-static field, method, or property 'PlayerSM.idleState'
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
Sounds cool for some mechanic tbh, but yes I can see how generally that'd be a problem LOL
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
rip
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
So like, if one enemy goes to patrol state all will?
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
Ah rip, so for numerous enemies it might be an issue?
102 replies
CC#
Created by BlandLife on 10/7/2024 in #help
Building Constructors 101
I still never did figure out, so for abstract class implementations if I have a class that inherits from it but don't make/override one it's methods it'll just default to the parent's implementation yes?
102 replies