I am new into game development and after a couple of simple game I decided to challeng myself.
I have problem with logic and mechanic
I have my state machine and I have my jumping logic with a public jumpForce.
In my previous state machine with enum and switch I used to have a Jump function that was helping me to swap state and pass a jumpForce to it.
Now my problem is that I have my Player with my stateMachineNode that SwitchState.
Should I keep my switch logic inside the states or can I have a Jump function that can be called from IJumpable for example and SwitchState to JumpState with a Force variable?
could you provide a screenshot of your state machine layout (the node hierarchy) , as well as some rough code, so we know what each of the classes you are talking about is. I will gladly help you after that.
I kind of fix it, I added in my State logic that when the velocity.Y is < 0 it will automatically switch to jump state, so I added a Jump in my Player class that push the Velocity.Y in order to make it go up. Everything works fine, but I don’t know if it is logically correct
public partial class States : Node
{
[Export(PropertyHint.Range, "-600, -100, 25")] protected private float jumpVelocity = -200;
protected Player _player;
//Moving Variables
[ExportCategory("Movement Controller")]
[Export(PropertyHint.Range, "100, 250, 10")]
protected float speed = 200.0f;
protected bool canTeleport = false;
private protected bool isJumping = false;
public override void _Ready()
{
_player = GetOwner<Player>();
SetPhysicsProcess(false);
SetProcess(false);
}
public override void _PhysicsProcess(double delta)
{
if (_player.Velocity.Y < 0)
{
_player.StateMachineNode.SwitchState<JumpState>();
}
public override void _Input(InputEvent @event)
{
if (Input.IsKeyPressed(Key.Space) && _player.IsOnFloor())
{
_player.Jump(jumpVelocity);
}
}
public partial class JumpState : States
{
public override void _Ready()
{
base._Ready();
}
public override void _PhysicsProcess(double delta)
{
if (_player.velocity.Y > 0)
{
_player.myAnimationPlayer.Play(PlayerConstants.ANIM_FALL);
}
if (_player.IsOnFloor())
{
isJumping = false;
if (_player.direction != 0) _player.StateMachineNode.SwitchState<MoveState>();
else
{
_player.velocity = Vector2.Zero;
_player.StateMachineNode.SwitchState<IdleState>();
}
}
else
{
_player.velocity.X = _player.direction * speed;
}
base._PhysicsProcess(delta);
}
protected override void EnterState()
{
//_player.velocity.Y = jumpVelocity;
isJumping = true;
_player.myAnimationPlayer.Play(PlayerConstants.ANIM_JUMP);
}
public partial class Player : CharacterBody2D
{
[ExportGroup("Required Nodes")]
[Export] public StateMachine StateMachineNode { get; private set; }
//....... something
public void Jump(float _jumpVelocity)
/* Allow the player to Jump by passing a velocity */
{
velocity.Y = _jumpVelocity;
Velocity = velocity;
}
Put all of the jump logic in the jump state code. The velocity, the floor check, everything.
The central state machine would then be a switcher, one that makes sure multiple states are fighting over control of the player and his systems.
The code would look something like having an ‘active’ boolean variable in each state, which gets set true/false by the central state machine node. Or you can use the ‘set_process(boolean)’ function to turn the process function on/off entirely on the state nodes.
That was at the beginning like that but I didn’t take in consideration my Jumping platform that should push the Player upward.
When I collide the player should change the state in Jumping, how do I do that my IJumpable can swith my player state?