Hey all,
So I’m working on simple use of a State Machine, using C# and classes.
All I’m doing currently is having a cube that traverses between two points, starting in a Walk() state, and toggling between that and a Run() state when it hits each waypoint.
Also, this cube slowly loses energy, going into a sleep state from either the Walk() or Run() state when its’ energy reaches zero. It has no speed and gains energy in this state, going back to the stored previous state once energy is back to 100.
The following is the code for my State Machine, base State() class, and SleepState() child class.
public class StateMachine : MonoBehaviour
{
private State currentState;
private State previousState;
private double energyAdjustment;
private int movementSpeed;
void Start()
{
currentState = new WalkState();
energyAdjustment = 0;
}
int updateStateMachine (double energyCount)
{
if(energyCount <= 0)
{
previousState = currentState;
currentState = new SleepState();
}
if(energyCount >= 100)
currentState = previousState;
[COLOR="red"]movementSpeed = currentState.updateState(energyAdjustment);[/COLOR]
energyCount += energyAdjustment;
return movementSpeed;
}
}
public class State : MonoBehaviour
{
int updateState (double energyAdjustment) {return 0;}
}
public class SleepState : State
{
int updateState (double energyAdjustment)
{
energyAdjustment = 1;
renderer.material.color = Color.blue;
return 0;
}
}
There are several things going on in here that I may be doing wrong, as I don’t fully understand them. I’ll try to pinpoint them so it’s easier to understand my question:
- StateMachine() accepts a pointer from the cube for his energyCount, adjusting it each cycle.
- movementSpeed is checked by calling the updateState() of the currentState.
- energyAdjustment is passed into the currentState’s updateState(), to be set in that function, and added to the energyCount (passed into StateMachine).
- Finally, StateMachine sends back the movementSpeed, which is returned from the currentState’s updateState().
So, when doing this, on the red line of code, it says “‘State.updateState(double)’ is inaccessible due to its protection level”.
No idea what I’m doing wrong, and not even sure I’m doing anything right. I can’t use (*) pointers and such like C++, so I’m not sure if I’m passing in variables correctly.
If anyone could help, I’d truly appreciate it. I apologize for the length. If you need any clarification, let me know. Thanks in advance!